Merge pull request #99 from Astonex/master

Simple init_networking() error checking
This commit is contained in:
irungentoo 2013-07-24 05:26:03 -07:00
commit 57e7ed7c10
4 changed files with 26 additions and 10 deletions

View File

@ -393,14 +393,18 @@ void m_callback_userstatus(void (*function)(int, uint8_t *, uint16_t))
#define PORT 33445 #define PORT 33445
/* run this at startup */ /* run this at startup */
void initMessenger() int initMessenger()
{ {
new_keys(); new_keys();
m_set_userstatus((uint8_t*)"Online", sizeof("Online")); m_set_userstatus((uint8_t*)"Online", sizeof("Online"));
initNetCrypto(); initNetCrypto();
IP ip; IP ip;
ip.i = 0; ip.i = 0;
init_networking(ip, PORT);
if(init_networking(ip,PORT) == -1)
return -1;
return 0;
} }
static void doFriends() static void doFriends()

View File

@ -132,8 +132,10 @@ void m_callback_namechange(void (*function)(int, uint8_t *, uint16_t));
you are not responsible for freeing newstatus */ you are not responsible for freeing newstatus */
void m_callback_userstatus(void (*function)(int, uint8_t *, uint16_t)); void m_callback_userstatus(void (*function)(int, uint8_t *, uint16_t));
/* run this at startup */ /* run this at startup
void initMessenger(); returns 0 if no connection problems
returns -1 if there are problems */
int initMessenger();
/* the main loop that needs to be run at least 200 times per second */ /* the main loop that needs to be run at least 200 times per second */

View File

@ -101,8 +101,8 @@ int receivepacket(IP_Port * ip_port, uint8_t * data, uint32_t * length)
bind to ip and port bind to ip and port
ip must be in network order EX: 127.0.0.1 = (7F000001) ip must be in network order EX: 127.0.0.1 = (7F000001)
port is in host byte order (this means don't worry about it) port is in host byte order (this means don't worry about it)
returns 0 if no problems returns 0 if no problems
TODO: add something to check if there are errors */ returns -1 if there are problems */
int init_networking(IP ip ,uint16_t port) int init_networking(IP ip ,uint16_t port)
{ {
#ifdef WIN32 #ifdef WIN32
@ -119,7 +119,17 @@ int init_networking(IP ip ,uint16_t port)
/* initialize our socket */ /* initialize our socket */
sock = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP); sock = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
/* Check for socket error */
#ifdef WIN32
if (sock == INVALID_SOCKET) //MSDN recommends this
return -1;
#else
if (sock < 0)
return -1;
#endif
/* Functions to increase the size of the send and receive UDP buffers /* Functions to increase the size of the send and receive UDP buffers
NOTE: uncomment if necessary */ NOTE: uncomment if necessary */
/* /*
@ -146,7 +156,8 @@ int init_networking(IP ip ,uint16_t port)
/* Bind our socket to port PORT and address 0.0.0.0 */ /* Bind our socket to port PORT and address 0.0.0.0 */
ADDR addr = {AF_INET, htons(port), ip}; ADDR addr = {AF_INET, htons(port), ip};
bind(sock, (struct sockaddr*)&addr, sizeof(addr)); bind(sock, (struct sockaddr*)&addr, sizeof(addr));
return 0; return 0;
} }

View File

@ -121,10 +121,9 @@ int receivepacket(IP_Port * ip_port, uint8_t * data, uint32_t * length);
ip must be in network order EX: 127.0.0.1 = (7F000001) ip must be in network order EX: 127.0.0.1 = (7F000001)
port is in host byte order (this means don't worry about it) port is in host byte order (this means don't worry about it)
returns 0 if no problems returns 0 if no problems
TODO: add something to check if there are errors */ returns -1 if there were problems */
int init_networking(IP ip ,uint16_t port); int init_networking(IP ip ,uint16_t port);
/* function to cleanup networking stuff(doesn't do much right now) */ /* function to cleanup networking stuff(doesn't do much right now) */
void shutdown_networking(); void shutdown_networking();