2007-01-02 17:34:03 +00:00
|
|
|
/* $Id$ */
|
|
|
|
|
2009-08-21 20:21:05 +00:00
|
|
|
/*
|
|
|
|
* This file is part of OpenTTD.
|
|
|
|
* 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.
|
|
|
|
* 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.
|
|
|
|
* 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/>.
|
|
|
|
*/
|
|
|
|
|
2007-02-23 08:37:33 +00:00
|
|
|
/**
|
|
|
|
* @file packet.h Basic functions to create, fill and read packets.
|
|
|
|
*/
|
|
|
|
|
2007-01-02 17:34:03 +00:00
|
|
|
#ifndef NETWORK_CORE_PACKET_H
|
|
|
|
#define NETWORK_CORE_PACKET_H
|
|
|
|
|
|
|
|
#ifdef ENABLE_NETWORK
|
|
|
|
|
2007-01-04 21:21:14 +00:00
|
|
|
#include "config.h"
|
2007-01-12 20:19:49 +00:00
|
|
|
#include "core.h"
|
2007-01-04 21:21:14 +00:00
|
|
|
|
2007-01-02 17:34:03 +00:00
|
|
|
typedef uint16 PacketSize; ///< Size of the whole packet.
|
|
|
|
typedef uint8 PacketType; ///< Identifier for the packet
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Internal entity of a packet. As everything is sent as a packet,
|
|
|
|
* all network communication will need to call the functions that
|
|
|
|
* populate the packet.
|
|
|
|
* Every packet can be at most SEND_MTU bytes. Overflowing this
|
|
|
|
* limit will give an assertion when sending (i.e. writing) the
|
|
|
|
* packet. Reading past the size of the packet when receiving
|
|
|
|
* will return all 0 values and "" in case of the string.
|
|
|
|
*/
|
2007-01-31 09:51:22 +00:00
|
|
|
struct Packet {
|
2007-01-02 17:34:03 +00:00
|
|
|
/** The next packet. Used for queueing packets before sending. */
|
2007-01-31 09:51:22 +00:00
|
|
|
Packet *next;
|
2007-01-02 17:34:03 +00:00
|
|
|
/** The size of the whole packet for received packets. For packets
|
|
|
|
* that will be sent, the value is filled in just before the
|
|
|
|
* actual transmission. */
|
|
|
|
PacketSize size;
|
|
|
|
/** The current read/write position in the packet */
|
|
|
|
PacketSize pos;
|
|
|
|
/** The buffer of this packet */
|
|
|
|
byte buffer[SEND_MTU];
|
2007-02-01 22:30:35 +00:00
|
|
|
private:
|
|
|
|
NetworkSocketHandler *cs;
|
|
|
|
|
|
|
|
public:
|
|
|
|
Packet(NetworkSocketHandler *cs);
|
|
|
|
Packet(PacketType type);
|
|
|
|
|
2007-02-01 23:26:44 +00:00
|
|
|
/* Sending/writing of packets */
|
2007-03-07 11:47:46 +00:00
|
|
|
void PrepareToSend();
|
2007-02-01 22:30:35 +00:00
|
|
|
|
2007-02-02 23:16:58 +00:00
|
|
|
void Send_bool (bool data);
|
2007-02-01 23:26:44 +00:00
|
|
|
void Send_uint8 (uint8 data);
|
|
|
|
void Send_uint16(uint16 data);
|
|
|
|
void Send_uint32(uint32 data);
|
|
|
|
void Send_uint64(uint64 data);
|
2009-01-10 00:31:47 +00:00
|
|
|
void Send_string(const char *data);
|
2007-01-02 17:34:03 +00:00
|
|
|
|
2007-02-01 23:26:44 +00:00
|
|
|
/* Reading/receiving of packets */
|
2007-03-07 11:47:46 +00:00
|
|
|
void ReadRawPacketSize();
|
|
|
|
void PrepareToRead();
|
2007-01-02 17:34:03 +00:00
|
|
|
|
2007-02-01 23:26:44 +00:00
|
|
|
bool CanReadFromPacket (uint bytes_to_read);
|
2007-03-07 11:47:46 +00:00
|
|
|
bool Recv_bool ();
|
|
|
|
uint8 Recv_uint8 ();
|
|
|
|
uint16 Recv_uint16();
|
|
|
|
uint32 Recv_uint32();
|
|
|
|
uint64 Recv_uint64();
|
2009-01-18 13:12:57 +00:00
|
|
|
void Recv_string(char *buffer, size_t size, bool allow_newlines = false);
|
2007-02-01 23:26:44 +00:00
|
|
|
};
|
2007-01-02 17:34:03 +00:00
|
|
|
|
2007-02-01 23:26:44 +00:00
|
|
|
Packet *NetworkSend_Init(PacketType type);
|
2007-01-02 17:34:03 +00:00
|
|
|
|
|
|
|
#endif /* ENABLE_NETWORK */
|
|
|
|
|
|
|
|
#endif /* NETWORK_CORE_PACKET_H */
|