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
NetworkCommunication Class Reference

#include <networkcomm.h>

Public Types

enum  CommunicationMethod { ClientBroadcast, ServerUnicast }
 

Public Member Functions

 NetworkCommunication ()
 
 NetworkCommunication (float minfreq, float maxfreq, bool video, bool secured, std::string method)
 
 NetworkCommunication (int nb)
 
 ~NetworkCommunication ()
 
int InitSession (float frequency)
 
void SendSound (SOCKETALT &send_sock, ObjSerial serial)
 
void SendMessage (SOCKETALT &send_sock, ObjSerial serial, std::string message)
 
void RecvSound (const char *sndbuffer, int length, bool encrypted=false)
 
void RecvMessage (std::string message, bool encrypted=false)
 
int DestroySession ()
 
void AddToSession (ClientPtr clt)
 
void RemoveFromSession (ClientPtr clt)
 
bool IsActive ()
 
char * GetWebcamCapture ()
 
char * GetWebcamFromNetwork (int &length)
 
void StartWebcamTransfer ()
 
void StopWebcamTransfer ()
 
char HasWebcam ()
 
char HasPortaudio ()
 
char IsSecured ()
 
void SwitchSecured ()
 
void SwitchWebcam ()
 
float MinFreq ()
 
float MaxFreq ()
 

Detailed Description

Definition at line 41 of file networkcomm.h.

Member Enumeration Documentation

Enumerator
ClientBroadcast 
ServerUnicast 

Definition at line 109 of file networkcomm.h.

Constructor & Destructor Documentation

NetworkCommunication::NetworkCommunication ( )

Definition at line 168 of file networkcomm.cpp.

169 {
170  private_init();
171 }
NetworkCommunication::NetworkCommunication ( float  minfreq,
float  maxfreq,
bool  video,
bool  secured,
std::string  method 
)
NetworkCommunication::NetworkCommunication ( int  nb)

Definition at line 263 of file networkcomm.cpp.

264 {
265  private_init();
266  assert( nb < 65536 );
267  this->max_messages = nb;
268 }
NetworkCommunication::~NetworkCommunication ( )

Definition at line 544 of file networkcomm.cpp.

References use_pa.

545 {
546 #ifdef NETCOMM_JVOIP
547  if (this->session)
548  delete this->session;
549  if (this->params)
550  delete this->params;
551  if (this->rtpparams)
552  delete this->rtpparams;
553 #endif /* NETCOMM_JVOIP */
554 #ifdef NETCOMM_PORTAUDIO
555  if (use_pa)
556  CheckPAError( Pa_Terminate(), "Pa_Terminate" );
557 #endif /* NETCOMM_PORTAUDIO */
558 #ifdef NETCOMM_WEBCAM
559  if (this->Webcam) {
560  this->Webcam->Shutdown();
561  delete this->Webcam;
562  this->Webcam = NULL;
563  }
564  if (bufitem)
565  delete bufitem;
566 #endif /* NETCOMM_NOWEBCAM */
567 #ifdef CRYPTO
568 #endif
569 }

Member Function Documentation

void NetworkCommunication::AddToSession ( ClientPtr  clt)

Definition at line 270 of file networkcomm.cpp.

References VOIP_PORT.

271 {
272  this->commClients.push_back( clt );
273  //If the client has a webcam enabled and we don't have one selected yet
274  //FIND A FIX FOR THAT TEST
275  if (*(this->webcamClient) && clt->webcam) {
276  CltPtrIterator cltit = commClients.end();
277  assert( (*cltit) == clt );
278  this->webcamClient = cltit;
279  }
280 #ifdef NETCOMM_JVOIP
281  CheckVOIPError( this->session->AddDestination( ntohl( clt->cltadr.inaddr() ), VOIP_PORT ) );
282 #endif /* NETCOMM_JVOIP */
283 }
int NetworkCommunication::DestroySession ( )

Definition at line 511 of file networkcomm.cpp.

References use_pa.

Referenced by NetClient::stopCommunication().

512 {
513  this->active = false;
514 #ifdef NETCOMM_PORTAUDIO
515  if (use_pa) {
516  CheckPAError( Pa_StopStream( this->instream ), "Pa_StopStream(instream)" );
517  CheckPAError( Pa_StopStream( this->outstream ), "Pa_StopStream(outstream)" );
518 
519  CheckPAError( Pa_CloseStream( this->instream ), "Pa_CloseStream(instream)" );
520  CheckPAError( Pa_CloseStream( this->outstream ), "Pa_CloseStream(outstream)" );
521  }
522 #endif
523 #ifdef NETCOMM_JVOIP
524  CheckVOIPError( this->session->Destroy() );
525  if (this->session)
526  delete this->session;
527  if (this->params)
528  delete this->params;
529  if (this->rtpparams)
530  delete this->rtpparams;
531 #endif /* NETCOMM_JVOIP */
532 #ifdef NETCOMM_WEBCAM
533  if (this->Webcam)
534  this->Webcam->EndCapture();
535  if (bufitem) {
536  delete bufitem;
537  bufitem == NULL;
538  }
539 #endif /* NETCOMM_NOWEBCAM */
540 
541  return 0;
542 }
char * NetworkCommunication::GetWebcamCapture ( )

Definition at line 571 of file networkcomm.cpp.

Referenced by NetClient::getWebcamCapture().

572 {
573 #ifdef NETCOMM_WEBCAM
574  if (Webcam != NULL)
575  return Webcam->jpeg_buffer;
576 #endif
577  return NULL; //We have no choice :-/
578 }
char * NetworkCommunication::GetWebcamFromNetwork ( int length)

Definition at line 580 of file networkcomm.cpp.

References VsnetOSS::memcpy().

Referenced by NetClient::getWebcamFromNetwork().

581 {
582  char *wshot = NULL;
583 #ifdef NETCOMM_WEBCAM
584  if ( (*webcamClient) && bufitem ) {
585  if ( !bufitem->done() ) {
586  return NULL;
587  } else {
588  //Re add the buf to get another webcam shot
589  length = bufitem->getSize();
590  wshot = new char[bufitem->getSize()+1];
591  memcpy( wshot, bufitem->getBuffer().get(), bufitem->getSize() );
592  wshot[bufitem->getSize()] = 0;
593  _downloader->addItem( bufitem );
594  }
595  }
596 #endif
597  return wshot;
598 }
char NetworkCommunication::HasPortaudio ( )

Definition at line 253 of file networkcomm.cpp.

Referenced by NetClient::startCommunication().

254 {
255 #ifdef NETCOMM_PORTAUDIO
256  return 1;
257 
258 #else
259  return 0;
260 #endif
261 }
char NetworkCommunication::HasWebcam ( )

Definition at line 241 of file networkcomm.cpp.

Referenced by NetClient::hasWebcam(), and NetClient::startCommunication().

242 {
243 #ifdef NETCOMM_WEBCAM
244  if (this->Webcam)
245  return 1;
246  return 0;
247 
248 #else
249  return 0;
250 #endif
251 }
int NetworkCommunication::InitSession ( float  frequency)

Definition at line 466 of file networkcomm.cpp.

References use_pa.

Referenced by NetClient::startCommunication().

467 {
468  //Init the VoIP session
469 #ifdef NETCOMM_JVOIP
470  this->session = new JVOIPSession;
471  this->session->SetSampleInterval( 100 );
472 
473  this->params = new JVOIPSessionParams;
474  this->rtpparams = new JVOIPRTPTransmissionParams;
475 
476  CheckVOIPError( this->session->Create( *(this->params) ) );
477 #endif /* NETCOMM_JVOIP */
478 
479 #ifdef NETCOMM_PORTAUDIO
480  if (use_pa) {
481  //Create the input stream with indev and 1 channel with paInt16 samples
482  CheckPAError( Pa_OpenStream( &this->instream,
483  this->indev, 1, paInt16, NULL,
484  paNoDevice, 0, paInt16, NULL,
485  this->sample_rate, 256, 0, paNoFlag,
486  Pa_RecordCallback, (void*) this ), "Pa_OpenStream(instream)" );
487 
488  //Create the output stream with outdev and 1 channel with paInt16 samples
489  CheckPAError( Pa_OpenStream( &this->outstream,
490  paNoDevice, 0, paInt16, NULL,
491  this->outdev, 1, paInt16, NULL,
492  this->sample_rate, 256, 0, paNoFlag,
493  Pa_PlayCallback, (void*) this ), "Pa_OpenStream(outstream)" );
494  if (this->instream)
495  CheckPAError( Pa_StartStream( this->instream ), "Pa_StartStream(instream)" );
496  if (this->outstream)
497  CheckPAError( Pa_StartStream( this->outstream ), "Pa_StartStream(outstream)" );
498  }
499 #endif /* NETCOMM_PORTAUDIO */
500 
501 #ifdef NETCOMM_WEBCAM
502  if (Webcam)
503  this->Webcam->StartCapture();
504 #endif /* NETCOMM_NOWEBCAM */
505 
506  this->active = true;
507  this->freq = frequency;
508  return 0;
509 }
bool NetworkCommunication::IsActive ( )
inline

Definition at line 127 of file networkcomm.h.

Referenced by NetClient::checkMsg(), NetClient::IsNetcommActive(), and NetClient::sendTextMessage().

128  {
129  return active;
130  }
char NetworkCommunication::IsSecured ( )
inline

Definition at line 139 of file networkcomm.h.

Referenced by NetClient::IsNetcommSecured(), and NetClient::startCommunication().

140  {
141  return secured;
142  }
float NetworkCommunication::MaxFreq ( )
inline

Definition at line 150 of file networkcomm.h.

Referenced by NetClient::decreaseFrequency(), and NetClient::increaseFrequency().

151  {
152  return this->max_freq;
153  }
float NetworkCommunication::MinFreq ( )
inline

Definition at line 146 of file networkcomm.h.

Referenced by NetClient::decreaseFrequency(), and NetClient::increaseFrequency().

147  {
148  return this->min_freq;
149  }
void NetworkCommunication::RecvMessage ( std::string  message,
bool  encrypted = false 
)

Definition at line 413 of file networkcomm.cpp.

References UniverseUtil::IOmessage().

414 {
415  //If max log size is reached we remove the oldest message
416  if (this->message_history.size() == this->max_messages)
417  this->message_history.pop_front();
418  string decrypted;
419 #ifdef CRYPTO
420  if (encrypted) {
421  decrypted = DecryptBuffer( message.c_str(), message.length() );
422  message = decrypted;
423  }
424 #endif
425 
426  this->message_history.push_back( message );
427  //Display message
428  string color;
429  if (secured)
430  color = "#DD0000";
431  else
432  color = "#99CCFF";
433  UniverseUtil::IOmessage( 0, "game", "all", color+message+"#000000" );
434 }
void NetworkCommunication::RecvSound ( const char *  sndbuffer,
int  length,
bool  encrypted = false 
)

Definition at line 395 of file networkcomm.cpp.

References MAXBUFFER, VsnetOSS::memcpy(), and use_pa.

396 {
397 #ifdef NETCOMM_PORTAUDIO
398  if (use_pa) {
399  string decrypted;
400  assert( length < MAXBUFFER );
401  memset( audio_outbuffer, 0, MAXBUFFER );
402 #ifdef CRYPTO
403  if (encrypted) {
404  decrypted = DecryptBuffer( sndbuffer, length );
405  memcpy( audio_outbuffer, decrypted.c_str(), decrypted.length() );
406  } else
407 #endif
408  memcpy( audio_outbuffer, sndbuffer, length );
409  }
410 #endif
411 }
void NetworkCommunication::RemoveFromSession ( ClientPtr  clt)

Definition at line 285 of file networkcomm.cpp.

References VOIP_PORT.

286 {
287  this->commClients.remove( clt );
288 #ifdef NETCOMM_JVOIP
289  CheckVOIPError( this->session->AddDestination( ntohl( clt->cltadr.inaddr() ), VOIP_PORT ) );
290 #endif /* NETCOMM_JVOIP */
291 }
void NetworkCommunication::SendMessage ( SOCKETALT send_sock,
ObjSerial  serial,
std::string  message 
)

Definition at line 299 of file networkcomm.cpp.

References CMD_SECMESSAGE, CMD_TXTMESSAGE, PSEUDO__LINE__, Packet::send(), SENDRELIABLE, and use_secured.

Referenced by NetClient::sendTextMessage().

300 {
301  Packet p;
302  string encrypted;
303  //If max log size is reached we remove the oldest message
304  if (this->message_history.size() == this->max_messages)
305  this->message_history.pop_front();
306  this->message_history.push_back( message );
307  //Send the text message according to the chosen method
308  if (method == ServerUnicast) {
309  //SEND STRNIG PARAMETER TO SERVER SO THAT HE BROADCASTS IT TO CONCERNED PLAYERS
310 #ifdef CRYPTO
311  if (this->secured && use_secured) {
312  encrypted = this->EncryptBuffer( message.c_str(), message.length() );
313  p.send( CMD_TXTMESSAGE, serial, encrypted.c_str(), message.length(), SENDRELIABLE, NULL, send_sock,
314  __FILE__, PSEUDO__LINE__( 244 ) );
315  } else
316 #endif
317  {
318  p.send( CMD_SECMESSAGE, serial, message.c_str(), message.length(), SENDRELIABLE, NULL, send_sock,
319  __FILE__, PSEUDO__LINE__( 244 ) );
320  }
321  } else if (method == ClientBroadcast) {
322  //SEND STRING PARAMETER TO EACH PLAYER COMMUNICATING ON THAT FREQUENCY
323  CltPtrIterator it;
324  for (it = commClients.begin(); it != commClients.end(); it++) {
325 #ifdef CRYPTO
326  if (this->secured && use_secured) {
327  encrypted = this->EncryptBuffer( message.c_str(), message.length() );
328  p.send( CMD_TXTMESSAGE, serial, encrypted.c_str(), message.length(), SENDRELIABLE, NULL, (*it)->tcp_sock,
329  __FILE__, PSEUDO__LINE__( 244 ) );
330  } else
331 #endif
332  {
333  p.send( CMD_SECMESSAGE, serial, message.c_str(), message.length(), SENDRELIABLE, NULL, (*it)->tcp_sock,
334  __FILE__, PSEUDO__LINE__( 244 ) );
335  }
336  }
337  }
338 }
void NetworkCommunication::SendSound ( SOCKETALT send_sock,
ObjSerial  serial 
)

Definition at line 346 of file networkcomm.cpp.

References CMD_SECSNDSAMPLE, CMD_SOUNDSAMPLE, MAXBUFFER, PSEUDO__LINE__, Packet::send(), SENDANDFORGET, use_pa, and use_secured.

Referenced by NetClient::checkMsg().

347 {
348 #ifdef USE_PORTAUDIO
349  string encrypted;
350  if (use_pa) {
351  Packet p;
352  if (method == ServerUnicast) {
353  //SEND INPUT AUDIO BUFFER TO SERVER SO THAT HE BROADCASTS IT TO CONCERNED PLAYERS
354  Packet p;
355  //We don't need that to be reliable in UDP mode
356 #ifdef CRYPTO
357  if (this->secured && use_secured) {
358  encrypted = this->EncryptBufer( this->audio_inbuffer, this->audio_inlength );
359  p.send( CMD_SOUNDSAMPLE, serial, encrypted.c_str(), encrypted.length(), SENDANDFORGET, NULL, sock,
360  __FILE__, PSEUDO__LINE__( 244 ) );
361  } else
362 #endif
363  {
364  p.send( CMD_SECSNDSAMPLE, serial, this->audio_inbuffer, this->audio_inlength, SENDANDFORGET, NULL, sock,
365  __FILE__, PSEUDO__LINE__( 244 ) );
366  }
367  } else if (method == ClientBroadcast) {
368  //SEND INPUT AUDIO BUFFER TO EACH PLAYER COMMUNICATING ON THAT FREQUENCY
369  CltPtrIterator it;
370  for (it = commClients.begin(); it != commClients.end(); it++) {
371  if ( (*it)->portaudio ) {
372 #ifdef CRYPTO
373  if (this->secured && use_secured) {
374  encrypted = this->EncryptBufer( this->audio_inbuffer, this->audio_inlength );
375  p.send( CMD_SOUNDSAMPLE, serial, encrypted.c_str(),
376  encrypted.length(), SENDANDFORGET, NULL, (*it)->sock,
377  __FILE__, PSEUDO__LINE__( 244 ) );
378  } else
379 #endif
380  {
381  p.send( CMD_SECSNDSAMPLE, serial, this->audio_inbuffer, this->audio_inlength, SENDANDFORGET, NULL,
382  (*it)->sock,
383  __FILE__, PSEUDO__LINE__(
384  244 ) );
385  }
386  }
387  }
388  }
389  memset( audio_inbuffer, 0, MAXBUFFER );
390  }
391 #endif /* USE_PORTAUDIO */
392 //Do not do anything when using JVoIP lib
393 }
void NetworkCommunication::StartWebcamTransfer ( )

Definition at line 611 of file networkcomm.cpp.

Referenced by NetClient::startWebcamTransfer().

612 {
613 #ifdef NETCOMM_WEBCAM
614  //To select the first webcam to display and begin transfers
615  this->SwitchWebcam();
616 #endif
617 }
void NetworkCommunication::StopWebcamTransfer ( )

Definition at line 600 of file networkcomm.cpp.

Referenced by NetClient::stopWebcamTransfer().

601 {
602 #ifdef NETCOMM_WEBCAM
603  if (bufitem)
604  //Abort current transfer
605  //- remove from dloadmgr
606  //- delete it
607  bufitem = NULL;
608 #endif
609 }
void NetworkCommunication::SwitchSecured ( )

Definition at line 652 of file networkcomm.cpp.

References use_secured.

Referenced by NetClient::switchSecured().

653 {
654 #ifdef CRYPTO
655  if (secured)
656  secured = 0;
657  else if (use_secured && crypt_key[0] != 0)
658  secured = 1;
659 #else
660  //If compiled without crypto++ support secured comms are not available
661  secured = 0;
662 #endif
663 }
void NetworkCommunication::SwitchWebcam ( )

Definition at line 619 of file networkcomm.cpp.

Referenced by NetClient::switchWebcam().

620 {
621 #ifdef NETCOMM_WEBCAM
622  bool found = false;
623  CltPtrIterator it;
624  if (bufitem)
625  delete bufitem;
626  //Go through listClients from current client to the end in order to find the next webcam client
627  if ( (*this->webcamClient) )
628  it = ++webcamClient;
629  else
630  it = commClients.begin();
631  for (; it != commClients.end() && !found; it++)
632  if ( (*it)->webcam )
633  found = true;
634  //If a client with webcam support was not found before the end we go through listClients from te beginning
635  if (!found) {
636  for (it = commClients.begin(), found = false; it != webcamClient && !found; it++)
637  if ( (*it)->webcam )
638  found = true;
639  //If we found another client supporting webcam
640  if (found)
641  webcamClient = it;
642  }
643  if (found)
644  //Should init transfer and buf with right values
645  //1. Create a socket to the interested client
646  //2. Init the bufitem
647  //bufitem = new VsnetDownload::Client::Buffer(...);
648  _downloader->addItem( bufitem );
649 #endif
650 }

The documentation for this class was generated from the following files: