Read from multiple sockets
This commit is contained in:
+24
-14
@@ -35,7 +35,8 @@ void poll_message(Client client, union Message* resp) {
|
||||
fd_set read_fds;
|
||||
FD_ZERO(&read_fds);
|
||||
FD_SET(client->sdk_conn->sockfd, &read_fds);
|
||||
int result = select(client->sdk_conn->sockfd + 1, &read_fds, NULL, NULL, NULL);
|
||||
FD_SET(client->dev_conn->sockfd, &read_fds);
|
||||
int result = select(max_fd + 1, &read_fds, NULL, NULL, NULL);
|
||||
|
||||
// Check for socket polling errors
|
||||
if(result < 0) {
|
||||
@@ -45,23 +46,32 @@ void poll_message(Client client, union Message* resp) {
|
||||
|
||||
// Skip if nothing was received yet
|
||||
// TODO: Make a static "empty" message or something
|
||||
if (result == 0) {
|
||||
if (result == 0)
|
||||
return;
|
||||
}
|
||||
|
||||
// Read a message from the socket
|
||||
int recvb = recvfrom(client->sdk_conn->sockfd, resp, sizeof(union Message), 0, (struct sockaddr*)&client->sdk_conn->remote_addr, &client->sdk_conn->addrlen);
|
||||
// Read a message from the sockets
|
||||
for(int i = 0; i < 2; i++)
|
||||
{
|
||||
|
||||
// Check for socket read errors
|
||||
if(recvb < 0) {
|
||||
perror("reading socket failed");
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
if(!FD_ISSET(client->conns[i]->sockfd, &read_fds))
|
||||
continue;
|
||||
|
||||
int recvb = recvfrom(client->sdk_conn->sockfd, resp, sizeof(union Message), 0, (struct sockaddr*)&client->sdk_conn->remote_addr, &client->sdk_conn->addrlen);
|
||||
|
||||
// Check for socket read errors
|
||||
if(recvb < 0) {
|
||||
perror("reading socket failed");
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
|
||||
// Check for message errors
|
||||
if(message_validate(resp) != MESSAGEERR_NONE) {
|
||||
perror("invalid message");
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
|
||||
return;
|
||||
|
||||
// Check for message errors
|
||||
if(message_validate(resp) != MESSAGEERR_NONE) {
|
||||
perror("invalid message");
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user