OpenTTD
packet.h
Go to the documentation of this file.
1 /* $Id$ */
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 typedef uint16 PacketSize;
22 typedef uint8 PacketType;
23 
42 struct Packet {
54  byte *buffer;
55 
56 private:
59 
60 public:
62  Packet(PacketType type);
63  ~Packet();
64 
65  /* Sending/writing of packets */
66  void PrepareToSend();
67 
68  void Send_bool (bool data);
69  void Send_uint8 (uint8 data);
70  void Send_uint16(uint16 data);
71  void Send_uint32(uint32 data);
72  void Send_uint64(uint64 data);
73  void Send_string(const char *data);
74 
75  /* Reading/receiving of packets */
76  void ReadRawPacketSize();
77  void PrepareToRead();
78 
79  bool CanReadFromPacket (uint bytes_to_read);
80  bool Recv_bool ();
81  uint8 Recv_uint8 ();
82  uint16 Recv_uint16();
83  uint32 Recv_uint32();
84  uint64 Recv_uint64();
86 };
87 
88 #endif /* NETWORK_CORE_PACKET_H */
Internal entity of a packet.
Definition: packet.h:42
uint32 Recv_uint32()
Read a 32 bits integer from the packet.
Definition: packet.cpp:248
PacketSize pos
The current read/write position in the packet.
Definition: packet.h:52
void PrepareToSend()
Writes the packet size from the raw packet from packet->size.
Definition: packet.cpp:63
void Send_string(const char *data)
Sends a string over the network.
Definition: packet.cpp:150
uint8 PacketType
Identifier for the packet.
Definition: packet.h:22
fluid_settings_t * settings
FluidSynth settings handle.
Definition: fluidsynth.cpp:22
void Send_uint8(uint8 data)
Package a 8 bits integer in the packet.
Definition: packet.cpp:98
Base for all network types (UDP and TCP)
void Send_uint32(uint32 data)
Package a 32 bits integer in the packet.
Definition: packet.cpp:119
bool CanReadFromPacket(uint bytes_to_read)
Is it safe to read from the packet, i.e.
Definition: packet.cpp:171
Configuration options of the network stuff.
StringValidationSettings
Settings for the string validation.
Definition: string_type.h:50
void PrepareToRead()
Prepares the packet so it can be read.
Definition: packet.cpp:198
void Send_uint64(uint64 data)
Package a 64 bits integer in the packet.
Definition: packet.cpp:132
byte * buffer
The buffer of this packet, of basically variable length up to SEND_MTU.
Definition: packet.h:54
void Send_uint16(uint16 data)
Package a 16 bits integer in the packet.
Definition: packet.cpp:108
~Packet()
Free the buffer of this packet.
Definition: packet.cpp:55
PacketSize size
The size of the whole packet for received packets.
Definition: packet.h:50
bool Recv_bool()
Read a boolean from the packet.
Definition: packet.cpp:210
Packet * next
The next packet.
Definition: packet.h:44
NetworkSocketHandler * cs
Socket we&#39;re associated with.
Definition: packet.h:58
Packet(NetworkSocketHandler *cs)
Create a packet that is used to read from a network socket.
Definition: packet.cpp:25
uint8 Recv_uint8()
Read a 8 bits integer from the packet.
Definition: packet.cpp:219
uint64 Recv_uint64()
Read a 64 bits integer from the packet.
Definition: packet.cpp:265
Replace the unknown/bad bits with question marks.
Definition: string_type.h:52
void ReadRawPacketSize()
Reads the packet size from the raw packet and stores it in the packet->size.
Definition: packet.cpp:188
uint16 Recv_uint16()
Read a 16 bits integer from the packet.
Definition: packet.cpp:233
void Send_bool(bool data)
Package a boolean in the packet.
Definition: packet.cpp:89
uint16 PacketSize
Size of the whole packet.
Definition: packet.h:21
SocketHandler for all network sockets in OpenTTD.
Definition: core.h:43
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:288