00001 /* $Id: packet.h 17746 2009-10-09 11:03:00Z smatz $ */ 00002 00003 /* 00004 * This file is part of OpenTTD. 00005 * 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. 00006 * 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. 00007 * 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/>. 00008 */ 00009 00014 #ifndef NETWORK_CORE_PACKET_H 00015 #define NETWORK_CORE_PACKET_H 00016 00017 #include "config.h" 00018 #include "core.h" 00019 00020 #ifdef ENABLE_NETWORK 00021 00022 typedef uint16 PacketSize; 00023 typedef uint8 PacketType; 00024 00034 struct Packet { 00036 Packet *next; 00040 PacketSize size; 00042 PacketSize pos; 00044 byte buffer[SEND_MTU]; 00045 private: 00046 NetworkSocketHandler *cs; 00047 00048 public: 00049 Packet(NetworkSocketHandler *cs); 00050 Packet(PacketType type); 00051 00052 /* Sending/writing of packets */ 00053 void PrepareToSend(); 00054 00055 void Send_bool (bool data); 00056 void Send_uint8 (uint8 data); 00057 void Send_uint16(uint16 data); 00058 void Send_uint32(uint32 data); 00059 void Send_uint64(uint64 data); 00060 void Send_string(const char *data); 00061 00062 /* Reading/receiving of packets */ 00063 void ReadRawPacketSize(); 00064 void PrepareToRead(); 00065 00066 bool CanReadFromPacket (uint bytes_to_read); 00067 bool Recv_bool (); 00068 uint8 Recv_uint8 (); 00069 uint16 Recv_uint16(); 00070 uint32 Recv_uint32(); 00071 uint64 Recv_uint64(); 00072 void Recv_string(char *buffer, size_t size, bool allow_newlines = false); 00073 }; 00074 00075 #endif /* ENABLE_NETWORK */ 00076 00077 #endif /* NETWORK_CORE_PACKET_H */