comparison core/client.cpp @ 41:4255ecbb9bec ultra-staging

Timers to measure performances, ultra experimental
author Giulio Moro <giuliomoro@yahoo.it>
date Tue, 19 May 2015 16:41:07 +0100
parents 12ee9a61e81d
children
comparison
equal deleted inserted replaced
40:419ce4ebfc4c 41:4255ecbb9bec
5 #include <fcntl.h> 5 #include <fcntl.h>
6 #include "../include/client.h" 6 #include "../include/client.h"
7 #include <unistd.h> 7 #include <unistd.h>
8 #include <rtdk.h> 8 #include <rtdk.h>
9 9
10 #define BUFF_LEN 1024 10 #define MESSAGE_BUFF_LEN 1024
11 #define MAX_VAR_STRING 20 11 #define MAX_VAR_STRING 20
12 12
13 struct sockaddr_in outServer, inServer; 13 struct sockaddr_in outServer, inServer;
14 int outSock, inSock, n, length; 14 int outSock, inSock, n, length;
15 socklen_t fromlen; 15 socklen_t fromlen;
45 return 0; 45 return 0;
46 } 46 }
47 int sendMessage(networkData message) 47 int sendMessage(networkData message)
48 { 48 {
49 unsigned int length; 49 unsigned int length;
50 char buffer[BUFF_LEN]; 50 char buffer[MESSAGE_BUFF_LEN];
51 length=sizeof(struct sockaddr_in); 51 length=sizeof(struct sockaddr_in);
52 int k=0; 52 int k=0;
53 k=sprintf(buffer+k, "%8d;",*message.counter); 53 k=sprintf(buffer+k, "%8d;",*message.counter);
54 for(int j=0; j<message.numVariables; j++){ 54 for(int j=0; j<message.numVariables; j++){
55 k+=sprintf(buffer+k, "%.3f;",*message.variables[j]); 55 k+=sprintf(buffer+k, "%.3f;",*message.variables[j]);
56 if(k>BUFF_LEN - 20) //safety margin 56 if(k>MESSAGE_BUFF_LEN - 20) //safety margin
57 continue; 57 continue;
58 } 58 }
59 sprintf(buffer+k,"\n"); 59 sprintf(buffer+k,"\n");
60 // printf(buffer); 60 // printf(buffer);
61 n=sendto(outSock,buffer, 61 n=sendto(outSock,buffer,
62 strlen(buffer),0,(const struct sockaddr *)&outServer,length); 62 strlen(buffer),0,(const struct sockaddr *)&outServer,length);
63 if (n < 0) error("Sendto"); 63 if (n < 0) error("Sendto");
64 return 0; 64 return 0;
65 } 65 }
66 /* 66
67 int receiveMessage() 67 int sendAudio(networkAudio *audio)
68 { 68 {
69 int n = recvfrom(inSock,inBuffer,1024,0,(struct sockaddr *)&from,&fromlen); 69 unsigned int length;
70 if (n < 0) error("recvfrom"); 70 length=sizeof(struct sockaddr_in);
71 printf("Received a datagram: "); 71 // for(int k=0; k<NETWORK_AU DIO_BUFFER_SIZE; k++)
72 printf(inBuffer); 72 // printf("%f\n",audio.buffers[!audio.currentBuffer][k]);
73 } 73 n=sendto(outSock,audio->buffers[!audio->currentBuffer],NETWORK_AUDIO_BUFFER_SIZE*sizeof(float),0,(const struct sockaddr *)&outServer,length);
74 */ 74 if (n < 0) error("Sendto");
75 75 audio->doneOnTime=1;
76 return 0;
77 }
76 78
77 int receiveMessage(networkData message){ 79 int receiveMessage(networkData message){
78 struct timeval stTimeOut; 80 struct timeval stTimeOut;
79 fd_set stReadFDS; 81 fd_set stReadFDS;
80 FD_ZERO(&stReadFDS); 82 FD_ZERO(&stReadFDS);
99 printf("Received a datagram: "); 101 printf("Received a datagram: ");
100 printf(inBuffer); 102 printf(inBuffer);
101 //the worst parser ever 103 //the worst parser ever
102 int previousN=0; 104 int previousN=0;
103 int currentVariable=0; 105 int currentVariable=0;
104 for(int n=0; inBuffer[n]!=0 && currentVariable<message.numVariables && n-previousN<MAX_VAR_STRING && n<BUFF_LEN; n++){ //scan the string 106 for(int n=0; inBuffer[n]!=0 && currentVariable<message.numVariables && n-previousN<MAX_VAR_STRING && n<MESSAGE_BUFF_LEN; n++){ //scan the string
105 if(inBuffer[n]==';'||inBuffer[n]==0||inBuffer[n]=='\n'){ // if you find a separator or you are at the end of the string, parse the variable 107 if(inBuffer[n]==';'||inBuffer[n]==0||inBuffer[n]=='\n'){ // if you find a separator or you are at the end of the string, parse the variable
106 int j=0; 108 int j=0;
107 inBuffer[n]=0; //set the semicolon to 0 ... 109 inBuffer[n]=0; //set the semicolon to 0 ...
108 while( (variableString[j++]=inBuffer[previousN++]) );; // ... so that this will stop when it gets there 110 while( (variableString[j++]=inBuffer[previousN++]) );; // ... so that this will stop when it gets there
109 rt_printf("variable %d: %s\n", currentVariable, variableString); 111 rt_printf("variable %d: %s\n", currentVariable, variableString);