Re: [gnet-devel] GIOChannels



Ok, here is the relevant code:

// Chat update - this successfully gets text from a text widget and sends to another function for GNet to handle
long MainWindow::onChatUUpdt(FXObject *sender, FXSelector, void *)
{
  // gets the index of the current tab
  current_child = chattabs->getCurrent();
  
  // make this fetch the alias for the appropriate network
  curr_alias = "Alias";
  FXString bcText = curr_alias;
  bcText.append(": ");
  int nlength = bcText.FXString::length();
  
  // fetch the bottom chat of the current tab
  FXWindow* bottom_win = ((((chattabs->childAtIndex(current_child * 2))->getNext())->childAtIndex(0))->childAtIndex(1))->childAtIndex(0);
  FXText* bottom_chat = ((FXText*)bottom_win);
  
  FXString text = bottom_chat->getText();
  FXint len = bottom_chat->getLength();
  // if there is text in the bottom chat...
  if(text != "" && text[len - 1] == '\n')
  {
    // get the text of the bottom chat and append it to bcText
    bcText.append(bottom_chat->getText());
    // clear the text of the bottom chat
    bottom_chat->setText("", 0, true);
    
    // fetch the top chat of the current tab
    FXWindow* top_win = ((((chattabs->childAtIndex(current_child * 2))->getNext())->childAtIndex(0))->childAtIndex(0))->childAtIndex(0);
    FXText* top_chat = ((FXText*)top_win);
    
    // append bcText to the top chat
    top_chat->appendText(bcText);    
    
    int length = bcText.FXString::length();
    char sts[length];
    int l;
    for(l = nlength; l < length; l++)
    {
      if(bcText[l] == '\n')
      {
        sts[l - nlength] = bcText[l];
        sts[l - nlength + 1] = '\0';
        break;
      }
      sts[l - nlength] = bcText[l];
    }  
    length = strlen(sts);
    sendStringToGnet(sts, length);
  }  
  return 1;
}

// once this function is called it writes the string given it to a GIOChannel (doing this somehow also ends up printing the string to stdout if the program was launched from the terminal)
void sendStringToGnet(char* s, int len)
{
  lenOfOutString = len;
  s[len] = '\0';
 
  GIOChannel* sin;
  GIOError error;
  gsize bytes_read = strlen(s);
  gsize bytes_written;
     
  sin = server_iochannel;
  error = gnet_io_channel_writen (sin, s, bytes_read, &bytes_written);
  /*g_print ("# write %d\n", bytes_written); */

  g_assert (error == G_IO_ERROR_NONE);
  g_assert (bytes_written == bytes_read);
  return;
}


/* this is my slightly modified version of the main function from the GNet async-server example
  everything compiles and runs properly (on linux anyways) except my GIOChannel issue
void* netmain(void* arg)
{
  GTcpSocket* server;
  GInetAddr*  addr;
  gchar*      name;
  gint    port;
  GMainLoop*  main_loop;
 
  gnet_init ();
 
  port = atoi(((peer*)arg)->port);
 
  /* Create the server */
  server = gnet_tcp_socket_server_new_with_port (port);
  if (server == NULL)
  {
    fprintf (stderr, "Could not create server on port %d\n", port);
    exit (EXIT_FAILURE);
  }

  async_server = server;
  signal (SIGINT, async_sig_int);

  /* Print the address */
  addr = gnet_tcp_socket_get_local_inetaddr(server);
  g_assert (addr);
  name = gnet_inetaddr_get_canonical_name (addr);
  g_assert (name);
  port = gnet_inetaddr_get_port (addr);
  g_print ("Online and listening on %s:%d\n", name, port);
  gnet_inetaddr_delete (addr);
  g_free (name);

  /* Create the main loop */
  main_loop = g_main_new (FALSE);

  /* Wait asyncy for incoming clients */
  gnet_tcp_socket_server_accept_async (server, async_accept, NULL);

  /* this is the problem right here. I think that the input written directly to the GIOChannel from my text widget
     doesnt meet the G_IO_IN condition because the channel was initalized to read from stdin,
     however i dont know how to initalize it any other way, and g_io_channel_init () makes my program segfault */
  server_iochannel = g_io_channel_unix_new(fileno(stdin));
  g_io_add_watch (server_iochannel, (GIOCondition)(G_IO_IN | G_IO_ERR | G_IO_HUP | G_IO_NVAL),
    async_srv_iofunc, client_iochannel);
 
  /* Start the main loop */
  g_main_run (main_loop);

  exit (EXIT_SUCCESS);
 
  return 0;
}

I hope this helps and does not appear as just a garbled mess of code. I appreciate your help.

~Nate


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]