7 #ifndef CAFU_NETWORK_HPP_INCLUDED
8 #define CAFU_NETWORK_HPP_INCLUDED
14 #define WIN32_LEAN_AND_MEAN
20 #include <netinet/in.h>
23 #define INVALID_SOCKET -1
24 #define SOCKET_ERROR -1
25 #define closesocket close
26 #define WSAGetLastError() errno
29 #include "Templates/Array.hpp"
31 #if defined(_WIN32) && _MSC_VER<1600
77 WinSockT(
unsigned short RequestedVersion=0x0002);
89 SOCKET
GetTCPClientSocket(
const char* ServerAddress,
unsigned short ServerPortNr)
const;
111 NetAddressT(
char IP0,
char IP1,
char IP2,
char IP3,
unsigned short Port_);
117 NetAddressT(
const char* Name,
unsigned short Port_);
123 NetAddressT(
const std::string& Name,
unsigned short Port_);
168 operator SOCKET ()
const;
281 const unsigned long StringStart=
ReadPos;
293 return &
Data[StringStart];
302 DMsg.PushBackEmptyExact(
ReadLong());
303 for (
unsigned long i=0; i<DMsg.
Size(); i++) DMsg[i]=
ReadByte();
319 Data.PushBackEmpty(2);
327 Data.PushBackEmpty(4);
344 const unsigned long Start =
Data.
Size();
345 const unsigned long Length=(
unsigned long)strlen(String)+1;
347 Data.PushBackEmpty(Length);
348 memcpy(&
Data[Start], String, Length);
362 for (
unsigned long i=0; i<AoB.
Size(); i++)
Data.PushBack(AoB[i]);
370 for (
unsigned long i=0; i<DMsg.
Size(); i++)
Data.PushBack(DMsg[i]);
396 static const unsigned long ACK_FLAG;
399 static const unsigned long ACK_MASK;
402 static const unsigned long MAX_MSG_SIZE;
405 unsigned long NextOutgoingSequenceNr;
408 unsigned long LastIncomingSequenceNr;
411 unsigned long LastReliableSequenceNr;
414 unsigned long ExpectedIncomingAckBit;
417 unsigned long ExpectedOutgoingAckBit;
420 bool ResendReliableDataInFlight;
Exception that is thrown on name look-up failure.
Definition: Network.hpp:103
sockaddr_in ToSockAddrIn() const
Converts the network address into a sockaddr_in struct.
Definition: Network.cpp:185
NetAddressT(char IP0, char IP1, char IP2, char IP3, unsigned short Port_)
Constructor.
Definition: Network.cpp:117
Network address consisting of an IP4 address and port number.
Definition: Network.hpp:98
WinSockAPIError(unsigned long Error_)
Constructor.
Definition: Network.hpp:190
GameProtocol1T()
Create a connection over the GameProtocol1T.
Definition: Network.cpp:290
This class implements a mixture of a reliable and unreliable, bi-directional network protocol for Caf...
Definition: Network.hpp:391
void ReadBegin()
Initializes reading of the data buffer.
Definition: Network.hpp:226
Message length error.
Definition: Network.hpp:203
static bool IsIncomingMessageOutOfBand(NetDataT &InData)
Returns true if 'InData' is a 'out-of-band' message, 'false' otherwise.
Definition: Network.cpp:437
bool operator==(const NetAddressT &Address) const
Compares two network addresses.
Definition: Network.cpp:202
NetAddressT Address
Remote address on which this error occured.
Definition: Network.hpp:198
void WriteWord(unsigned short w)
Writes one Word (16 Bit) into the data buffer.
Definition: Network.hpp:317
unsigned long Actual
Actually read bytes.
Definition: Network.hpp:206
void Send(SOCKET Socket, const NetAddressT &ReceiverAddress) const
Send the content of NetDataT through 'Socket' (non-blocking UDP socket) to 'ReceiverAddress'.
Definition: Network.cpp:245
void WriteArrayOfBytes(const ArrayT< char > &AoB)
Writes an ArrayT<char> into the data buffer.
Definition: Network.hpp:360
WinSock error.
Definition: Network.hpp:186
unsigned long Size() const
Get size of array.
Definition: Array.hpp:138
MessageLength(unsigned long Wanted_, unsigned long Actual_)
Constructor.
Definition: Network.hpp:211
This class simplifies the usage of the WinSock API.
Definition: Network.hpp:43
void WriteString(const std::string &String)
Writes a String into the data buffer.
Definition: Network.hpp:353
void WriteDMsg(const ArrayT< uint8_t > &DMsg)
Writes a delta message as created by cf::Network::StateT into the data buffer.
Definition: Network.hpp:367
unsigned long Wanted
Bytes wanted to read.
Definition: Network.hpp:205
~WinSockT()
Shuts down WinSock properly by calling WSACleanup().
Definition: Network.cpp:35
WinSock initialization error.
Definition: Network.hpp:54
BadVersion(unsigned short Version_)
Constructor.
Definition: Network.hpp:70
unsigned long Error
Error code.
Definition: Network.hpp:197
NetDataT()
Constructor.
Definition: Network.hpp:221
bool operator!=(const NetAddressT &Address) const
Compares two network addresses.
Definition: Network.cpp:211
const NetDataT & GetTransmitData(const ArrayT< ArrayT< char > > &ReliableDatas, const ArrayT< char > &UnreliableData)
This function takes 'ReliableDatas' and 'UnreliableData' and returns a 'NetDataT' object created from...
Definition: Network.cpp:305
float ReadFloat()
Reads one Float (32 Bit) from the data buffer.
Definition: Network.hpp:270
int Error
Error code.
Definition: Network.hpp:56
const char * ReadString()
Reads a String from the data buffer.
Definition: Network.hpp:279
char ReadByte()
Reads one Byte (8 Bit) from the data buffer.
Definition: Network.hpp:234
SOCKET GetTCPClientSocket(const char *ServerAddress, unsigned short ServerPortNr) const
Creates a non-blocking TCP/IP socket that is connected to ServerPortNr at ServerAddr.
Definition: Network.cpp:66
Class that allows easy and portable handling, sending and receiving of data over a network...
Definition: Network.hpp:181
unsigned short ReadWord()
Read one Word (16 Bit) from the data buffer.
Definition: Network.hpp:246
NetSocketT(SOCKET Socket_)
Constructor.
Definition: Network.cpp:225
The exception that might be thrown on failure of the GetTransmitData() method.
Definition: Network.hpp:432
NetAddressT Receive(SOCKET Socket)
Receives one packet from 'Socket' (non-blocking UDP socket), overwrites the content of this NetDataT ...
Definition: Network.cpp:259
unsigned long GetNextOutgoingSequenceNr()
Returns the sequence number of the next packet sent to the remote client that is not-out-of-band.
Definition: Network.hpp:461
A socket that closes itself at the end of its livetime (scope).
Definition: Network.hpp:154
Network error (all other exceptions of this library derive from this one).
Definition: Network.hpp:39
static const NetDataT & GetTransmitOutOfBandData(const ArrayT< char > &UnreliableData)
Returns NetDataT with 'UnreliableData' as 'out-of-band' message.
Definition: Network.cpp:426
unsigned short Version
The bad version number.
Definition: Network.hpp:66
Bad version error.
Definition: Network.hpp:64
uint32_t ReadLong()
Reads one uint32_t from the data buffer.
Definition: Network.hpp:258
const char * ToString() const
Converts the network address into a string.
Definition: Network.cpp:177
void WriteLong(uint32_t ui)
Writes one uint32_t into the data buffer.
Definition: Network.hpp:325
SOCKET GetTCPServerSocket(unsigned short PortNr) const
Creates a non-blocking TCP/IP listener socket, that is bound to port PortNr.
Definition: Network.cpp:42
ArrayT< char > Data
Data buffer contents.
Definition: Network.hpp:215
unsigned long ProcessIncomingMessage(NetDataT &InData, void(*ProcessPayload)(NetDataT &, unsigned long))
Passes the data ('Payload') that has been received from our protocol remote client to ProcessPayload(...
Definition: Network.cpp:382
void WriteByte(char b)
Writes one Byte (8 Bit) into the data buffer.
Definition: Network.hpp:310
unsigned short Port
The port number of the network address.
Definition: Network.hpp:149
void WriteFloat(float f)
Writes one float (32 Bit) into the data buffer.
Definition: Network.hpp:333
void WriteString(const char *String)
Writes a String into the data buffer.
Definition: Network.hpp:340
InitFailure(unsigned long Error_)
Constructor.
Definition: Network.hpp:60
unsigned long ReadPos
Reading position in data.
Definition: Network.hpp:216
char IP[4]
The IP number of the network address.
Definition: Network.hpp:148
ArrayT< uint8_t > ReadDMsg()
Reads a delta message for cf::Network::StateT from the data buffer.
Definition: Network.hpp:298
bool ReadOfl
Whether the attempt was made to read over the data buffer boundaries.
Definition: Network.hpp:217
SOCKET GetUDPSocket(unsigned short PortNr) const
Creates a non-blocking UDP socket that is bound to port PortNr.
Definition: Network.cpp:89
~NetSocketT()
Desctructor.
Definition: Network.cpp:230
WinSockAPIError(unsigned long Error_, const NetAddressT &Address_)
Constructor.
Definition: Network.hpp:195