equal
deleted
inserted
replaced
10 #include "../../functions.h" |
10 #include "../../functions.h" |
11 |
11 |
12 #include "../network_data.h" |
12 #include "../network_data.h" |
13 #include "packet.h" |
13 #include "packet.h" |
14 #include "tcp.h" |
14 #include "tcp.h" |
|
15 #include "../../helpers.hpp" |
15 |
16 |
16 /** |
17 /** |
17 * @file tcp.c Basic functions to receive and send TCP packets. |
18 * @file tcp.c Basic functions to receive and send TCP packets. |
18 */ |
19 */ |
19 |
20 |
97 if (!cs->writable) return false; |
98 if (!cs->writable) return false; |
98 if (cs->socket == INVALID_SOCKET) return false; |
99 if (cs->socket == INVALID_SOCKET) return false; |
99 |
100 |
100 p = cs->packet_queue; |
101 p = cs->packet_queue; |
101 while (p != NULL) { |
102 while (p != NULL) { |
102 res = send(cs->socket, p->buffer + p->pos, p->size - p->pos, 0); |
103 res = send(cs->socket, (const char*)p->buffer + p->pos, p->size - p->pos, 0); |
103 if (res == -1) { |
104 if (res == -1) { |
104 int err = GET_LAST_ERROR(); |
105 int err = GET_LAST_ERROR(); |
105 if (err != EWOULDBLOCK) { |
106 if (err != EWOULDBLOCK) { |
106 /* Something went wrong.. close client! */ |
107 /* Something went wrong.. close client! */ |
107 DEBUG(net, 0, "send failed with error %d", err); |
108 DEBUG(net, 0, "send failed with error %d", err); |
146 *status = NETWORK_RECV_STATUS_OKAY; |
147 *status = NETWORK_RECV_STATUS_OKAY; |
147 |
148 |
148 if (cs->socket == INVALID_SOCKET) return NULL; |
149 if (cs->socket == INVALID_SOCKET) return NULL; |
149 |
150 |
150 if (cs->packet_recv == NULL) { |
151 if (cs->packet_recv == NULL) { |
151 cs->packet_recv = malloc(sizeof(Packet)); |
152 MallocT(&cs->packet_recv, 1); |
152 if (cs->packet_recv == NULL) error("Failed to allocate packet"); |
153 if (cs->packet_recv == NULL) error("Failed to allocate packet"); |
153 /* Set pos to zero! */ |
154 /* Set pos to zero! */ |
154 cs->packet_recv->pos = 0; |
155 cs->packet_recv->pos = 0; |
155 cs->packet_recv->size = 0; // Can be ommited, just for safety reasons |
156 cs->packet_recv->size = 0; // Can be ommited, just for safety reasons |
156 } |
157 } |
159 |
160 |
160 /* Read packet size */ |
161 /* Read packet size */ |
161 if (p->pos < sizeof(PacketSize)) { |
162 if (p->pos < sizeof(PacketSize)) { |
162 while (p->pos < sizeof(PacketSize)) { |
163 while (p->pos < sizeof(PacketSize)) { |
163 /* Read the size of the packet */ |
164 /* Read the size of the packet */ |
164 res = recv(cs->socket, p->buffer + p->pos, sizeof(PacketSize) - p->pos, 0); |
165 res = recv(cs->socket, (char*)p->buffer + p->pos, sizeof(PacketSize) - p->pos, 0); |
165 if (res == -1) { |
166 if (res == -1) { |
166 int err = GET_LAST_ERROR(); |
167 int err = GET_LAST_ERROR(); |
167 if (err != EWOULDBLOCK) { |
168 if (err != EWOULDBLOCK) { |
168 /* Something went wrong... (104 is connection reset by peer) */ |
169 /* Something went wrong... (104 is connection reset by peer) */ |
169 if (err != 104) DEBUG(net, 0, "recv failed with error %d", err); |
170 if (err != 104) DEBUG(net, 0, "recv failed with error %d", err); |
189 } |
190 } |
190 } |
191 } |
191 |
192 |
192 /* Read rest of packet */ |
193 /* Read rest of packet */ |
193 while (p->pos < p->size) { |
194 while (p->pos < p->size) { |
194 res = recv(cs->socket, p->buffer + p->pos, p->size - p->pos, 0); |
195 res = recv(cs->socket, (char*)p->buffer + p->pos, p->size - p->pos, 0); |
195 if (res == -1) { |
196 if (res == -1) { |
196 int err = GET_LAST_ERROR(); |
197 int err = GET_LAST_ERROR(); |
197 if (err != EWOULDBLOCK) { |
198 if (err != EWOULDBLOCK) { |
198 /* Something went wrong... (104 is connection reset by peer) */ |
199 /* Something went wrong... (104 is connection reset by peer) */ |
199 if (err != 104) DEBUG(net, 0, "recv failed with error %d", err); |
200 if (err != 104) DEBUG(net, 0, "recv failed with error %d", err); |