diff options
author | rubidium <rubidium@openttd.org> | 2007-02-01 22:30:35 +0000 |
---|---|---|
committer | rubidium <rubidium@openttd.org> | 2007-02-01 22:30:35 +0000 |
commit | 99f860e68615b011b959ac9edb45660a0396dc1b (patch) | |
tree | d1732ff5833da9103bce694429be7bf8317b2f50 /src/network/core | |
parent | 15980fc023c9fffa1af1ab71a446d0154d7962ca (diff) | |
download | openttd-99f860e68615b011b959ac9edb45660a0396dc1b.tar.xz |
(svn r8521) -Codechange: initial step in converting Packet to a class; make and use constructors and functions related to the reading/saving the packet size.
Diffstat (limited to 'src/network/core')
-rw-r--r-- | src/network/core/packet.cpp | 67 | ||||
-rw-r--r-- | src/network/core/packet.h | 15 | ||||
-rw-r--r-- | src/network/core/tcp.cpp | 20 | ||||
-rw-r--r-- | src/network/core/udp.cpp | 10 |
4 files changed, 73 insertions, 39 deletions
diff --git a/src/network/core/packet.cpp b/src/network/core/packet.cpp index a1f50e15e..653566e32 100644 --- a/src/network/core/packet.cpp +++ b/src/network/core/packet.cpp @@ -17,34 +17,60 @@ /* Do not want to include functions.h and all required headers */ extern void NORETURN CDECL error(const char *str, ...); +/** + * Create a packet that is used to read from a network socket + * @param cs the socket handler associated with the socket we are reading from + */ +Packet::Packet(NetworkSocketHandler *cs) +{ + assert(cs != NULL); + + this->cs = cs; + this->next = NULL; + this->pos = 0; // We start reading from here + this->size = 0; +} + +/** + * Creates a packet to send + * @param type of the packet to send + */ +Packet::Packet(PacketType type) +{ + this->cs = NULL; + this->next = NULL; + + /* Skip the size so we can write that in before sending the packet */ + this->pos = 0; + this->size = sizeof(PacketSize); + this->buffer[this->size++] = type; +} /** * Create a packet for sending * @param type the of packet * @return the newly created packet */ -Packet *NetworkSend_Init(const PacketType type) +Packet *NetworkSend_Init(PacketType type) { - Packet *packet = MallocT<Packet>(1); + Packet *packet = new Packet(type); /* An error is inplace here, because it simply means we ran out of memory. */ if (packet == NULL) error("Failed to allocate Packet"); - /* Skip the size so we can write that in before sending the packet */ - packet->size = sizeof(packet->size); - packet->buffer[packet->size++] = type; - packet->pos = 0; - return packet; } /** * Writes the packet size from the raw packet from packet->size - * @param packet the packet to write the size of */ -void NetworkSend_FillPacketSize(Packet *packet) +void Packet::PrepareToSend() { - packet->buffer[0] = GB(packet->size, 0, 8); - packet->buffer[1] = GB(packet->size, 8, 8); + assert(this->cs == NULL && this->next == NULL); + + this->buffer[0] = GB(this->size, 0, 8); + this->buffer[1] = GB(this->size, 8, 8); + + this->pos = 0; // We start reading from here } /** @@ -129,12 +155,23 @@ static inline bool CanReadFromPacket(NetworkSocketHandler *cs, const Packet *pac /** * Reads the packet size from the raw packet and stores it in the packet->size - * @param packet the packet to read the size of */ -void NetworkRecv_ReadPacketSize(Packet *packet) +void Packet::ReadRawPacketSize() { - packet->size = (uint16)packet->buffer[0]; - packet->size += (uint16)packet->buffer[1] << 8; + assert(this->cs != NULL && this->next == NULL); + this->size = (PacketSize)this->buffer[0]; + this->size += (PacketSize)this->buffer[1] << 8; +} + +/** + * Prepares the packet so it can be read + */ +void Packet::PrepareToRead() +{ + this->ReadRawPacketSize(); + + /* Put the position on the right place */ + this->pos = sizeof(PacketSize); } uint8 NetworkRecv_uint8(NetworkSocketHandler *cs, Packet *packet) diff --git a/src/network/core/packet.h b/src/network/core/packet.h index 641fd4ad2..8b0607485 100644 --- a/src/network/core/packet.h +++ b/src/network/core/packet.h @@ -35,18 +35,27 @@ struct Packet { PacketSize pos; /** The buffer of this packet */ byte buffer[SEND_MTU]; +private: + NetworkSocketHandler *cs; + +public: + Packet(NetworkSocketHandler *cs); + Packet(PacketType type); + + void PrepareToSend(); + + void ReadRawPacketSize(); + void PrepareToRead(); }; -Packet *NetworkSend_Init(const PacketType type); -void NetworkSend_FillPacketSize(Packet *packet); +Packet *NetworkSend_Init(PacketType type); void NetworkSend_uint8 (Packet *packet, uint8 data); void NetworkSend_uint16(Packet *packet, uint16 data); void NetworkSend_uint32(Packet *packet, uint32 data); void NetworkSend_uint64(Packet *packet, uint64 data); void NetworkSend_string(Packet *packet, const char* data); -void NetworkRecv_ReadPacketSize(Packet *packet); uint8 NetworkRecv_uint8 (NetworkSocketHandler *cs, Packet *packet); uint16 NetworkRecv_uint16(NetworkSocketHandler *cs, Packet *packet); uint32 NetworkRecv_uint32(NetworkSocketHandler *cs, Packet *packet); diff --git a/src/network/core/tcp.cpp b/src/network/core/tcp.cpp index 8a7da433d..c79f5f14c 100644 --- a/src/network/core/tcp.cpp +++ b/src/network/core/tcp.cpp @@ -74,10 +74,7 @@ void NetworkSend_Packet(Packet *packet, NetworkTCPSocketHandler *cs) Packet *p; assert(packet != NULL); - packet->pos = 0; - packet->next = NULL; - - NetworkSend_FillPacketSize(packet); + packet->PrepareToSend(); /* Locate last packet buffered for the client */ p = cs->packet_queue; @@ -133,7 +130,7 @@ bool NetworkSend_Packets(NetworkTCPSocketHandler *cs) if (p->pos == p->size) { /* Go to the next packet */ cs->packet_queue = p->next; - free(p); + delete p; p = cs->packet_queue; } else { return true; @@ -159,11 +156,8 @@ Packet *NetworkRecv_Packet(NetworkTCPSocketHandler *cs, NetworkRecvStatus *statu if (!cs->IsConnected()) return NULL; if (cs->packet_recv == NULL) { - cs->packet_recv = MallocT<Packet>(1); + cs->packet_recv = new Packet(cs); 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; @@ -192,7 +186,8 @@ Packet *NetworkRecv_Packet(NetworkTCPSocketHandler *cs, NetworkRecvStatus *statu p->pos += res; } - NetworkRecv_ReadPacketSize(p); + /* Read the packet size from the received packet */ + p->ReadRawPacketSize(); if (p->size > SEND_MTU) { *status = cs->CloseConnection(); @@ -223,13 +218,10 @@ Packet *NetworkRecv_Packet(NetworkTCPSocketHandler *cs, NetworkRecvStatus *statu p->pos += res; } - /* We have a complete packet, return it! */ - p->pos = 2; - p->next = NULL; // Should not be needed, but who knows... - /* Prepare for receiving a new packet */ cs->packet_recv = NULL; + p->PrepareToRead(); return p; } diff --git a/src/network/core/udp.cpp b/src/network/core/udp.cpp index 2c975215b..865b2ad3a 100644 --- a/src/network/core/udp.cpp +++ b/src/network/core/udp.cpp @@ -92,7 +92,7 @@ void NetworkUDPSocketHandler::SendPacket(Packet *p, const struct sockaddr_in *re { int res; - NetworkSend_FillPacketSize(p); + p->PrepareToSend(); /* Send the buffer */ res = sendto(this->sock, (const char*)p->buffer, p->size, 0, (struct sockaddr *)recv, sizeof(*recv)); @@ -109,7 +109,7 @@ void NetworkUDPSocketHandler::ReceivePackets() struct sockaddr_in client_addr; socklen_t client_len; int nbytes; - Packet p; + Packet p(this); int packet_len; if (!this->IsConnected()) return; @@ -122,7 +122,7 @@ void NetworkUDPSocketHandler::ReceivePackets() /* We got some bytes for the base header of the packet. */ if (nbytes > 2) { - NetworkRecv_ReadPacketSize(&p); + p.PrepareToRead(); /* If the size does not match the packet must be corrupted. * Otherwise it will be marked as corrupted later on. */ @@ -133,10 +133,6 @@ void NetworkUDPSocketHandler::ReceivePackets() return; } - /* Put the position on the right place */ - p.pos = 2; - p.next = NULL; - /* Handle the packet */ this->HandleUDPPacket(&p, &client_addr); } |