|
@@ -9,12 +9,13 @@
|
|
|
#include "table/strings.h"
|
|
|
#include "../../functions.h"
|
|
|
|
|
|
#include "../network_data.h"
|
|
|
#include "packet.h"
|
|
|
#include "tcp.h"
|
|
|
#include "../../helpers.hpp"
|
|
|
|
|
|
/**
|
|
|
* @file tcp.c Basic functions to receive and send TCP packets.
|
|
|
*/
|
|
|
|
|
|
/**
|
|
@@ -96,13 +97,13 @@ bool NetworkSend_Packets(NetworkClientSt
|
|
|
/* We can not write to this socket!! */
|
|
|
if (!cs->writable) return false;
|
|
|
if (cs->socket == INVALID_SOCKET) return false;
|
|
|
|
|
|
p = cs->packet_queue;
|
|
|
while (p != NULL) {
|
|
|
res = send(cs->socket, p->buffer + p->pos, p->size - p->pos, 0);
|
|
|
res = send(cs->socket, (const char*)p->buffer + p->pos, p->size - p->pos, 0);
|
|
|
if (res == -1) {
|
|
|
int err = GET_LAST_ERROR();
|
|
|
if (err != EWOULDBLOCK) {
|
|
|
/* Something went wrong.. close client! */
|
|
|
DEBUG(net, 0, "send failed with error %d", err);
|
|
|
CloseConnection(cs);
|
|
@@ -145,26 +146,26 @@ Packet *NetworkRecv_Packet(NetworkClient
|
|
|
|
|
|
*status = NETWORK_RECV_STATUS_OKAY;
|
|
|
|
|
|
if (cs->socket == INVALID_SOCKET) return NULL;
|
|
|
|
|
|
if (cs->packet_recv == NULL) {
|
|
|
cs->packet_recv = malloc(sizeof(Packet));
|
|
|
MallocT(&cs->packet_recv, 1);
|
|
|
if (cs->packet_recv == NULL) error("Failed to allocate packet");
|
|
|
/* Set pos to zero! */
|
|
|
cs->packet_recv->pos = 0;
|
|
|
cs->packet_recv->size = 0; // Can be ommited, just for safety reasons
|
|
|
}
|
|
|
|
|
|
p = cs->packet_recv;
|
|
|
|
|
|
/* Read packet size */
|
|
|
if (p->pos < sizeof(PacketSize)) {
|
|
|
while (p->pos < sizeof(PacketSize)) {
|
|
|
/* Read the size of the packet */
|
|
|
res = recv(cs->socket, p->buffer + p->pos, sizeof(PacketSize) - p->pos, 0);
|
|
|
res = recv(cs->socket, (char*)p->buffer + p->pos, sizeof(PacketSize) - p->pos, 0);
|
|
|
if (res == -1) {
|
|
|
int err = GET_LAST_ERROR();
|
|
|
if (err != EWOULDBLOCK) {
|
|
|
/* Something went wrong... (104 is connection reset by peer) */
|
|
|
if (err != 104) DEBUG(net, 0, "recv failed with error %d", err);
|
|
|
*status = CloseConnection(cs);
|
|
@@ -188,13 +189,13 @@ Packet *NetworkRecv_Packet(NetworkClient
|
|
|
return NULL;
|
|
|
}
|
|
|
}
|
|
|
|
|
|
/* Read rest of packet */
|
|
|
while (p->pos < p->size) {
|
|
|
res = recv(cs->socket, p->buffer + p->pos, p->size - p->pos, 0);
|
|
|
res = recv(cs->socket, (char*)p->buffer + p->pos, p->size - p->pos, 0);
|
|
|
if (res == -1) {
|
|
|
int err = GET_LAST_ERROR();
|
|
|
if (err != EWOULDBLOCK) {
|
|
|
/* Something went wrong... (104 is connection reset by peer) */
|
|
|
if (err != 104) DEBUG(net, 0, "recv failed with error %d", err);
|
|
|
*status = CloseConnection(cs);
|