Pol  Revision:cb584c9
uolisten.cpp
Go to the documentation of this file.
1 
8 #include <string.h>
9 #include <string>
10 
11 #include <format/format.h>
12 #include "../clib/compilerspecifics.h"
13 #include "../clib/esignal.h"
14 #include "../clib/logfacility.h"
15 #include "../clib/socketsvc.h"
16 #include "../clib/stlutil.h"
17 #include "../clib/threadhelp.h"
18 #include "../clib/wnsckt.h"
19 #include "../plib/systemstate.h"
20 #include "core.h"
21 #include "globals/network.h"
22 #include "network/client.h"
23 #include "network/cliface.h"
24 #include "polsem.h"
25 #include "uoclient.h"
26 
27 namespace Pol
28 {
29 namespace Core
30 {
32 {
33 public:
35  : Clib::SocketClientThread( SL ), _def( *def ), client( nullptr )
36  {
37  }
39  : Clib::SocketClientThread( copy._sck ), _def( copy._def ), client( copy.client ){};
40  virtual void run() POL_OVERRIDE;
41  void create();
42  virtual ~UoClientThread(){};
43 
44 private:
46 
47 public:
49 };
50 
51 bool client_io_thread( Network::Client* client, bool login = false );
52 
54 {
55  if ( !Plib::systemstate.config.use_single_thread_login )
56  {
57  create();
58  }
61 }
62 
64 {
65  {
66  if ( Plib::systemstate.config.disable_nagle )
67  {
69  }
70  struct sockaddr client_addr = _sck.peer_address();
71  struct sockaddr host_addr;
72  socklen_t host_addrlen = sizeof host_addr;
73 
74  PolLock lck;
75  client =
76  new Network::Client( *Core::networkManager.uo_client_interface.get(), _def.encryption );
77  client->csocket = _sck.release_handle(); // client cleans up its socket.
78  if ( _def.sticky )
80  if ( _def.aosresist )
81  client->aosresist = true; // UOCLient.cfg Entry
82  // Added null setting for pre-char selection checks using nullptr validation
83  client->acct = nullptr;
84  memcpy( &client->ipaddr, &client_addr, sizeof client->ipaddr );
85 
86  networkManager.clients.push_back( client );
87  CoreSetSysTrayToolTip( Clib::tostring( networkManager.clients.size() ) + " clients connected",
89  fmt::Writer tmp;
90  tmp.Format( "Client#{} connected from {} ({} connections)" )
91  << client->instance_ << Network::AddressToString( &client_addr )
92  << networkManager.clients.size();
93  if ( getsockname( client->csocket, &host_addr, &host_addrlen ) == 0 )
94  {
95  tmp << " on interface " << Network::AddressToString( &host_addr );
96  }
97  POLLOG << tmp.str() << "\n";
98  }
99 }
100 
101 
102 void uo_client_listener_thread( void* arg )
103 {
104  UoClientListener* ls = static_cast<UoClientListener*>( arg );
105  INFO_PRINT << "Listening for UO clients on port " << ls->port
106  << " (encryption: " << ls->encryption.eType << ",0x"
107  << fmt::hexu( ls->encryption.uiKey1 ) << ",0x" << fmt::hexu( ls->encryption.uiKey2 )
108  << ")\n";
109 
112  std::list<std::unique_ptr<UoClientThread>> login_clients;
113  while ( !Clib::exit_signalled )
114  {
115  unsigned int timeout = 2;
116  if ( !login_clients.empty() )
117  timeout = 0;
118  if ( SL.GetConnection( timeout ) )
119  {
120  // create an appropriate Client object
121  if ( Plib::systemstate.config.use_single_thread_login )
122  {
123  std::unique_ptr<UoClientThread> thread( new UoClientThread( ls, SL ) );
124  thread->create();
125  client_io_thread( thread->client, true );
126  login_clients.push_back( std::move( thread ) );
127  }
128  else
129  {
130  Clib::SocketClientThread* thread = new UoClientThread( ls, SL );
131  thread->start();
132  }
133  }
134 
135  auto itr = login_clients.begin();
136  while ( itr != login_clients.end() )
137  {
138  if ( ( *itr )->client != nullptr && ( *itr )->client->isReallyConnected() )
139  {
140  if ( !client_io_thread( ( *itr )->client, true ) )
141  {
142  itr = login_clients.erase( itr );
143  continue;
144  }
145 
146  if ( ( *itr )->client->isConnected() && ( *itr )->client->chr )
147  {
148  Clib::SocketClientThread::start_thread( itr->release() );
149  itr = login_clients.erase( itr );
150  }
151  else
152  {
153  ++itr;
154  }
155  }
156  else
157  {
158  itr = login_clients.erase( itr );
159  }
160  }
161  }
162 }
163 
165 {
166  for ( unsigned i = 0; i < networkManager.uoclient_listeners.size(); ++i )
167  {
169  std::string threadname = "UO Client Listener Port " + Clib::tostring( ls->port );
170  threadhelp::start_thread( uo_client_listener_thread, threadname.c_str(), ls );
171  }
172 }
173 }
174 }
SocketClientThread(SocketListener &SL)
Definition: socketsvc.cpp:49
static void start_thread(SocketClientThread *instance)
Definition: socketsvc.cpp:66
#define POL_OVERRIDE
ECryptType eType
Definition: cryptkey.h:33
SystemState systemstate
Definition: systemstate.cpp:12
sockaddr ipaddr
Definition: client.h:213
Accounts::Account * acct
Definition: client.h:181
bool client_io_thread(Network::Client *client, bool login)
size_t thread_pid()
Definition: threadhelp.cpp:107
unsigned int uiKey1
Definition: cryptkey.h:31
Crypt::TCryptInfo encryption
Definition: uoclient.h:73
UoClientThread(UoClientListener *def, Clib::SocketListener &SL)
Definition: uolisten.cpp:34
void disable_nagle()
Definition: wnsckt.cpp:164
unsigned short port
Definition: uoclient.h:74
void start_thread(void(*entry)(void *), const char *thread_name, void *arg)
Definition: threadhelp.cpp:233
UoClientListener _def
Definition: uolisten.cpp:42
virtual void run() POL_OVERRIDE
Definition: uolisten.cpp:53
void CoreSetSysTrayToolTip(const std::string &text, Priority priority)
Definition: core.cpp:36
void start_uo_client_listeners(void)
Definition: uolisten.cpp:164
unsigned int uiKey2
Definition: cryptkey.h:32
NetworkManager networkManager
Definition: network.cpp:28
std::string tostring(const Bscript::BTokenType &v)
Definition: tokens.cpp:19
const char * AddressToString(struct sockaddr *addr)
Definition: sockio.cpp:212
#define POLLOG
Definition: logfacility.h:219
Network::Client * client
Definition: uolisten.cpp:48
UoClientThread(UoClientThread &copy)
Definition: uolisten.cpp:38
unsigned short listen_port
Definition: client.h:194
struct sockaddr peer_address() const
Definition: wnsckt.cpp:107
SOCKET release_handle()
Definition: wnsckt.cpp:117
void uo_client_listener_thread(void *arg)
Definition: uolisten.cpp:102
std::vector< UoClientListener > uoclient_listeners
Definition: network.h:69
#define INFO_PRINT
Definition: logfacility.h:223
bool GetConnection(unsigned int timeout_sec)
Definition: socketsvc.cpp:38
Definition: berror.cpp:12
std::atomic< bool > exit_signalled
unsigned int instance_
Definition: client.h:253