diff options
author | madmaxoft@gmail.com <madmaxoft@gmail.com@0a769ca7-a7f5-676a-18bf-c427514a06d6> | 2013-03-04 22:13:08 +0100 |
---|---|---|
committer | madmaxoft@gmail.com <madmaxoft@gmail.com@0a769ca7-a7f5-676a-18bf-c427514a06d6> | 2013-03-04 22:13:08 +0100 |
commit | 28d8d8419a5b900e9d20ce91dc63e28349b6470a (patch) | |
tree | bf5b7bafa902a138339c78877ecec376c055b727 /source/Server.cpp | |
parent | Removed the unused cHeartbeat object (diff) | |
download | cuberite-28d8d8419a5b900e9d20ce91dc63e28349b6470a.tar cuberite-28d8d8419a5b900e9d20ce91dc63e28349b6470a.tar.gz cuberite-28d8d8419a5b900e9d20ce91dc63e28349b6470a.tar.bz2 cuberite-28d8d8419a5b900e9d20ce91dc63e28349b6470a.tar.lz cuberite-28d8d8419a5b900e9d20ce91dc63e28349b6470a.tar.xz cuberite-28d8d8419a5b900e9d20ce91dc63e28349b6470a.tar.zst cuberite-28d8d8419a5b900e9d20ce91dc63e28349b6470a.zip |
Diffstat (limited to '')
-rw-r--r-- | source/Server.cpp | 139 |
1 files changed, 46 insertions, 93 deletions
diff --git a/source/Server.cpp b/source/Server.cpp index 1e830874e..3fcaa6e8e 100644 --- a/source/Server.cpp +++ b/source/Server.cpp @@ -62,14 +62,10 @@ typedef std::list< cClientHandle* > ClientList; struct cServer::sServerState { sServerState() - : pListenThread( 0 ) - , pTickThread( 0 ) - , bStopListenThread( false ) - , bStopTickThread( false ) + : pTickThread(NULL) + , bStopTickThread(false) {} - cSocket SListenClient; // socket listening for client calls - cThread* pListenThread; bool bStopListenThread; cThread* pTickThread; bool bStopTickThread; cEvent RestartEvent; @@ -80,21 +76,6 @@ struct cServer::sServerState -void cServer::ServerListenThread( void *a_Args ) -{ - LOG("ServerListenThread"); - cServer* self = (cServer*)a_Args; - sServerState* m_pState = self->m_pState; - while( !m_pState->bStopListenThread ) - { - self->StartListenClient(); - } -} - - - - - void cServer::ClientDestroying(const cClientHandle * a_Client) { m_SocketThreads.StopReading(a_Client); @@ -172,36 +153,13 @@ bool cServer::InitServer(cIniFile & a_SettingsIni) return false; } - m_pState->SListenClient = cSocket::CreateSocket(); - - if( !m_pState->SListenClient.IsValid() ) - { - LOGERROR("m_SListenClient==INVALID_SOCKET (%s)", cSocket::GetErrorString( cSocket::GetLastError() ).c_str() ); - return false; - } - - if( m_pState->SListenClient.SetReuseAddress() == -1 ) + AString Ports = a_SettingsIni.GetValueSet("Server", "Port", "25565"); + m_ListenThread.SetReuseAddr(true); + if (!m_ListenThread.Initialize(Ports)) { - LOGERROR("setsockopt == -1"); return false; } - int Port = a_SettingsIni.GetValueSetI("Server", "Port", 25565); - - if (m_pState->SListenClient.BindToAny(Port) != 0) - { - LOGERROR("bind fail (%s)", cSocket::GetErrorString( cSocket::GetLastError() ).c_str() ); - return false; - } - - if( m_pState->SListenClient.Listen( 10 ) != 0) - { - LOGERROR("listen fail (%s)", cSocket::GetErrorString( cSocket::GetLastError() ).c_str() ); - return false; - } - - m_iServerPort = Port; - LOG("Port %i has been bound", m_iServerPort); m_bIsConnected = true; m_pState->ServerID = "-"; @@ -241,13 +199,13 @@ bool cServer::InitServer(cIniFile & a_SettingsIni) -cServer::cServer() - : m_pState( new sServerState ) - , m_Millisecondsf( 0 ) - , m_Milliseconds( 0 ) - , m_bIsConnected( false ) - , m_iServerPort( 0 ) - , m_bRestarting( false ) +cServer::cServer(void) + : m_pState(new sServerState) + , m_ListenThread(*this) + , m_Millisecondsf(0) + , m_Milliseconds(0) + , m_bIsConnected(false) + , m_bRestarting(false) { } @@ -258,14 +216,6 @@ cServer::cServer() cServer::~cServer() { // TODO: Shut down the server gracefully - if ( m_pState->SListenClient ) - { - m_pState->SListenClient.CloseSocket(); - } - m_pState->SListenClient = 0; - - m_pState->bStopListenThread = true; - delete m_pState->pListenThread; m_pState->pListenThread = NULL; m_pState->bStopTickThread = true; delete m_pState->pTickThread; m_pState->pTickThread = NULL; @@ -295,54 +245,52 @@ void cServer::PrepareKeys(void) -void cServer::BroadcastChat(const AString & a_Message, const cClientHandle * a_Exclude) -{ - cCSLock Lock(m_CSClients); - for (ClientList::iterator itr = m_Clients.begin(); itr != m_Clients.end(); ++itr) - { - if ((*itr == a_Exclude) || !(*itr)->IsLoggedIn()) - { - continue; - } - (*itr)->SendChat(a_Message); - } -} - - - - - -void cServer::StartListenClient() +void cServer::OnConnectionAccepted(cSocket & a_Socket) { - cSocket SClient = m_pState->SListenClient.Accept(); - - if (!SClient.IsValid()) + if (!a_Socket.IsValid()) { return; } - const AString & ClientIP = SClient.GetIPString(); + const AString & ClientIP = a_Socket.GetIPString(); if (ClientIP.empty()) { LOGWARN("cServer: A client connected, but didn't present its IP, disconnecting."); - SClient.CloseSocket(); + a_Socket.CloseSocket(); return; } LOG("Client \"%s\" connected!", ClientIP.c_str()); - cClientHandle * NewHandle = new cClientHandle(&SClient, m_ClientViewDistance); - if (!m_SocketThreads.AddClient(SClient, NewHandle)) + cClientHandle * NewHandle = new cClientHandle(&a_Socket, m_ClientViewDistance); + if (!m_SocketThreads.AddClient(a_Socket, NewHandle)) { // For some reason SocketThreads have rejected the handle, clean it up - LOGERROR("Client \"%s\" cannot be handled, server probably unstable", SClient.GetIPString().c_str()); - SClient.CloseSocket(); + LOGERROR("Client \"%s\" cannot be handled, server probably unstable", ClientIP.c_str()); + a_Socket.CloseSocket(); delete NewHandle; return; } cCSLock Lock(m_CSClients); - m_Clients.push_back( NewHandle ); + m_Clients.push_back(NewHandle); +} + + + + + +void cServer::BroadcastChat(const AString & a_Message, const cClientHandle * a_Exclude) +{ + cCSLock Lock(m_CSClients); + for (ClientList::iterator itr = m_Clients.begin(); itr != m_Clients.end(); ++itr) + { + if ((*itr == a_Exclude) || !(*itr)->IsLoggedIn()) + { + continue; + } + (*itr)->SendChat(a_Message); + } } @@ -434,12 +382,15 @@ void ServerTickThread( void * a_Param ) -void cServer::StartListenThread() +bool cServer::Start(void) { - m_pState->pListenThread = new cThread( ServerListenThread, this, "cServer::ServerListenThread" ); m_pState->pTickThread = new cThread( ServerTickThread, this, "cServer::ServerTickThread" ); - m_pState->pListenThread->Start( true ); + if (!m_ListenThread.Start()) + { + return false; + } m_pState->pTickThread->Start( true ); + return true; } @@ -532,6 +483,8 @@ void cServer::SendMessage(const AString & a_Message, cPlayer * a_Player /* = NUL void cServer::Shutdown() { + m_ListenThread.Stop(); + m_bRestarting = true; m_pState->RestartEvent.Wait(); |