Changeset 363 in flair-src for trunk/lib/FlairCore/src/UdpSocket_impl.cpp
- Timestamp:
- Sep 3, 2020, 8:54:16 AM (4 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/lib/FlairCore/src/UdpSocket_impl.cpp
r213 r363 53 53 void UdpSocket_impl::Init(void) { 54 54 int yes = 1; 55 sock_in_size=0; 56 sock_in=NULL; 55 57 56 58 fd = socket(AF_INET, SOCK_DGRAM, 0); // UDP … … 136 138 self->Err("gethostbyname\n"); 137 139 } 138 139 sock_in.sin_addr = *(in_addr *)hostinfo->h_addr; 140 sock_in.sin_port = htons(port); 141 sock_in.sin_family = AF_INET; 140 sock_in=(struct sockaddr_in*)malloc(sizeof(struct sockaddr_in)); 141 sock_in[0].sin_addr = *(in_addr *)hostinfo->h_addr; 142 sock_in[0].sin_port = htons(port); 143 sock_in[0].sin_family = AF_INET; 144 sock_in_size++; 142 145 } 143 146 } … … 155 158 #endif 156 159 close(fd); 157 } 158 159 void UdpSocket_impl::SendMessage(const char *src, size_t src_len) { 160 free(sock_in); 161 } 162 163 void UdpSocket_impl::SendMessage(const char *msg, size_t msg_len,int dst_id) { 160 164 ssize_t written; 161 165 string to_send; 162 166 163 167 if (broadcast == true) { 164 to_send = getFrameworkManager()->ObjectName() + ":" + string( src, src_len);165 src_len = to_send.size();166 src= (char *)to_send.c_str();168 to_send = getFrameworkManager()->ObjectName() + ":" + string(msg, msg_len); 169 msg_len = to_send.size(); 170 msg = (char *)to_send.c_str(); 167 171 } 168 172 169 173 #ifdef __XENO__ 170 174 // Printf("send pipe %s\n",src); 171 written = rt_pipe_write(&pipe, src, src_len, P_NORMAL);175 written = rt_pipe_write(&pipe, msg, msg_len, P_NORMAL); 172 176 173 177 if (written < 0) { 174 178 char errorMsg[256]; 175 179 self->Err("rt_pipe_write error (%s)\n", strerror_r(-written, errorMsg, sizeof(errorMsg))); 176 } else if (written != (ssize_t) src_len) {180 } else if (written != (ssize_t)msg_len) { 177 181 self->Err("rt_pipe_write error %i/%i\n", written, to_send.size()); 178 182 } 179 183 #else 180 written = 181 sendto(fd, src, src_len, 0, (struct sockaddr *)&sock_in, sizeof(sock_in)); 184 written = sendto(fd, msg, msg_len, 0, (struct sockaddr *)&(sock_in[dst_id]), sizeof(struct sockaddr_in)); 182 185 if(written==-1) { 183 186 char errorMsg[256]; 184 187 self->Err("sendto error (%s)\n",strerror_r(errno, errorMsg, sizeof(errorMsg))); 185 } else if (written != (ssize_t) src_len) {186 self->Err("sendto error %i/%i\n",written, src_len);188 } else if (written != (ssize_t)msg_len) { 189 self->Err("sendto error %i/%i\n",written,msg_len); 187 190 } 188 191 #endif … … 205 208 } 206 209 #else 207 written = sendto(fd, message.c_str(), message.size(), 0, 208 (struct sockaddr *)&sock_in, sizeof(sock_in)); 210 written = sendto(fd, message.c_str(), message.size(), 0, (struct sockaddr *)&sock_in, sizeof(struct sockaddr_in)); 209 211 if (written != (ssize_t)message.size()) { 210 212 self->Err("sendto error\n"); … … 215 217 216 218 ssize_t UdpSocket_impl::RecvMessage(char *msg, size_t msg_len, Time timeout, 217 char *src, size_t *src_len ) {219 char *src, size_t *src_len,int* src_id) { 218 220 ssize_t nb_read; 219 221 char buffer[128]; … … 221 223 nb_read = rt_pipe_read(&pipe, &buffer, sizeof(buffer), timeout); 222 224 #else 223 socklen_t sinsize = sizeof(sock_in);224 struct timeval tv;225 225 226 226 if (timeout != TIME_NONBLOCK) { 227 struct timeval tv; 227 228 int attr = fcntl(fd, F_GETFL, 0); 228 229 fcntl(fd, F_SETFL, attr & (~O_NONBLOCK)); … … 238 239 } 239 240 241 struct sockaddr_in sock_in_tmp; 240 242 if (broadcast == false) { 241 nb_read =242 recvfrom(fd, buffer, sizeof(buffer), 0, (sockaddr *)&sock_in, &sinsize);243 socklen_t sinsize = sizeof(struct sockaddr_in); 244 nb_read = recvfrom(fd, buffer, sizeof(buffer), 0, (sockaddr *)&sock_in_tmp, &sinsize); 243 245 } else { 244 246 nb_read = recvfrom(fd, buffer, sizeof(buffer), 0, NULL, NULL); 247 } 248 249 if(broadcast==false && nb_read>0) { 250 int i; 251 for(i=0;i<sock_in_size;i++) { 252 if(sock_in[i].sin_port==sock_in_tmp.sin_port) { 253 Printf("match found in former adress\n"); 254 *src_id=i; 255 break; 256 } 257 } 258 if(i==sock_in_size) { 259 Printf("no match found in former adress\n"); 260 sock_in_size++; 261 sock_in=(sockaddr_in*)realloc(sock_in,sock_in_size*sizeof(sockaddr_in)); 262 sock_in[sock_in_size-1]=sock_in_tmp; 263 *src_id=sock_in_size-1; 264 } 245 265 } 246 266 #endif … … 266 286 return -1; 267 287 } 268 } else if (nb_read - index - 1 + 1 > 269 (int)msg_len) { //+1 pour inserer un 0 270 self->Warn("insufficent msg size (%i/%i)\n", nb_read - index - 1 + 1, 271 msg_len); 288 } else if (nb_read - index - 1 + 1 > (int)msg_len) { //+1 pour inserer un 0 289 self->Warn("insufficent msg size (%i/%i)\n", nb_read - index - 1 + 1, msg_len); 272 290 return -1; 273 291 } … … 329 347 330 348 if (FD_ISSET(caller->fd, &set)) { 331 socklen_t sinsize = sizeof( caller->sock_in);349 socklen_t sinsize = sizeof(struct sockaddr_in); 332 350 if (caller->broadcast == false) { 333 351 nb_read = recvfrom(caller->fd, buffer, sizeof(buffer), 0, … … 361 379 nb_write = sendto(caller->fd, buffer, nb_read, 0, 362 380 (struct sockaddr *)&(caller->sock_in), 363 sizeof( caller->sock_in));381 sizeof(struct sockaddr_in)); 364 382 if (nb_write != nb_read) { 365 383 caller->self->Err("sendto error\n");
Note:
See TracChangeset
for help on using the changeset viewer.