-
-
Save roxlu/9dec2fe60f17d8748ea8 to your computer and use it in GitHub Desktop.
libuv udp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include "rxs_network.h" | |
int rxs_network_init(rxs_network* net, const char* ip, int port) { | |
int r; | |
struct sockaddr_in addr; | |
if (!net) { return -1; } | |
/* get default loop */ | |
net->loop = uv_default_loop(); | |
if (!net->loop) { | |
printf("Error: cannot get default loop in networking.\n"); | |
return -2; | |
} | |
/* init sock */ | |
r = uv_udp_init(net->loop, &net->send_sock); | |
if (r != 0) { | |
printf("Error: cannot initialize uv_udp_t in networking.\n"); | |
return -3; | |
} | |
/* create ip4 addr */ | |
r = uv_ip4_addr(ip, port, &addr); | |
if (r != 0) { | |
printf("Error: cannot create addr struct for networking: %s\n", uv_strerror(r)); | |
return -4; | |
} | |
/* bind udp sender */ | |
r = uv_udp_bind(&net->send_sock, (const struct sockaddr*)&addr, 0); | |
if (r != 0) { | |
printf("Error: cannot bind udp: %s\n", uv_strerror(r)); | |
return -5; | |
} | |
return 0; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#ifndef RXS_NETWORK_H | |
#define RXS_NETWORK_H | |
#include <uv.h> | |
typedef struct rxs_network rxs_network; | |
struct rxs_network { | |
uv_udp_t send_sock; | |
uv_loop_t* loop; | |
}; | |
int rxs_network_init(rxs_network* net, const char* ip, int port); | |
#endif |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment