diff options
author | irungentoo <irungentoo@gmail.com> | 2013-08-20 17:07:55 -0700 |
---|---|---|
committer | irungentoo <irungentoo@gmail.com> | 2013-08-20 17:07:55 -0700 |
commit | c12853275c03ca6153c9424f29220bc5b8903875 (patch) | |
tree | d8191c6a088bcaf8c908fbb5e81a22825712330f /core/network.c | |
parent | 617b2c8ba59970a67178c602d5348d036140d559 (diff) | |
parent | 128223d9d1c70afe0adb4cfe0cfda39204379c3a (diff) |
Merge pull request #506 from irungentoo/refactor
Refactored Everything.
Diffstat (limited to 'core/network.c')
-rw-r--r-- | core/network.c | 92 |
1 files changed, 57 insertions, 35 deletions
diff --git a/core/network.c b/core/network.c index 1977ce38..849c7e2a 100644 --- a/core/network.c +++ b/core/network.c | |||
@@ -56,12 +56,9 @@ uint32_t random_int(void) | |||
56 | #endif | 56 | #endif |
57 | } | 57 | } |
58 | 58 | ||
59 | /* our UDP socket, a global variable. */ | ||
60 | static int sock; | ||
61 | |||
62 | /* Basic network functions: | 59 | /* Basic network functions: |
63 | Function to send packet(data) of length length to ip_port */ | 60 | Function to send packet(data) of length length to ip_port */ |
64 | int sendpacket(IP_Port ip_port, uint8_t *data, uint32_t length) | 61 | int sendpacket(int sock, IP_Port ip_port, uint8_t *data, uint32_t length) |
65 | { | 62 | { |
66 | ADDR addr = {AF_INET, ip_port.port, ip_port.ip}; | 63 | ADDR addr = {AF_INET, ip_port.port, ip_port.ip}; |
67 | return sendto(sock, (char *) data, length, 0, (struct sockaddr *)&addr, sizeof(addr)); | 64 | return sendto(sock, (char *) data, length, 0, (struct sockaddr *)&addr, sizeof(addr)); |
@@ -71,7 +68,7 @@ int sendpacket(IP_Port ip_port, uint8_t *data, uint32_t length) | |||
71 | the packet data into data | 68 | the packet data into data |
72 | the packet length into length. | 69 | the packet length into length. |
73 | dump all empty packets. */ | 70 | dump all empty packets. */ |
74 | static int receivepacket(IP_Port *ip_port, uint8_t *data, uint32_t *length) | 71 | static int receivepacket(int sock, IP_Port *ip_port, uint8_t *data, uint32_t *length) |
75 | { | 72 | { |
76 | ADDR addr; | 73 | ADDR addr; |
77 | #ifdef WIN32 | 74 | #ifdef WIN32 |
@@ -89,36 +86,33 @@ static int receivepacket(IP_Port *ip_port, uint8_t *data, uint32_t *length) | |||
89 | return 0; | 86 | return 0; |
90 | } | 87 | } |
91 | 88 | ||
92 | static packet_handler_callback packethandlers[256] = {0}; | 89 | void networking_registerhandler(Networking_Core *net, uint8_t byte, packet_handler_callback cb, void *object) |
93 | |||
94 | void networking_registerhandler(uint8_t byte, packet_handler_callback cb) | ||
95 | { | 90 | { |
96 | packethandlers[byte] = cb; | 91 | net->packethandlers[byte].function = cb; |
92 | net->packethandlers[byte].object = object; | ||
97 | } | 93 | } |
98 | 94 | ||
99 | void networking_poll() | 95 | void networking_poll(Networking_Core *net) |
100 | { | 96 | { |
101 | IP_Port ip_port; | 97 | IP_Port ip_port; |
102 | uint8_t data[MAX_UDP_PACKET_SIZE]; | 98 | uint8_t data[MAX_UDP_PACKET_SIZE]; |
103 | uint32_t length; | 99 | uint32_t length; |
104 | 100 | ||
105 | while (receivepacket(&ip_port, data, &length) != -1) { | 101 | while (receivepacket(net->sock, &ip_port, data, &length) != -1) { |
106 | if (length < 1) continue; | 102 | if (length < 1) continue; |
107 | 103 | ||
108 | if (!packethandlers[data[0]]) continue; | 104 | if (!(net->packethandlers[data[0]].function)) continue; |
109 | 105 | ||
110 | packethandlers[data[0]](ip_port, data, length); | 106 | net->packethandlers[data[0]].function(net->packethandlers[data[0]].object, ip_port, data, length); |
111 | } | 107 | } |
112 | } | 108 | } |
113 | 109 | ||
114 | /* initialize networking | 110 | uint8_t at_startup_ran; |
115 | bind to ip and port | 111 | static void at_startup(void) |
116 | ip must be in network order EX: 127.0.0.1 = (7F000001) | ||
117 | port is in host byte order (this means don't worry about it) | ||
118 | returns 0 if no problems | ||
119 | returns -1 if there are problems */ | ||
120 | int init_networking(IP ip, uint16_t port) | ||
121 | { | 112 | { |
113 | if (at_startup_ran != 0) | ||
114 | return; | ||
115 | |||
122 | #ifdef WIN32 | 116 | #ifdef WIN32 |
123 | WSADATA wsaData; | 117 | WSADATA wsaData; |
124 | 118 | ||
@@ -129,20 +123,49 @@ int init_networking(IP ip, uint16_t port) | |||
129 | srandom((uint32_t)current_time()); | 123 | srandom((uint32_t)current_time()); |
130 | #endif | 124 | #endif |
131 | srand((uint32_t)current_time()); | 125 | srand((uint32_t)current_time()); |
126 | at_startup_ran = 1; | ||
127 | } | ||
128 | |||
129 | /* TODO: put this somewhere | ||
130 | static void at_shutdown(void) | ||
131 | { | ||
132 | #ifdef WIN32 | ||
133 | WSACleanup(); | ||
134 | #endif | ||
135 | } | ||
136 | */ | ||
132 | 137 | ||
138 | /* initialize networking | ||
139 | bind to ip and port | ||
140 | ip must be in network order EX: 127.0.0.1 = (7F000001) | ||
141 | port is in host byte order (this means don't worry about it) | ||
142 | returns Networking_Core object if no problems | ||
143 | returns NULL if there are problems */ | ||
144 | Networking_Core *new_networking(IP ip, uint16_t port) | ||
145 | { | ||
146 | at_startup(); | ||
133 | /* initialize our socket */ | 147 | /* initialize our socket */ |
134 | sock = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP); | 148 | Networking_Core *temp = calloc(1, sizeof(Networking_Core)); |
149 | |||
150 | if (temp == NULL) | ||
151 | return NULL; | ||
152 | |||
153 | temp->sock = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP); | ||
135 | 154 | ||
136 | /* Check for socket error */ | 155 | /* Check for socket error */ |
137 | #ifdef WIN32 | 156 | #ifdef WIN32 |
138 | 157 | ||
139 | if (sock == INVALID_SOCKET) /* MSDN recommends this */ | 158 | if (temp->sock == INVALID_SOCKET) { /* MSDN recommends this */ |
140 | return -1; | 159 | free(temp); |
160 | return NULL; | ||
161 | } | ||
141 | 162 | ||
142 | #else | 163 | #else |
143 | 164 | ||
144 | if (sock < 0) | 165 | if (temp->sock < 0) { |
145 | return -1; | 166 | free(temp); |
167 | return NULL; | ||
168 | } | ||
146 | 169 | ||
147 | #endif | 170 | #endif |
148 | 171 | ||
@@ -161,34 +184,33 @@ int init_networking(IP ip, uint16_t port) | |||
161 | 184 | ||
162 | /* Enable broadcast on socket */ | 185 | /* Enable broadcast on socket */ |
163 | int broadcast = 1; | 186 | int broadcast = 1; |
164 | setsockopt(sock, SOL_SOCKET, SO_BROADCAST, (char *)&broadcast, sizeof(broadcast)); | 187 | setsockopt(temp->sock, SOL_SOCKET, SO_BROADCAST, (char *)&broadcast, sizeof(broadcast)); |
165 | 188 | ||
166 | /* Set socket nonblocking */ | 189 | /* Set socket nonblocking */ |
167 | #ifdef WIN32 | 190 | #ifdef WIN32 |
168 | /* I think this works for windows */ | 191 | /* I think this works for windows */ |
169 | u_long mode = 1; | 192 | u_long mode = 1; |
170 | /* ioctl(sock, FIONBIO, &mode); */ | 193 | /* ioctl(sock, FIONBIO, &mode); */ |
171 | ioctlsocket(sock, FIONBIO, &mode); | 194 | ioctlsocket(temp->sock, FIONBIO, &mode); |
172 | #else | 195 | #else |
173 | fcntl(sock, F_SETFL, O_NONBLOCK, 1); | 196 | fcntl(temp->sock, F_SETFL, O_NONBLOCK, 1); |
174 | #endif | 197 | #endif |
175 | 198 | ||
176 | /* Bind our socket to port PORT and address 0.0.0.0 */ | 199 | /* Bind our socket to port PORT and address 0.0.0.0 */ |
177 | ADDR addr = {AF_INET, htons(port), ip}; | 200 | ADDR addr = {AF_INET, htons(port), ip}; |
178 | bind(sock, (struct sockaddr *)&addr, sizeof(addr)); | 201 | bind(temp->sock, (struct sockaddr *)&addr, sizeof(addr)); |
179 | 202 | return temp; | |
180 | return 0; | ||
181 | } | 203 | } |
182 | 204 | ||
183 | /* function to cleanup networking stuff */ | 205 | /* function to cleanup networking stuff */ |
184 | void shutdown_networking(void) | 206 | void kill_networking(Networking_Core *net) |
185 | { | 207 | { |
186 | #ifdef WIN32 | 208 | #ifdef WIN32 |
187 | closesocket(sock); | 209 | closesocket(net->sock); |
188 | WSACleanup(); | ||
189 | #else | 210 | #else |
190 | close(sock); | 211 | close(net->sock); |
191 | #endif | 212 | #endif |
213 | free(net); | ||
192 | return; | 214 | return; |
193 | } | 215 | } |
194 | 216 | ||