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 {
56  byte *buffer;
57 
58 private:
61 
62 public:
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 */
Internal entity of a packet.
Definition: packet.h:44
uint32 Recv_uint32()
Read a 32 bits integer from the packet.
Definition: packet.cpp:250
PacketSize pos
The current read/write position in the packet.
Definition: packet.h:54
void PrepareToSend()
Writes the packet size from the raw packet from packet->size.
Definition: packet.cpp:65
void Send_string(const char *data)
Sends a string over the network.
Definition: packet.cpp:152
uint8 PacketType
Identifier for the packet.
Definition: packet.h:24
void Send_uint8(uint8 data)
Package a 8 bits integer in the packet.
Definition: packet.cpp:100
Base for all network types (UDP and TCP)
void Send_uint32(uint32 data)
Package a 32 bits integer in the packet.
Definition: packet.cpp:121
bool CanReadFromPacket(uint bytes_to_read)
Is it safe to read from the packet, i.e.
Definition: packet.cpp:173
Configuration options of the network stuff.
StringValidationSettings
Settings for the string validation.
Definition: string_type.h:48
void PrepareToRead()
Prepares the packet so it can be read.
Definition: packet.cpp:200
void Send_uint64(uint64 data)
Package a 64 bits integer in the packet.
Definition: packet.cpp:134
byte * buffer
The buffer of this packet, of basically variable length up to SEND_MTU.
Definition: packet.h:56
void Send_uint16(uint16 data)
Package a 16 bits integer in the packet.
Definition: packet.cpp:110
~Packet()
Free the buffer of this packet.
Definition: packet.cpp:57
PacketSize size
The size of the whole packet for received packets.
Definition: packet.h:52
bool Recv_bool()
Read a boolean from the packet.
Definition: packet.cpp:212
Packet * next
The next packet.
Definition: packet.h:46
NetworkSocketHandler * cs
Socket we&#39;re associated with.
Definition: packet.h:60
Packet(NetworkSocketHandler *cs)
Create a packet that is used to read from a network socket.
Definition: packet.cpp:27
uint8 Recv_uint8()
Read a 8 bits integer from the packet.
Definition: packet.cpp:221
uint64 Recv_uint64()
Read a 64 bits integer from the packet.
Definition: packet.cpp:267
Replace the unknown/bad bits with question marks.
Definition: string_type.h:50
void ReadRawPacketSize()
Reads the packet size from the raw packet and stores it in the packet->size.
Definition: packet.cpp:190
uint16 Recv_uint16()
Read a 16 bits integer from the packet.
Definition: packet.cpp:235
void Send_bool(bool data)
Package a boolean in the packet.
Definition: packet.cpp:91
uint16 PacketSize
Size of the whole packet.
Definition: packet.h:23
SocketHandler for all network sockets in OpenTTD.
Definition: core.h:45
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