Vegastrike 0.5.1 rc1  1.0
Original sources for Vegastrike Evolved
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
netclient_devices.cpp
Go to the documentation of this file.
2 #include "vsfilesystem.h"
4 #include "networking/netclient.h"
5 #include "cmd/unit_generic.h"
6 #include "cmd/ai/firekeyboard.h"
10 #include "networking/fileutil.h"
11 
12 void NetClient::send( Cmd cmd, NetBuffer &netbuf, int mode, const char *file, int line )
13 {
14  Unit *un = this->game_unit.GetUnit();
15  int serial = 0;
16  if (un)
17  serial = un->GetSerial();
18  Packet p;
19  p.send( cmd, serial,
20  netbuf.getData(), netbuf.getDataLength(),
21  mode, NULL,
22  *( (mode&SENDANDFORGET) ? this->lossy_socket : this->clt_tcp_sock ),
23  file, line );
24 }
25 
26 /*
27  *****************************************************************************************
28  *** WEAPON STUFF ***
29  *****************************************************************************************
30  */
31 void NetClient::sendCloak( bool engage )
32 {
33  NetBuffer netbuf;
34 
35  netbuf.addChar( engage ? 1 : 0 );
36  //Shold people be allowed to scan units in other zones?
37 //netbuf.addShort( un->activeStarSystem->GetZone());
38 
39  send( CMD_CLOAK, netbuf, SENDRELIABLE, __FILE__, __LINE__ );
40 }
41 
42 void NetClient::sendCustom( string command, string args, string id )
43 {
44  NetBuffer netbuf;
45 
46  netbuf.addString( command );
47  netbuf.addString( args );
48  netbuf.addString( id );
49 
50  send( CMD_CUSTOM, netbuf, SENDRELIABLE, __FILE__, __LINE__ );
51 }
52 
53 //Send a info request about the target
55 {
56  Packet p;
57  NetBuffer netbuf;
58  Unit *un = this->game_unit.GetUnit();
59  if (!un) return;
60  netbuf.addSerial( target->GetSerial() );
61  //Shold people be allowed to scan units in other zones?
62 //netbuf.addShort( un->activeStarSystem->GetZone());
63 
64  send( CMD_SCAN, netbuf, SENDRELIABLE, __FILE__, __LINE__ );
65 }
66 
67 //Send a info request about the target
69 {
70  NetBuffer netbuf;
71  Unit *un = this->game_unit.GetUnit();
72  if (!un) return;
73  netbuf.addSerial( target->GetSerial() );
74 
75  send( CMD_TARGET, netbuf, SENDRELIABLE, __FILE__, __LINE__ );
76  if (target->GetSerial() == 0)
77  //not networked unit
78  un->computer.target.SetUnit( target );
79 }
80 
81 //In fireRequest we must use the provided serial because it may not be the client's serial
82 //but may be a turret serial
83 void NetClient::fireRequest( ObjSerial serial, const vector< int > &mount_indicies, char mis )
84 {
85  NetBuffer netbuf;
86 
87  netbuf.addSerial( serial );
88  netbuf.addChar( mis );
89  netbuf.addInt32( mount_indicies.size() );
90  for (unsigned int i = 0; i < mount_indicies.size(); ++i)
91  netbuf.addInt32( mount_indicies[i] );
92  //NETFIXME: Use UDP for fire requests? or only from server->other clients
93  send( CMD_FIREREQUEST, netbuf, SENDRELIABLE, __FILE__, __LINE__ );
94 }
95 
96 void NetClient::unfireRequest( ObjSerial serial, const vector< int > &mount_indicies )
97 {
98  NetBuffer netbuf;
99 
100  netbuf.addSerial( serial );
101  netbuf.addInt32( mount_indicies.size() );
102  for (unsigned int i = 0; i < mount_indicies.size(); ++i)
103  netbuf.addInt32( mount_indicies[i] );
104  send( CMD_UNFIREREQUEST, netbuf, SENDRELIABLE, __FILE__, __LINE__ );
105 }
106 
108  ObjSerial seller,
109  const std::string &cargo,
110  unsigned int quantity,
111  int mountOffset,
112  int subunitOffset )
113 {
114  NetBuffer netbuf;
115  Unit *un = this->game_unit.GetUnit();
116  if (!un) return;
117  netbuf.addSerial( buyer ); //If the buyer is the player, it is buying cargo.
118  netbuf.addSerial( seller ); //If seller is the player, it is selling cargo.
119  netbuf.addInt32( quantity );
120  netbuf.addString( cargo );
121  netbuf.addInt32( (unsigned int) (mountOffset) );
122  netbuf.addInt32( (unsigned int) (subunitOffset) );
123 
124  send( CMD_CARGOUPGRADE, netbuf, SENDRELIABLE, __FILE__, __LINE__ );
125 }
126 
127 void NetClient::shipRequest( const std::string &cargo, unsigned char type )
128 {
129  NetBuffer netbuf;
130  netbuf.addString( cargo );
131  netbuf.addChar( type );
132  send( CMD_SHIPDEALER, netbuf, SENDRELIABLE, __FILE__, __LINE__ );
133 }
134 
135 bool NetClient::jumpRequest( string newsystem, ObjSerial jumpserial )
136 {
137  NetBuffer netbuf;
138  Unit *un = this->game_unit.GetUnit();
139  if (!un) return false;
140  /*
141  * netbuf.addString( newsystem);
142  * netbuf.addSerial( jumpserial);
143  * netbuf.addShort( un->getStarSystem()->GetZone());
144  * #ifdef CRYPTO
145  * unsigned char * hash = new unsigned char[FileUtil::Hash.DigestSize()];
146  * bool autogen;
147  * FileUtil::HashFileCompute( VSFileSystem::GetCorrectStarSysPath( newsystem+".system", autogen), hash, SystemFile);
148  * netbuf.addBuffer( hash, FileUtil::Hash.DigestSize());
149  * #endif
150  */
151  send( CMD_JUMP, netbuf, SENDRELIABLE, __FILE__, __LINE__ );
152  //NO, WE MUST NOT BLOCK THE GAME WHILE WE ARE WAITING FOR SERVER AUTH
153  //Should wait for jump authorization
154  /*
155  * this->PacketLoop( CMD_JUMP);
156  * bool ret;
157  * if( this->jumpok)
158  * ret = true;
159  * else
160  * ret = false;
161  *
162  * jumpok = false;
163  */
164 
165  return true;
166 }
167 
169 {
170  return jumpok;
171 }
172 
174 {
175  jumpok = false;
176 }
177 
179 {
180  //Send a packet with CMD_DOCK with serial and an ObjSerial = unit_to_dock_with_serial
181  NetBuffer netbuf;
182  Unit *un = this->game_unit.GetUnit();
183  if (!un) return;
184  cerr<<"SENDING A KILL REQUEST FOR PLAYER "<<callsign<<endl;
185  send( CMD_KILL, netbuf, SENDRELIABLE, __FILE__, __LINE__ );
186 }
187 
189 {
190  //Send a packet with CMD_DOCK with serial and an ObjSerial = unit_to_dock_with_serial
191  NetBuffer netbuf;
192  Unit *un = this->game_unit.GetUnit();
193  if (!un) return;
194  cerr<<"SENDING A SAVE REQUEST FOR PLAYER "<<callsign<<endl;
195  send( CMD_SAVEACCOUNTS, netbuf, SENDRELIABLE, __FILE__, __LINE__ );
196 
197  int cpnum = _Universe->whichPlayerStarship( un );
198  if (cpnum >= 0 && lastsave.size() >= 2 && this->netversion < 4960)
199  SaveNetUtil::GetSaveStrings( cpnum, lastsave[0], lastsave[1], true );
200 }
201 
203 {
204  //Send a packet with CMD_DOCK with serial and an ObjSerial = unit_to_dock_with_serial
205  NetBuffer netbuf;
206 
207  cerr<<"SENDING A DOCK REQUEST FOR UNIT "<<utdw_serial<<endl;
208  netbuf.addSerial( utdw_serial );
209  send( CMD_DOCK, netbuf, SENDRELIABLE, __FILE__, __LINE__ );
210 }
211 
212 void NetClient::missionRequest( unsigned short packetType, string mission, int pos )
213 {
214  NetBuffer netbuf;
215 
216  netbuf.addShort( packetType );
217  netbuf.addString( mission );
218  netbuf.addInt32( pos );
219 
220  send( CMD_MISSION, netbuf, SENDRELIABLE, __FILE__, __LINE__ );
221 }
222 
224 {
225  NetBuffer netbuf;
226  netbuf.addSerial( sendTo );
227  int edge = c.edgenum;
228  if (edge > 127)
229  edge = -1;
230  netbuf.addChar( edge );
231  if (edge == -1)
232  netbuf.addInt32( c.curstate );
233  else
234  netbuf.addInt32( c.prevstate );
235  send( CMD_COMM, netbuf, SENDRELIABLE, __FILE__, __LINE__ );
236 }
237 
239 {
240  //Send a packet with CMD_UNDOCK with serial and an ObjSerial = unit_to_undock_with_serial
241  NetBuffer netbuf;
242 
243  cerr<<"SENDING A UNDOCK NOTIFICATION FOR UNIT "<<utdw_serial<<endl;
244  netbuf.addSerial( utdw_serial );
245  send( CMD_UNDOCK, netbuf, SENDRELIABLE, __FILE__, __LINE__ );
246 }
247 
248 /*
249  *****************************************************************************************
250  *** COMMUNICATION STUFF ***
251  *****************************************************************************************
252  */
253 
254 void NetClient::createNetComm( float minfreq, float maxfreq, bool video, bool secured, string method )
255 {
256  if (NetComm != NULL) {
257  current_freq = minfreq;
258  selected_freq = maxfreq;
259  this->NetComm = new NetworkCommunication( minfreq, maxfreq, video, secured, method );
260  }
261 }
262 
264 {
265  if (NetComm != NULL) {
266  delete NetComm;
267  NetComm = NULL;
268  }
269 }
270 
272 {
273  if (NetComm != NULL) {
274  char webcam_support = NetComm->HasWebcam();
275  char portaudio_support = NetComm->HasPortaudio();
276  selected_freq = current_freq;
277  NetBuffer netbuf;
278  netbuf.addFloat( selected_freq );
279  netbuf.addChar( NetComm->IsSecured() );
280  netbuf.addChar( webcam_support );
281  netbuf.addChar( portaudio_support );
282  NetComm->InitSession( selected_freq );
283  //cerr<<"Session started."<<endl;
284  //cerr<<"Grabbing an image"<<endl;
285  Packet p;
286  p.send( CMD_STARTNETCOMM, serial, netbuf.getData(), netbuf.getDataLength(), SENDRELIABLE, NULL, *this->clt_tcp_sock,
287  __FILE__, PSEUDO__LINE__( 1565 ) );
288  cerr<<"Starting communication session\n\n"<<endl;
289  //NetComm->GrabImage();
290  }
291 }
292 
294 {
295  if (NetComm != NULL) {
296  NetBuffer netbuf;
297  netbuf.addFloat( selected_freq );
298  Packet p;
299  p.send( CMD_STOPNETCOMM, serial, netbuf.getData(), netbuf.getDataLength(), SENDRELIABLE, NULL, *this->clt_tcp_sock,
300  __FILE__, PSEUDO__LINE__( 1578 ) );
301  NetComm->DestroySession();
302  cerr<<"Stopped communication session"<<endl;
303  }
304 }
305 
307 {
308  if (NetComm != NULL) {
309  if ( current_freq == NetComm->MinFreq() )
310  current_freq = NetComm->MaxFreq();
311  else
312  current_freq -= .1;
313  }
314 }
315 
317 {
318  if (NetComm != NULL) {
319  if ( current_freq == NetComm->MaxFreq() )
320  current_freq = NetComm->MinFreq();
321  else
322  current_freq += .1;
323  }
324 }
325 
327 {
328  return this->selected_freq;
329 }
330 
332 {
333  return this->current_freq;
334 }
335 
336 void NetClient::sendTextMessage( string message )
337 {
338  //Only send if netcomm is active and we are connected on a frequency
339  if ( NetComm != NULL && NetComm->IsActive() )
340  NetComm->SendMessage( *this->clt_tcp_sock, this->serial, message );
341 }
342 
343 /*
344  *************************************************************
345  **** Check if we have to send a webcam picture ***
346  *************************************************************
347  */
348 
350 {
351  this->NetComm->StartWebcamTransfer();
352 }
353 
355 {
356  this->NetComm->StopWebcamTransfer();
357 }
358 
360 {
361  if (NetComm != NULL)
362  return NetComm->GetWebcamCapture();
363  return NULL; //We have no choice...
364 }
365 
367 {
368  if (NetComm != NULL)
369  return NetComm->GetWebcamFromNetwork( length );
370  return NULL; //We have no choice...
371 }
372 
374 {
375  return this->NetComm == NULL ? false : this->NetComm->IsActive();
376 }
377 
379 {
380  bool ret = false;
381  if (this->NetComm != NULL)
382  ret = this->NetComm->IsSecured();
383  return ret;
384 }
385 
387 {
388  if (NetComm != NULL)
389  NetComm->SwitchSecured();
390 }
392 {
393  if (NetComm != NULL)
394  NetComm->SwitchWebcam();
395 }
397 {
398  return NetComm->HasWebcam();
399 }
400