1 | /*! |
---|
2 | * @file udp_server_socket.h |
---|
3 | * waits for incoming connections |
---|
4 | |
---|
5 | */ |
---|
6 | |
---|
7 | #ifndef _UDP_SERVER_SOCKET |
---|
8 | #define _UDP_SERVER_SOCKET |
---|
9 | |
---|
10 | /* include this file, it contains some default definitions */ |
---|
11 | #include "netdefs.h" |
---|
12 | |
---|
13 | |
---|
14 | /* include base_object.h since all classes are derived from this one */ |
---|
15 | #include "base_object.h" |
---|
16 | #include "server_socket.h" |
---|
17 | #include "udp_socket.h" |
---|
18 | |
---|
19 | #include <list> |
---|
20 | #include <vector> |
---|
21 | |
---|
22 | #define UDP_PACKET_SIZE 10240 |
---|
23 | #define MAX_NEW_CONNECTIONS 8 |
---|
24 | |
---|
25 | struct NetworkPacket |
---|
26 | { |
---|
27 | int length; |
---|
28 | byte * data; |
---|
29 | }; |
---|
30 | |
---|
31 | typedef std::list<NetworkPacket> NetworkPacketList; |
---|
32 | |
---|
33 | typedef std::vector<NetworkPacketList> PacketBuffer; |
---|
34 | |
---|
35 | |
---|
36 | //!< informations struct for each user |
---|
37 | struct UserInfo |
---|
38 | { |
---|
39 | IPaddress addr; //!< ip address of this user |
---|
40 | byte randomByte; //!< random byte of this user |
---|
41 | }; |
---|
42 | |
---|
43 | |
---|
44 | typedef std::vector<UserInfo> UserList; |
---|
45 | |
---|
46 | typedef std::list<UdpSocket*> UdpSocketList; |
---|
47 | |
---|
48 | |
---|
49 | //!< the upd server socket listening for incoming connections |
---|
50 | class UdpServerSocket : public ServerSocket |
---|
51 | { |
---|
52 | ObjectListDeclaration(UdpServerSocket); |
---|
53 | public: |
---|
54 | UdpServerSocket( int port); |
---|
55 | virtual ~UdpServerSocket(); |
---|
56 | |
---|
57 | /* server socket manipulations */ |
---|
58 | virtual bool listen( unsigned int port ); |
---|
59 | virtual NetworkSocket* getNewSocket( void ); |
---|
60 | virtual void close(); |
---|
61 | |
---|
62 | virtual void update(); |
---|
63 | |
---|
64 | /* network traffic interface */ |
---|
65 | NetworkPacket getPacket( int userId ); |
---|
66 | void removeUser( int userId ); |
---|
67 | bool sendPacket( NetworkPacket networkPacket, int userId ); |
---|
68 | |
---|
69 | |
---|
70 | private: |
---|
71 | void removeUserPackets( int userId ); |
---|
72 | int getPacketCount( int childId ); |
---|
73 | void initUser( int childId, IPaddress ip, byte randomByte ); |
---|
74 | |
---|
75 | |
---|
76 | private: |
---|
77 | UDPsocket socket; //!< will be used to send/recieve data to/from clients |
---|
78 | UDPpacket * packet; //!< packet structure to recieve packet |
---|
79 | PacketBuffer packetBuffer; //!< will store recieved packets for UdpSockets |
---|
80 | UserList userList; //!< contains information about clients |
---|
81 | UdpSocketList newSocketList; //!< contains new socket |
---|
82 | |
---|
83 | }; |
---|
84 | |
---|
85 | #endif |
---|