OpenTTD

packet.h
Go to the documentation of this file.
1 /* $Id: packet.h 23590 2011-12-18 18:37:54Z rubidium $ */
2
3 /*
4 * This file is part of OpenTTD.
5 * OpenTTD is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 2.
6 * OpenTTD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
7 * See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with OpenTTD. If not, see <http://www.gnu.org/licenses/>.
8 */
9
14 #ifndef NETWORK_CORE_PACKET_H
15 #define NETWORK_CORE_PACKET_H
16
17 #include "config.h"
18 #include "core.h"
19 #include "../../string_type.h"
20
21 #ifdef ENABLE_NETWORK
22
23 typedef uint16 PacketSize;
24 typedef uint8 PacketType;
25
44 struct Packet {
46 Packet *next;
52 PacketSize size;
54 PacketSize pos;
56 byte *buffer;
57
58 private:
60 NetworkSocketHandler *cs;
61
62 public:
63 Packet(NetworkSocketHandler *cs);
64 Packet(PacketType type);
65 ~Packet();
66
67 /* Sending/writing of packets */
68 void PrepareToSend();
69
70 void Send_bool (bool data);
71 void Send_uint8 (uint8 data);
72 void Send_uint16(uint16 data);
73 void Send_uint32(uint32 data);
74 void Send_uint64(uint64 data);
75 void Send_string(const char *data);
76
77 /* Reading/receiving of packets */
78 void ReadRawPacketSize();
79 void PrepareToRead();
80
81 bool CanReadFromPacket (uint bytes_to_read);
82 bool Recv_bool ();
83 uint8 Recv_uint8 ();
84 uint16 Recv_uint16();
85 uint32 Recv_uint32();
86 uint64 Recv_uint64();
87 void Recv_string(char *buffer, size_t size, StringValidationSettings settings = SVS_REPLACE_WITH_QUESTION_MARK);
88 };
89
90 #endif /* ENABLE_NETWORK */
91
92 #endif /* NETWORK_CORE_PACKET_H */
Packet
Internal entity of a packet.
Definition: packet.h:44
Packet::Recv_uint32
uint32 Recv_uint32()
Read a 32 bits integer from the packet.
Definition: packet.cpp:250
Packet::pos
PacketSize pos
The current read/write position in the packet.
Definition: packet.h:54
Packet::PrepareToSend
void PrepareToSend()
Writes the packet size from the raw packet from packet->size.
Definition: packet.cpp:65
Packet::Send_string
void Send_string(const char *data)
Sends a string over the network.
Definition: packet.cpp:152
PacketType
uint8 PacketType
Identifier for the packet.
Definition: packet.h:24
Packet::Send_uint8
void Send_uint8(uint8 data)
Package a 8 bits integer in the packet.
Definition: packet.cpp:100
core.h
Base for all network types (UDP and TCP)
Packet::Send_uint32
void Send_uint32(uint32 data)
Package a 32 bits integer in the packet.
Definition: packet.cpp:121
Packet::CanReadFromPacket
bool CanReadFromPacket(uint bytes_to_read)
Is it safe to read from the packet, i.e.
Definition: packet.cpp:173
config.h
Configuration options of the network stuff.
StringValidationSettings
StringValidationSettings
Settings for the string validation.
Definition: string_type.h:48
Packet::PrepareToRead
void PrepareToRead()
Prepares the packet so it can be read.
Definition: packet.cpp:200
Packet::Send_uint64
void Send_uint64(uint64 data)
Package a 64 bits integer in the packet.
Definition: packet.cpp:134
Packet::buffer
byte * buffer
The buffer of this packet, of basically variable length up to SEND_MTU.
Definition: packet.h:56
Packet::Send_uint16
void Send_uint16(uint16 data)
Package a 16 bits integer in the packet.
Definition: packet.cpp:110
Packet::~Packet
~Packet()
Free the buffer of this packet.
Definition: packet.cpp:57
Packet::size
PacketSize size
The size of the whole packet for received packets.
Definition: packet.h:52
Packet::Recv_bool
bool Recv_bool()
Read a boolean from the packet.
Definition: packet.cpp:212
Packet::next
Packet * next
The next packet.
Definition: packet.h:46
Packet::cs
NetworkSocketHandler * cs
Socket we&#39;re associated with.
Definition: packet.h:60
Packet::Packet
Packet(NetworkSocketHandler *cs)
Create a packet that is used to read from a network socket.
Definition: packet.cpp:27
Packet::Recv_uint8
uint8 Recv_uint8()
Read a 8 bits integer from the packet.
Definition: packet.cpp:221
Packet::Recv_uint64
uint64 Recv_uint64()
Read a 64 bits integer from the packet.
Definition: packet.cpp:267
SVS_REPLACE_WITH_QUESTION_MARK
Replace the unknown/bad bits with question marks.
Definition: string_type.h:50
Packet::ReadRawPacketSize
void ReadRawPacketSize()
Reads the packet size from the raw packet and stores it in the packet->size.
Definition: packet.cpp:190
Packet::Recv_uint16
uint16 Recv_uint16()
Read a 16 bits integer from the packet.
Definition: packet.cpp:235
Packet::Send_bool
void Send_bool(bool data)
Package a boolean in the packet.
Definition: packet.cpp:91
PacketSize
uint16 PacketSize
Size of the whole packet.
Definition: packet.h:23
NetworkSocketHandler
SocketHandler for all network sockets in OpenTTD.
Definition: core.h:45
Packet::Recv_string
void Recv_string(char *buffer, size_t size, StringValidationSettings settings=SVS_REPLACE_WITH_QUESTION_MARK)
Reads a string till it finds a &#39;\0&#39; in the stream.
Definition: packet.cpp:290