summaryrefslogtreecommitdiff
path: root/toxcore/TCP_server.c
diff options
context:
space:
mode:
authorirungentoo <irungentoo@gmail.com>2014-12-26 20:12:51 -0500
committerirungentoo <irungentoo@gmail.com>2014-12-26 20:12:51 -0500
commitc2e394c5c2a60927b543c8eb9791724edca917ff (patch)
tree917b23bb8dddaca2b7c3c47fea31c25d1ca96cff /toxcore/TCP_server.c
parentfe57a72659a8a298d8c11f7355d15815dee10195 (diff)
Fixed bug with auto tests and cleaned up the code a bit.
Diffstat (limited to 'toxcore/TCP_server.c')
-rw-r--r--toxcore/TCP_server.c34
1 files changed, 19 insertions, 15 deletions
diff --git a/toxcore/TCP_server.c b/toxcore/TCP_server.c
index c7bccab7..250d6c44 100644
--- a/toxcore/TCP_server.c
+++ b/toxcore/TCP_server.c
@@ -178,14 +178,10 @@ static int del_accepted(TCP_Server *TCP_server, int index)
178 return 0; 178 return 0;
179} 179}
180 180
181/* Read the next two bytes in TCP stream then convert them to 181/* return the amount of data in the tcp recv buffer.
182 * length (host byte order). 182 * return 0 on failure.
183 *
184 * return length on success
185 * return 0 if nothing has been read from socket.
186 * return ~0 on failure.
187 */ 183 */
188uint16_t read_TCP_length(sock_t sock) 184unsigned int TCP_socket_data_recv_buffer(sock_t sock)
189{ 185{
190#if defined(_WIN32) || defined(__WIN32__) || defined (WIN32) 186#if defined(_WIN32) || defined(__WIN32__) || defined (WIN32)
191 unsigned long count = 0; 187 unsigned long count = 0;
@@ -195,7 +191,21 @@ uint16_t read_TCP_length(sock_t sock)
195 ioctl(sock, FIONREAD, &count); 191 ioctl(sock, FIONREAD, &count);
196#endif 192#endif
197 193
198 if ((unsigned int)count >= sizeof(uint16_t)) { 194 return count;
195}
196
197/* Read the next two bytes in TCP stream then convert them to
198 * length (host byte order).
199 *
200 * return length on success
201 * return 0 if nothing has been read from socket.
202 * return ~0 on failure.
203 */
204uint16_t read_TCP_length(sock_t sock)
205{
206 unsigned int count = TCP_socket_data_recv_buffer(sock);
207
208 if (count >= sizeof(uint16_t)) {
199 uint16_t length; 209 uint16_t length;
200 int len = recv(sock, (uint8_t *)&length, sizeof(uint16_t), MSG_NOSIGNAL); 210 int len = recv(sock, (uint8_t *)&length, sizeof(uint16_t), MSG_NOSIGNAL);
201 211
@@ -223,13 +233,7 @@ uint16_t read_TCP_length(sock_t sock)
223 */ 233 */
224int read_TCP_packet(sock_t sock, uint8_t *data, uint16_t length) 234int read_TCP_packet(sock_t sock, uint8_t *data, uint16_t length)
225{ 235{
226#if defined(_WIN32) || defined(__WIN32__) || defined (WIN32) 236 unsigned int count = TCP_socket_data_recv_buffer(sock);
227 unsigned long count = 0;
228 ioctlsocket(sock, FIONREAD, &count);
229#else
230 int count = 0;
231 ioctl(sock, FIONREAD, &count);
232#endif
233 237
234 if (count >= length) { 238 if (count >= length) {
235 int len = recv(sock, data, length, MSG_NOSIGNAL); 239 int len = recv(sock, data, length, MSG_NOSIGNAL);