Pol  Revision:cb584c9
clienttransmit.cpp
Go to the documentation of this file.
1 #include "clienttransmit.h"
2 
3 #include "../../clib/esignal.h"
4 #include "../../clib/rawtypes.h"
5 #include "../globals/network.h"
6 #include "../polsem.h"
7 #include "client.h"
8 
9 namespace Pol
10 {
11 namespace Network
12 {
13 ClientTransmit::ClientTransmit() : _transmitqueue() {}
14 
16 
18 {
20 }
21 void ClientTransmit::AddToQueue( Client* client, const void* data, int len )
22 {
23  const u8* message = static_cast<const u8*>( data );
24  auto transmitdata = TransmitDataSPtr( new TransmitData );
25  transmitdata->client = client->getWeakPtr();
26  transmitdata->len = len;
27  transmitdata->data.assign( message, message + len );
28  transmitdata->disconnects = false;
29  _transmitqueue.push_move( std::move( transmitdata ) );
30 }
31 
33 {
34  auto transmitdata = TransmitDataSPtr( new TransmitData );
35  transmitdata->disconnects = true;
36  transmitdata->client = client->getWeakPtr();
37  _transmitqueue.push_move( std::move( transmitdata ) );
38 }
39 
41 {
42  auto transmitdata = TransmitDataSPtr( new TransmitData );
43  transmitdata->remove = true;
44  transmitdata->client = client->getWeakPtr();
45  _transmitqueue.push_move( std::move( transmitdata ) );
46 }
47 
49 {
50  auto transmitdata = TransmitDataSPtr();
51  _transmitqueue.pop_wait( &transmitdata );
52  return transmitdata;
53 }
54 
56 {
57  ClientTransmit* transmit_instance = Core::networkManager.clientTransmit.get();
58  while ( !Clib::exit_signalled )
59  {
60  try
61  {
62  auto data = transmit_instance->NextQueueEntry();
63  if ( data->client.exists() )
64  {
65  if ( data->remove )
66  {
67  Core::PolLock lock;
68  Client::Delete( data->client.get_weakptr() );
69  }
70  else if ( data->disconnects )
71  {
72  data->client->forceDisconnect();
73  }
74  else if ( data->client->isReallyConnected() )
75  data->client->transmit( static_cast<void*>( &data->data[0] ), data->len, true );
76  }
77  }
78  catch ( ClientTransmitQueue::Canceled& )
79  {
80  return;
81  }
82  }
83 }
84 }
85 }
unsigned char u8
Definition: rawtypes.h:25
TransmitDataSPtr NextQueueEntry()
void ClientTransmitThread()
static void Delete(Client *client)
Definition: client.cpp:129
void AddToQueue(Client *client, const void *data, int len)
void pop_wait(Message *msg)
std::unique_ptr< Network::ClientTransmit > clientTransmit
Definition: network.h:97
std::unique_ptr< TransmitData > TransmitDataSPtr
NetworkManager networkManager
Definition: network.cpp:28
void QueueDelete(Client *client)
weak_ptr< Client > getWeakPtr() const
Definition: client.cpp:713
std::unordered_map< u64, ScriptDiffData > data
Definition: osmod.cpp:966
void push_move(Message &&msg)
Definition: message_queue.h:88
void QueueDisconnection(Client *client)
ClientTransmitQueue _transmitqueue
Definition: berror.cpp:12
std::atomic< bool > exit_signalled