Pol  Revision:f37d500
network.cpp
Go to the documentation of this file.
1 #include "network.h"
2 
3 #include <curl/curl.h>
4 #include <string.h>
5 
6 #include "../../clib/logfacility.h"
7 #include "../../clib/stlutil.h"
8 #include "../../clib/threadhelp.h"
9 #include "../../plib/systemstate.h"
10 #include "../accounts/account.h"
11 #include "../mobile/charactr.h"
12 #include "../network/auxclient.h"
13 #include "../network/clienttransmit.h"
14 #include "../network/cliface.h"
15 #include "../network/msgfiltr.h"
16 #include "../network/msghandl.h"
17 #include "../network/packethooks.h"
18 #include "../network/packetinterface.h"
19 #include "../servdesc.h"
20 #include "../sockio.h"
21 #include "../sqlscrobj.h"
22 #include "../uoclient.h"
23 
24 namespace Pol
25 {
26 namespace Core
27 {
29 
31  : clients(),
32  servers(),
33  polstats(),
34 #ifdef HAVE_MYSQL
35  sql_service( new SQLService ),
36 #endif
37  uo_client_interface( new Network::UOClientInterface() ),
38  auxservices(),
39  uoclient_general(),
40  uoclient_protocol(),
41  uoclient_listeners(),
42  iostats(),
43  queuedmode_iostats(),
44  login_filter( nullptr ),
45  game_filter( nullptr ),
46  disconnected_filter( nullptr ),
47  packet_hook_data(),
48  packet_hook_data_v2(),
49  handler(),
50  handler_v2(),
51  ext_handler_table(),
52  packetsSingleton( new Network::PacketsSingleton() ),
53  clientTransmit( new Network::ClientTransmit() ),
54  auxthreadpool( new threadhelp::DynTaskThreadPool( "AuxPool" ) ), // TODO: seems to work
55  // activate by default?
56  // maybe add a cfg entry for
57  // max number of threads
58  banned_ips(),
59  polsocket()
60 {
61  memset( ipaddr_str, 0, sizeof ipaddr_str );
62  memset( lanaddr_str, 0, sizeof lanaddr_str );
63  memset( hostname, 0, sizeof hostname );
67 
69 
70  curl_global_init( CURL_GLOBAL_DEFAULT );
71 }
72 
75 {
76  Clients::iterator itr = clients.begin();
77  while ( itr != clients.end() )
78  {
79  Network::Client* client = *itr;
80  if ( !client->isReallyConnected() )
81  {
82  fmt::Writer tmp;
83  tmp.Format( "Disconnecting Client#{} ({}/{})" )
84  << client->instance_ << ( client->acct ? client->acct->name() : "[no account]" )
85  << ( client->chr ? client->chr->name() : "[no character]" );
86  ERROR_PRINT << tmp.str() << "\n";
87  if ( Plib::systemstate.config.loglevel >= 4 )
88  POLLOG << tmp.str() << "\n";
89 
90  Network::Client::Delete( client );
91  client = NULL;
92  itr = clients.erase( itr );
93  }
94  else
95  {
96  ++itr;
97  }
98  }
99 }
100 
102 {
103  for ( auto& client : clients )
104  {
105  client->forceDisconnect();
106  }
108  for ( auto& client : clients )
109  {
110  Network::Client::Delete( client );
111  }
112  clients.clear();
113 
115 
116  // unload_aux_service
118  auxthreadpool.reset();
119  banned_ips.clear();
120 #ifdef _WIN32
121  closesocket( polsocket.listen_socket );
122 #else
123  close( polsocket.listen_socket ); // shutdown( polsocket.listen_socket, 2 ); ??
124 #endif
127  curl_global_cleanup();
129 }
130 
132 {
133  Memory usage;
134  memset( &usage, 0, sizeof( usage ) );
135  usage.misc = sizeof( NetworkManager );
136 
137  usage.client_size =
138  3 * sizeof( Network::Client** ) + clients.capacity() * sizeof( Network::Client* );
139  usage.client_count = clients.size();
140  for ( const auto& client : clients )
141  {
142  if ( client != nullptr )
143  usage.client_size += client->estimatedSize();
144  }
145 
146  usage.misc +=
147  3 * sizeof( ServerDescription** ) + servers.capacity() * sizeof( ServerDescription* );
148  for ( const auto& server : servers )
149  if ( server != nullptr )
150  usage.misc += server->estimateSize();
151 
152 #ifdef HAVE_MYSQL
153  usage.misc += sizeof( SQLService ); /* sql_service */
154 #endif
155  usage.misc += sizeof( Network::UOClientInterface ); /*uo_client_interface*/
156  usage.misc +=
157  3 * sizeof( Network::AuxService** ) + auxservices.capacity() * sizeof( Network::AuxService* );
158  for ( const auto& aux : auxservices )
159  if ( aux != nullptr )
160  usage.misc += aux->estimateSize();
161 
164  usage.misc += 3 * sizeof( UoClientListener* );
165  for ( const auto& listener : uoclient_listeners )
166  usage.misc += listener.estimateSize();
167 
168  usage.misc +=
169  3 * ( sizeof( MessageTypeFilter ) ); /*login_filter, game_filter, disconnected_filter*/
170 
171  usage.misc += 3 * sizeof( std::unique_ptr<Network::PacketHookData>* ) +
172  packet_hook_data.capacity() * sizeof( std::unique_ptr<Network::PacketHookData> );
173  usage.misc += 3 * sizeof( std::unique_ptr<Network::PacketHookData>* ) +
174  packet_hook_data_v2.capacity() * sizeof( std::unique_ptr<Network::PacketHookData> );
175  for ( const auto& hook : packet_hook_data )
176  {
177  if ( hook != nullptr )
178  usage.misc += hook->estimateSize();
179  }
180  for ( const auto& hook : packet_hook_data_v2 )
181  {
182  if ( hook != nullptr )
183  usage.misc += hook->estimateSize();
184  }
185 
186  usage.misc += packetsSingleton->estimateSize();
187  usage.misc += sizeof( Network::ClientTransmit );
188  usage.misc += sizeof( threadhelp::DynTaskThreadPool );
189 
190  usage.misc += 3 * sizeof( Network::IPRule* ) + banned_ips.capacity() * sizeof( Network::IPRule );
191 
192  return usage;
193 }
194 }
195 }
std::unique_ptr< Network::PacketsSingleton > packetsSingleton
Definition: network.h:95
void delete_all(T &coll)
Definition: stlutil.h:24
static void createMessageFilter()
Definition: msgfiltr.cpp:43
SystemState systemstate
Definition: systemstate.cpp:12
Accounts::Account * acct
Definition: client.h:181
std::vector< Network::AuxService * > auxservices
Definition: network.h:65
static void Delete(Client *client)
Definition: client.cpp:129
int deinit_sockets_library(void)
Definition: sockio.cpp:116
Mobile::Character * chr
Definition: client.h:182
std::vector< std::unique_ptr< Network::PacketHookData > > packet_hook_data_v2
Definition: network.h:79
std::unique_ptr< threadhelp::DynTaskThreadPool > auxthreadpool
Definition: network.h:99
Memory estimateSize() const
Definition: network.cpp:131
NetworkManager networkManager
Definition: network.cpp:28
std::vector< std::unique_ptr< Network::PacketHookData > > packet_hook_data
Definition: network.h:78
const char * name() const
Definition: account.cpp:193
void hook(ExportScript *shs, const std::string &hookname, const std::string &exfuncname)
Definition: syshook.cpp:111
std::vector< Network::IPRule > banned_ips
Definition: network.h:101
#define POLLOG
Definition: logfacility.h:219
void kill_disconnected_clients()
Definition: network.cpp:74
size_t estimateSize() const
Definition: uoclient.cpp:26
UoClientProtocol uoclient_protocol
Definition: network.h:68
bool isReallyConnected() const
Definition: client.h:295
static void initialize_msg_handlers()
Definition: msghandl.cpp:125
size_t estimateSize() const
Definition: uoclient.cpp:137
#define ERROR_PRINT
Definition: logfacility.h:230
std::vector< UoClientListener > uoclient_listeners
Definition: network.h:69
virtual std::string name() const
Definition: uobject.cpp:196
Definition: berror.cpp:12
Network::PolSocket polsocket
Definition: network.h:103
void clean_packethooks()
unsigned int instance_
Definition: client.h:253
static void initializeGameData(std::vector< std::unique_ptr< PacketHookData >> *data)
UoClientGeneral uoclient_general
Definition: network.h:67