1 | /* |
---|
2 | orxonox - the future of 3D-vertical-scrollers |
---|
3 | |
---|
4 | Copyright (C) 2004 orx |
---|
5 | |
---|
6 | This program is free software; you can redistribute it and/or modify |
---|
7 | it under the terms of the GNU General Public License as published by |
---|
8 | the Free Software Foundation; either version 2, or (at your option) |
---|
9 | any later version. |
---|
10 | |
---|
11 | ### File Specific: |
---|
12 | main-programmer: Christoph Renner |
---|
13 | co-programmer: |
---|
14 | */ |
---|
15 | |
---|
16 | #include "udp_socket.h" |
---|
17 | #include "udp_server_socket.h" |
---|
18 | |
---|
19 | |
---|
20 | void UdpSocket::init( ) |
---|
21 | { |
---|
22 | //TODO setClassId |
---|
23 | this->serverSocket = NULL; |
---|
24 | this->socket = NULL; |
---|
25 | this->packet = NULL; |
---|
26 | } |
---|
27 | |
---|
28 | |
---|
29 | /** |
---|
30 | * constructor - connects to give host |
---|
31 | * @param host host |
---|
32 | * @param port port |
---|
33 | */ |
---|
34 | UdpSocket::UdpSocket( std::string host, int port ) |
---|
35 | { |
---|
36 | init(); |
---|
37 | this->packet = SDLNet_AllocPacket( UDP_PACKET_SIZE ); |
---|
38 | |
---|
39 | this->connectToServer( host, port ); |
---|
40 | } |
---|
41 | |
---|
42 | /** |
---|
43 | * default constructor. use this one if you want to call connecttoServer |
---|
44 | */ |
---|
45 | UdpSocket::UdpSocket( ) |
---|
46 | { |
---|
47 | this->init(); |
---|
48 | this->packet = SDLNet_AllocPacket( UDP_PACKET_SIZE ); |
---|
49 | |
---|
50 | if ( !packet ) |
---|
51 | { |
---|
52 | PRINTF(1)("SDLNet_AllocPacket: %s\n", SDLNet_GetError()); |
---|
53 | |
---|
54 | assert( false ); |
---|
55 | bOk = false; |
---|
56 | } |
---|
57 | |
---|
58 | } |
---|
59 | |
---|
60 | /** |
---|
61 | * constructor. used by UdpServerSocket |
---|
62 | * @param serverSocket pointer to serverSocket |
---|
63 | * @param ip client's ip address |
---|
64 | * @param userId userid used by serverSocket |
---|
65 | */ |
---|
66 | UdpSocket::UdpSocket( UdpServerSocket * serverSocket, IPaddress ip, int userId ) |
---|
67 | { |
---|
68 | this->init(); |
---|
69 | this->serverSocket = serverSocket; |
---|
70 | } |
---|
71 | |
---|
72 | /** |
---|
73 | * destructor |
---|
74 | */ |
---|
75 | UdpSocket::~ UdpSocket( ) |
---|
76 | { |
---|
77 | if ( serverSocket ) |
---|
78 | serverSocket->removeUser( userId ); |
---|
79 | |
---|
80 | disconnectServer(); |
---|
81 | } |
---|
82 | |
---|
83 | /** |
---|
84 | * connect to server |
---|
85 | * @param host host name |
---|
86 | * @param port port number |
---|
87 | */ |
---|
88 | void UdpSocket::connectToServer( std::string host, int port ) |
---|
89 | { |
---|
90 | assert( serverSocket == NULL ); |
---|
91 | |
---|
92 | IPaddress ip; |
---|
93 | |
---|
94 | if ( SDLNet_ResolveHost( &ip, host.c_str(), port ) != 0 ) |
---|
95 | { |
---|
96 | PRINTF(1)("SDLNet_ResolveHost: %s\n", SDLNet_GetError() ); |
---|
97 | bOk = false; |
---|
98 | return; |
---|
99 | } |
---|
100 | |
---|
101 | socket = SDLNet_UDP_Open(0); |
---|
102 | if ( !socket ) |
---|
103 | { |
---|
104 | PRINTF(1)("SDLNet_UDP_Open: %s\n", SDLNet_GetError() ); |
---|
105 | bOk = false; |
---|
106 | return; |
---|
107 | } |
---|
108 | |
---|
109 | int channel = SDLNet_UDP_Bind(socket, 1, &ip); |
---|
110 | if ( channel == -1 ) |
---|
111 | { |
---|
112 | PRINTF(2)("SDLNet_UDP_Bind: %s\n", SDLNet_GetError()); |
---|
113 | bOk = false; |
---|
114 | return; |
---|
115 | } |
---|
116 | } |
---|
117 | |
---|
118 | /** |
---|
119 | * disconnect from server |
---|
120 | */ |
---|
121 | void UdpSocket::disconnectServer( ) |
---|
122 | { |
---|
123 | SDLNet_UDP_Unbind( socket, -1 ); |
---|
124 | SDLNet_UDP_Close( socket ); |
---|
125 | socket = NULL; |
---|
126 | } |
---|
127 | |
---|
128 | /** |
---|
129 | * send one packet to other host |
---|
130 | * @param data pointer to data which will be sent |
---|
131 | * @param length length of data |
---|
132 | * @return true on success |
---|
133 | */ |
---|
134 | bool UdpSocket::writePacket( byte * data, int length ) |
---|
135 | { |
---|
136 | if ( serverSocket ) |
---|
137 | { |
---|
138 | NetworkPacket networkPacket; |
---|
139 | networkPacket.length = length; |
---|
140 | networkPacket.data = data; |
---|
141 | if ( !serverSocket->sendPacket( networkPacket, this->userId ) ) |
---|
142 | { |
---|
143 | bOk = false; |
---|
144 | return false; |
---|
145 | } |
---|
146 | else |
---|
147 | return true; |
---|
148 | } |
---|
149 | else |
---|
150 | { |
---|
151 | assert( length <= packet->maxlen ); |
---|
152 | |
---|
153 | memcpy( packet->data, data, length ); |
---|
154 | packet->len = length; |
---|
155 | |
---|
156 | if ( SDLNet_UDP_Send( socket, 1, packet) == 0 ) |
---|
157 | { |
---|
158 | PRINTF(1)("SDLNet_UDP_Send: %s\n", SDLNet_GetError()); |
---|
159 | bOk = false; |
---|
160 | return false; |
---|
161 | } |
---|
162 | |
---|
163 | return true; |
---|
164 | } |
---|
165 | } |
---|
166 | |
---|
167 | /** |
---|
168 | * recieve one packet from another host |
---|
169 | * @param data pointer to buffer to copy data into |
---|
170 | * @param maxLength maximal length of buffer |
---|
171 | * @return less than 0 on error, number bytes read else |
---|
172 | */ |
---|
173 | int UdpSocket::readPacket( byte * data, int maxLength ) |
---|
174 | { |
---|
175 | if ( serverSocket ) |
---|
176 | { |
---|
177 | NetworkPacket networkPacket = serverSocket->getPacket( this->userId ); |
---|
178 | |
---|
179 | if ( networkPacket.length > 0 ) |
---|
180 | { |
---|
181 | assert( maxLength > networkPacket.length ); |
---|
182 | |
---|
183 | memcpy( data, networkPacket.data, networkPacket.length ); |
---|
184 | } |
---|
185 | |
---|
186 | if ( networkPacket.data ) |
---|
187 | { |
---|
188 | delete networkPacket.data; |
---|
189 | networkPacket.data = NULL; |
---|
190 | } |
---|
191 | |
---|
192 | return networkPacket.length; |
---|
193 | } |
---|
194 | else |
---|
195 | { |
---|
196 | int numrecv = SDLNet_UDP_Recv( socket, packet); |
---|
197 | if ( numrecv > 0) |
---|
198 | { |
---|
199 | assert( packet->len <= maxLength ); |
---|
200 | |
---|
201 | memcpy( data, packet->data, packet->len ); |
---|
202 | return packet->len; |
---|
203 | } |
---|
204 | else if ( numrecv < 0 ) |
---|
205 | { |
---|
206 | PRINTF(1)("SDLNet_UDP_Recv: %s\n", SDLNet_GetError()); |
---|
207 | bOk = false; |
---|
208 | return -1; |
---|
209 | } |
---|
210 | else |
---|
211 | { |
---|
212 | return 0; |
---|
213 | } |
---|
214 | } |
---|
215 | |
---|
216 | return 0; |
---|
217 | } |
---|
218 | |
---|
219 | |
---|
220 | |
---|