[7540] | 1 | /*! |
---|
| 2 | * @file upd_socket.h |
---|
| 3 | * class to connect to other clients via udp |
---|
| 4 | */ |
---|
| 5 | |
---|
| 6 | #ifndef _UDP_SOCKET |
---|
| 7 | #define _UDP_SOCKET |
---|
| 8 | |
---|
| 9 | /* include this file, it contains some default definitions */ |
---|
| 10 | #include "netdefs.h" |
---|
| 11 | |
---|
| 12 | |
---|
| 13 | /* include base_object.h since all classes are derived from this one */ |
---|
| 14 | #include "network_socket.h" |
---|
| 15 | |
---|
| 16 | class UdpServerSocket; |
---|
| 17 | |
---|
[8802] | 18 | enum |
---|
| 19 | { |
---|
| 20 | UDPCMD_DISCONNECT = 1, |
---|
| 21 | UDPCMD_INVALIDRNDBYTE = 2 |
---|
| 22 | }; |
---|
| 23 | |
---|
[7540] | 24 | class UdpSocket : public NetworkSocket |
---|
| 25 | { |
---|
| 26 | public: |
---|
| 27 | UdpSocket(); |
---|
[8802] | 28 | UdpSocket( UdpServerSocket * serverSocket, IPaddress ip, int userId, byte randomByte ); |
---|
[7540] | 29 | UdpSocket( std::string host, int port ); |
---|
| 30 | virtual ~UdpSocket(); |
---|
| 31 | |
---|
| 32 | virtual void connectToServer( std::string host, int port ); |
---|
| 33 | virtual void disconnectServer(); |
---|
| 34 | |
---|
[9406] | 35 | virtual void reconnectToServer( std::string host, int port); |
---|
| 36 | virtual void reconnectToServerSoft( std::string host, int port); |
---|
| 37 | |
---|
[8802] | 38 | virtual bool writePacket(byte * data, int length ); |
---|
[9406] | 39 | virtual int readPacket(byte * data, int maxLength); |
---|
[7540] | 40 | |
---|
[9406] | 41 | |
---|
[7540] | 42 | private: |
---|
[9656] | 43 | void init(); |
---|
[9406] | 44 | |
---|
[8802] | 45 | bool writeRawPacket( byte * data, int length ); |
---|
| 46 | bool checkUdpCmd( byte udpCmd ); |
---|
| 47 | bool checkRandomByte( byte rndByte ); |
---|
| 48 | byte generateNewRandomByte(); |
---|
[9406] | 49 | |
---|
[7540] | 50 | |
---|
[9656] | 51 | private: |
---|
| 52 | UdpServerSocket * serverSocket; //!< will get packets here |
---|
| 53 | int userId; //!< user id used by serverSocket |
---|
| 54 | UDPsocket socket; //!< socket used to send/recieve |
---|
| 55 | UDPpacket * packet; |
---|
| 56 | |
---|
| 57 | byte randomByte; //!< contains random bytes & 0xFC |
---|
[7540] | 58 | }; |
---|
| 59 | |
---|
| 60 | |
---|
| 61 | |
---|
| 62 | #endif /* _NETWORK_SOCKET */ |
---|