Advertisement
milanmetal

[Linux-C] Thread function, websocket communication

Jan 26th, 2018
156
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.35 KB | None | 0 0
  1. /*
  2.  * Dependency: https://github.com/mortzdk/Websocket
  3.  * This thread function constantly sends data read from serial port.
  4.  */
  5. void *broadcastFromSerial(void *serialBuffer) {
  6.     pthread_detach(pthread_self());
  7.     (void) serialBuffer;
  8.     char buffer[16] = "milannnnn<br>";
  9.  
  10.     int n = 100;    // random value
  11.     while(1) {
  12.         usleep(1000000);
  13.         ws_connection_close status;
  14.  
  15.         ws_message *m = message_new();
  16.         m->len = strlen(buffer);
  17.  
  18.         char *temp = malloc( sizeof(char)*(m->len+1) );
  19.         if (temp == NULL) {
  20.             raise(SIGINT);
  21.             break;
  22.         }
  23.  
  24.         /*
  25.          * Test koji generise promjenjiv sadrzaj za slanje.
  26.          * https://stackoverflow.com/questions/2279379/how-to-convert-integer-to-char-in-c
  27.          *
  28.          * A char in C is already a number (the character's ASCII code), no conversion required.
  29.          *
  30.          * If you want to convert a digit to the corresponding character, you can simply add '0':
  31.          *
  32.          * c = i +'0';
  33.          * The '0' is a character in the ASCll table.
  34.          *
  35.          */
  36.         char a = (n % 10) + '0';
  37.         buffer[7] = a ;
  38.  
  39.         memset(temp, '\0', (m->len+1));
  40.         memcpy(temp, buffer, m->len);
  41.         memset(temp, (n % 10) + '0' , 5);
  42.         m->msg = temp;
  43.  
  44.         temp = NULL;
  45.  
  46.         if ( (status = encodeMessage(m)) != CONTINUE) {
  47.             message_free(m);
  48.             free(m);
  49.             raise(SIGINT);
  50.             break;;
  51.         }
  52.  
  53.         list_multicast_all(l, m);
  54.         message_free(m);
  55.         free(m);
  56.         n--;
  57.     }
  58.         return 0;
  59. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement