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 | |
---|
18 | enum |
---|
19 | { |
---|
20 | UDPCMD_DISCONNECT = 1, |
---|
21 | UDPCMD_INVALIDRNDBYTE = 2 |
---|
22 | }; |
---|
23 | |
---|
24 | class UdpSocket : public NetworkSocket |
---|
25 | { |
---|
26 | ObjectListDeclaration(UdpSocket); |
---|
27 | public: |
---|
28 | UdpSocket(); |
---|
29 | UdpSocket( UdpServerSocket * serverSocket, IPaddress ip, int userId, byte randomByte ); |
---|
30 | UdpSocket( std::string host, int port ); |
---|
31 | virtual ~UdpSocket(); |
---|
32 | |
---|
33 | virtual void connectToServer( std::string host, int port ); |
---|
34 | virtual void disconnectServer(); |
---|
35 | |
---|
36 | virtual void reconnectToServer( std::string host, int port); |
---|
37 | virtual void reconnectToServerSoft( std::string host, int port); |
---|
38 | |
---|
39 | virtual bool writePacket(byte * data, int length ); |
---|
40 | virtual int readPacket(byte * data, int maxLength); |
---|
41 | |
---|
42 | |
---|
43 | private: |
---|
44 | void init(); |
---|
45 | |
---|
46 | bool writeRawPacket( byte * data, int length ); |
---|
47 | bool checkUdpCmd( byte udpCmd ); |
---|
48 | bool checkRandomByte( byte rndByte ); |
---|
49 | byte generateNewRandomByte(); |
---|
50 | |
---|
51 | |
---|
52 | private: |
---|
53 | UdpServerSocket * serverSocket; //!< will get packets here |
---|
54 | int userId; //!< user id used by serverSocket |
---|
55 | UDPsocket socket; //!< socket used to send/recieve |
---|
56 | UDPpacket * packet; |
---|
57 | |
---|
58 | byte randomByte; //!< contains random bytes & 0xFC |
---|
59 | }; |
---|
60 | |
---|
61 | |
---|
62 | |
---|
63 | #endif /* _NETWORK_SOCKET */ |
---|