git-svn-id: http://mc-server.googlecode.com/svn/trunk@891 0a769ca7-a7f5-676a-18bf-c427514a06d6
This commit is contained in:
parent
24e94e7516
commit
7a43f6be09
@ -75,9 +75,9 @@ int cClientHandle::s_ClientCount = 0;
|
|||||||
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||||
// cClientHandle:
|
// cClientHandle:
|
||||||
|
|
||||||
cClientHandle::cClientHandle(const cSocket & a_Socket, int a_ViewDistance)
|
cClientHandle::cClientHandle(const cSocket * a_Socket, int a_ViewDistance)
|
||||||
: m_ViewDistance(a_ViewDistance)
|
: m_ViewDistance(a_ViewDistance)
|
||||||
, m_Socket(a_Socket)
|
, m_IPString(a_Socket->GetIPString())
|
||||||
, m_OutgoingData(64 KiB)
|
, m_OutgoingData(64 KiB)
|
||||||
, m_bDestroyed(false)
|
, m_bDestroyed(false)
|
||||||
, m_Player(NULL)
|
, m_Player(NULL)
|
||||||
@ -138,12 +138,9 @@ cClientHandle::~cClientHandle()
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (m_Socket.IsValid())
|
if (!m_bKicking)
|
||||||
{
|
{
|
||||||
if (!m_bKicking)
|
SendDisconnect("Server shut down? Kthnxbai");
|
||||||
{
|
|
||||||
SendDisconnect("Server shut down? Kthnxbai");
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (m_Player != NULL)
|
if (m_Player != NULL)
|
||||||
@ -158,15 +155,12 @@ cClientHandle::~cClientHandle()
|
|||||||
AString Data;
|
AString Data;
|
||||||
m_OutgoingData.ReadAll(Data);
|
m_OutgoingData.ReadAll(Data);
|
||||||
m_OutgoingData.CommitRead();
|
m_OutgoingData.CommitRead();
|
||||||
cRoot::Get()->GetServer()->WriteToClient(&m_Socket, Data);
|
cRoot::Get()->GetServer()->WriteToClient(this, Data);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Queue the socket to close as soon as it sends all outgoing data:
|
// Queue the socket to close as soon as it sends all outgoing data:
|
||||||
cRoot::Get()->GetServer()->QueueClientClose(&m_Socket);
|
cRoot::Get()->GetServer()->QueueClientClose(this);
|
||||||
|
cRoot::Get()->GetServer()->RemoveClient(this);
|
||||||
// We need to remove the socket from SocketThreads because we own it and it gets destroyed after this destructor finishes
|
|
||||||
// TODO: The socket needs to stay alive, someone else has to own it
|
|
||||||
cRoot::Get()->GetServer()->RemoveClient(&m_Socket);
|
|
||||||
|
|
||||||
delete m_Protocol;
|
delete m_Protocol;
|
||||||
m_Protocol = NULL;
|
m_Protocol = NULL;
|
||||||
@ -234,7 +228,7 @@ void cClientHandle::Authenticate(void)
|
|||||||
m_Player->LoginSetGameMode(World->GetGameMode());
|
m_Player->LoginSetGameMode(World->GetGameMode());
|
||||||
}
|
}
|
||||||
|
|
||||||
m_Player->SetIP (m_Socket.GetIPString());
|
m_Player->SetIP (m_IPString);
|
||||||
|
|
||||||
cRoot::Get()->GetPluginManager()->CallHook(cPluginManager::HOOK_PLAYER_SPAWN, 1, m_Player);
|
cRoot::Get()->GetPluginManager()->CallHook(cPluginManager::HOOK_PLAYER_SPAWN, 1, m_Player);
|
||||||
|
|
||||||
@ -1560,10 +1554,8 @@ void cClientHandle::AddWantedChunk(int a_ChunkX, int a_ChunkZ)
|
|||||||
void cClientHandle::PacketBufferFull(void)
|
void cClientHandle::PacketBufferFull(void)
|
||||||
{
|
{
|
||||||
// Too much data in the incoming queue, the server is probably too busy, kick the client:
|
// Too much data in the incoming queue, the server is probably too busy, kick the client:
|
||||||
LOGERROR("Too much data in queue for client \"%s\" @ %s, kicking them.", m_Username.c_str(), m_Socket.GetIPString().c_str());
|
LOGERROR("Too much data in queue for client \"%s\" @ %s, kicking them.", m_Username.c_str(), m_IPString.c_str());
|
||||||
SendDisconnect("Server busy");
|
SendDisconnect("Server busy");
|
||||||
// TODO: QueueDestroy();
|
|
||||||
cSleep::MilliSleep(1000); // Give packet some time to be received
|
|
||||||
Destroy();
|
Destroy();
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -1573,13 +1565,11 @@ void cClientHandle::PacketBufferFull(void)
|
|||||||
|
|
||||||
void cClientHandle::PacketUnknown(unsigned char a_PacketType)
|
void cClientHandle::PacketUnknown(unsigned char a_PacketType)
|
||||||
{
|
{
|
||||||
LOGERROR("Unknown packet type 0x%02x from client \"%s\" @ %s", a_PacketType, m_Username.c_str(), m_Socket.GetIPString().c_str());
|
LOGERROR("Unknown packet type 0x%02x from client \"%s\" @ %s", a_PacketType, m_Username.c_str(), m_IPString.c_str());
|
||||||
|
|
||||||
AString Reason;
|
AString Reason;
|
||||||
Printf(Reason, "[C->S] Unknown PacketID: 0x%02x", a_PacketType);
|
Printf(Reason, "[C->S] Unknown PacketID: 0x%02x", a_PacketType);
|
||||||
SendDisconnect(Reason);
|
SendDisconnect(Reason);
|
||||||
// TODO: QueueDestroy();
|
|
||||||
cSleep::MilliSleep(1000); // Give packet some time to be received
|
|
||||||
Destroy();
|
Destroy();
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -1591,8 +1581,6 @@ void cClientHandle::PacketError(unsigned char a_PacketType)
|
|||||||
{
|
{
|
||||||
LOGERROR("Protocol error while parsing packet type 0x%02x; disconnecting client \"%s\"", a_PacketType, m_Username.c_str());
|
LOGERROR("Protocol error while parsing packet type 0x%02x; disconnecting client \"%s\"", a_PacketType, m_Username.c_str());
|
||||||
SendDisconnect("Protocol error");
|
SendDisconnect("Protocol error");
|
||||||
// TODO: QueueDestroy();
|
|
||||||
cSleep::MilliSleep(1000); // Give packet some time to be received
|
|
||||||
Destroy();
|
Destroy();
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -1637,11 +1625,8 @@ void cClientHandle::SocketClosed(void)
|
|||||||
{
|
{
|
||||||
// The socket has been closed for any reason
|
// The socket has been closed for any reason
|
||||||
|
|
||||||
// TODO
|
LOG("Client \"%s\" @ %s disconnected", m_Username.c_str(), m_IPString.c_str());
|
||||||
/*
|
Destroy();
|
||||||
self->Destroy();
|
|
||||||
LOG("Client \"%s\" disconnected", GetLogName().c_str());
|
|
||||||
*/
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -50,11 +50,10 @@ public:
|
|||||||
static const int MAX_VIEW_DISTANCE = 10;
|
static const int MAX_VIEW_DISTANCE = 10;
|
||||||
static const int MIN_VIEW_DISTANCE = 4;
|
static const int MIN_VIEW_DISTANCE = 4;
|
||||||
|
|
||||||
cClientHandle(const cSocket & a_Socket, int a_ViewDistance);
|
cClientHandle(const cSocket * a_Socket, int a_ViewDistance);
|
||||||
~cClientHandle();
|
virtual ~cClientHandle();
|
||||||
|
|
||||||
cSocket & GetSocket (void) { return m_Socket; }
|
const AString & GetIPString(void) const { return m_IPString; }
|
||||||
const AString & GetIPString(void) const { return m_Socket.GetIPString(); }
|
|
||||||
|
|
||||||
cPlayer* GetPlayer() { return m_Player; } // tolua_export
|
cPlayer* GetPlayer() { return m_Player; } // tolua_export
|
||||||
|
|
||||||
@ -169,6 +168,8 @@ private:
|
|||||||
int m_ViewDistance; // Number of chunks the player can see in each direction; 4 is the minimum ( http://wiki.vg/Protocol_FAQ#.E2.80.A6all_connecting_clients_spasm_and_jerk_uncontrollably.21 )
|
int m_ViewDistance; // Number of chunks the player can see in each direction; 4 is the minimum ( http://wiki.vg/Protocol_FAQ#.E2.80.A6all_connecting_clients_spasm_and_jerk_uncontrollably.21 )
|
||||||
|
|
||||||
static const int GENERATEDISTANCE = 2; // Server generates this many chunks AHEAD of player sight. 2 is the minimum, since foliage is generated 1 step behind chunk terrain generation
|
static const int GENERATEDISTANCE = 2; // Server generates this many chunks AHEAD of player sight. 2 is the minimum, since foliage is generated 1 step behind chunk terrain generation
|
||||||
|
|
||||||
|
AString m_IPString;
|
||||||
|
|
||||||
int m_ProtocolVersion;
|
int m_ProtocolVersion;
|
||||||
AString m_Username;
|
AString m_Username;
|
||||||
@ -178,7 +179,6 @@ private:
|
|||||||
cChunkCoordsList m_LoadedChunks; // Chunks that the player belongs to
|
cChunkCoordsList m_LoadedChunks; // Chunks that the player belongs to
|
||||||
cChunkCoordsList m_ChunksToSend; // Chunks that need to be sent to the player (queued because they weren't generated yet or there's not enough time to send them)
|
cChunkCoordsList m_ChunksToSend; // Chunks that need to be sent to the player (queued because they weren't generated yet or there's not enough time to send them)
|
||||||
|
|
||||||
cSocket m_Socket;
|
|
||||||
cProtocol * m_Protocol;
|
cProtocol * m_Protocol;
|
||||||
|
|
||||||
cCriticalSection m_CSOutgoingData;
|
cCriticalSection m_CSOutgoingData;
|
||||||
|
@ -46,6 +46,12 @@ Usage:
|
|||||||
class cFile
|
class cFile
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
|
#ifdef _WIN32
|
||||||
|
static const char PathSeparator = '\\';
|
||||||
|
#else
|
||||||
|
static const char PathSeparator = '/';
|
||||||
|
#endif
|
||||||
|
|
||||||
/// The mode in which to open the file
|
/// The mode in which to open the file
|
||||||
enum EMode
|
enum EMode
|
||||||
{
|
{
|
||||||
|
@ -37,7 +37,7 @@ cSocketThreads::~cSocketThreads()
|
|||||||
|
|
||||||
|
|
||||||
|
|
||||||
bool cSocketThreads::AddClient(cSocket * a_Socket, cCallback * a_Client)
|
bool cSocketThreads::AddClient(const cSocket & a_Socket, cCallback * a_Client)
|
||||||
{
|
{
|
||||||
// Add a (socket, client) pair for processing, data from a_Socket is to be sent to a_Client
|
// Add a (socket, client) pair for processing, data from a_Socket is to be sent to a_Client
|
||||||
|
|
||||||
@ -71,6 +71,7 @@ bool cSocketThreads::AddClient(cSocket * a_Socket, cCallback * a_Client)
|
|||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
/*
|
||||||
void cSocketThreads::RemoveClient(const cSocket * a_Socket)
|
void cSocketThreads::RemoveClient(const cSocket * a_Socket)
|
||||||
{
|
{
|
||||||
// Remove the socket (and associated client) from processing
|
// Remove the socket (and associated client) from processing
|
||||||
@ -87,6 +88,7 @@ void cSocketThreads::RemoveClient(const cSocket * a_Socket)
|
|||||||
// Cannot assert here, this may actually happen legally, since cClientHandle has to clean up the socket and it may have already closed in the meantime
|
// Cannot assert here, this may actually happen legally, since cClientHandle has to clean up the socket and it may have already closed in the meantime
|
||||||
// ASSERT(!"Removing an unknown socket");
|
// ASSERT(!"Removing an unknown socket");
|
||||||
}
|
}
|
||||||
|
*/
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@ -133,20 +135,13 @@ void cSocketThreads::NotifyWrite(const cCallback * a_Client)
|
|||||||
|
|
||||||
|
|
||||||
|
|
||||||
void cSocketThreads::Write(const cSocket * a_Socket, const AString & a_Data)
|
void cSocketThreads::Write(const cCallback * a_Client, const AString & a_Data)
|
||||||
{
|
{
|
||||||
// Puts a_Data into outgoing data queue for a_Socket
|
// Puts a_Data into outgoing data queue for a_Client
|
||||||
|
|
||||||
if (!a_Socket->IsValid())
|
|
||||||
{
|
|
||||||
// Socket already closed, ignore the request
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
cCSLock Lock(m_CS);
|
cCSLock Lock(m_CS);
|
||||||
for (cSocketThreadList::iterator itr = m_Threads.begin(); itr != m_Threads.end(); ++itr)
|
for (cSocketThreadList::iterator itr = m_Threads.begin(); itr != m_Threads.end(); ++itr)
|
||||||
{
|
{
|
||||||
if ((*itr)->Write(a_Socket, a_Data))
|
if ((*itr)->Write(a_Client, a_Data))
|
||||||
{
|
{
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@ -181,24 +176,20 @@ void cSocketThreads::StopReading(const cCallback * a_Client)
|
|||||||
|
|
||||||
|
|
||||||
/// Queues the socket for closing, as soon as its outgoing data is sent
|
/// Queues the socket for closing, as soon as its outgoing data is sent
|
||||||
void cSocketThreads::QueueClose(const cSocket * a_Socket)
|
void cSocketThreads::QueueClose(const cCallback * a_Client)
|
||||||
{
|
{
|
||||||
if (!a_Socket->IsValid())
|
LOGD("QueueClose(client %p)", a_Client);
|
||||||
{
|
|
||||||
// Already closed, ignore the request
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
cCSLock Lock(m_CS);
|
cCSLock Lock(m_CS);
|
||||||
for (cSocketThreadList::iterator itr = m_Threads.begin(); itr != m_Threads.end(); ++itr)
|
for (cSocketThreadList::iterator itr = m_Threads.begin(); itr != m_Threads.end(); ++itr)
|
||||||
{
|
{
|
||||||
if ((*itr)->QueueClose(a_Socket))
|
if ((*itr)->QueueClose(a_Client))
|
||||||
{
|
{
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
} // for itr - m_Threads[]
|
} // for itr - m_Threads[]
|
||||||
|
|
||||||
ASSERT(!"Queueing close of an unknown socket");
|
ASSERT(!"Queueing close of an unknown client");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@ -240,13 +231,15 @@ cSocketThreads::cSocketThread::~cSocketThread()
|
|||||||
|
|
||||||
|
|
||||||
|
|
||||||
void cSocketThreads::cSocketThread::AddClient(cSocket * a_Socket, cCallback * a_Client)
|
void cSocketThreads::cSocketThread::AddClient(const cSocket & a_Socket, cCallback * a_Client)
|
||||||
{
|
{
|
||||||
ASSERT(m_NumSlots < MAX_SLOTS); // Use HasEmptySlot() to check before adding
|
ASSERT(m_NumSlots < MAX_SLOTS); // Use HasEmptySlot() to check before adding
|
||||||
|
|
||||||
m_Slots[m_NumSlots].m_Client = a_Client;
|
m_Slots[m_NumSlots].m_Client = a_Client;
|
||||||
m_Slots[m_NumSlots].m_Socket = a_Socket;
|
m_Slots[m_NumSlots].m_Socket = a_Socket;
|
||||||
m_Slots[m_NumSlots].m_Outgoing.clear();
|
m_Slots[m_NumSlots].m_Outgoing.clear();
|
||||||
|
m_Slots[m_NumSlots].m_ShouldClose = false;
|
||||||
|
m_Slots[m_NumSlots].m_ShouldCallClient = true;
|
||||||
m_NumSlots++;
|
m_NumSlots++;
|
||||||
|
|
||||||
// Notify the thread of the change:
|
// Notify the thread of the change:
|
||||||
@ -297,7 +290,7 @@ bool cSocketThreads::cSocketThread::RemoveSocket(const cSocket * a_Socket)
|
|||||||
|
|
||||||
for (int i = m_NumSlots - 1; i >= 0 ; --i)
|
for (int i = m_NumSlots - 1; i >= 0 ; --i)
|
||||||
{
|
{
|
||||||
if (m_Slots[i].m_Socket != a_Socket)
|
if (m_Slots[i].m_Socket != *a_Socket)
|
||||||
{
|
{
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
@ -339,7 +332,7 @@ bool cSocketThreads::cSocketThread::HasSocket(const cSocket * a_Socket) const
|
|||||||
{
|
{
|
||||||
for (int i = m_NumSlots - 1; i >= 0; --i)
|
for (int i = m_NumSlots - 1; i >= 0; --i)
|
||||||
{
|
{
|
||||||
if (m_Slots[i].m_Socket->GetSocket() == a_Socket->GetSocket())
|
if (m_Slots[i].m_Socket == *a_Socket)
|
||||||
{
|
{
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
@ -367,12 +360,12 @@ bool cSocketThreads::cSocketThread::NotifyWrite(const cCallback * a_Client)
|
|||||||
|
|
||||||
|
|
||||||
|
|
||||||
bool cSocketThreads::cSocketThread::Write(const cSocket * a_Socket, const AString & a_Data)
|
bool cSocketThreads::cSocketThread::Write(const cCallback * a_Client, const AString & a_Data)
|
||||||
{
|
{
|
||||||
// Returns true if socket handled by this thread
|
// Returns true if socket handled by this thread
|
||||||
for (int i = m_NumSlots - 1; i >= 0; --i)
|
for (int i = m_NumSlots - 1; i >= 0; --i)
|
||||||
{
|
{
|
||||||
if (m_Slots[i].m_Socket == a_Socket)
|
if (m_Slots[i].m_Client == a_Client)
|
||||||
{
|
{
|
||||||
m_Slots[i].m_Outgoing.append(a_Data);
|
m_Slots[i].m_Outgoing.append(a_Data);
|
||||||
|
|
||||||
@ -397,13 +390,7 @@ bool cSocketThreads::cSocketThread::StopReading (const cCallback * a_Client)
|
|||||||
{
|
{
|
||||||
if (m_Slots[i].m_Client == a_Client)
|
if (m_Slots[i].m_Client == a_Client)
|
||||||
{
|
{
|
||||||
m_Slots[i].m_Client = NULL;
|
m_Slots[i].m_ShouldCallClient = false;
|
||||||
m_Slots[i].m_ShouldClose = false;
|
|
||||||
|
|
||||||
// Notify the thread that there's a stop reading request:
|
|
||||||
ASSERT(m_ControlSocket2.IsValid());
|
|
||||||
m_ControlSocket2.Send("s", 1);
|
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
} // for i - m_Slots[]
|
} // for i - m_Slots[]
|
||||||
@ -414,14 +401,13 @@ bool cSocketThreads::cSocketThread::StopReading (const cCallback * a_Client)
|
|||||||
|
|
||||||
|
|
||||||
|
|
||||||
bool cSocketThreads::cSocketThread::QueueClose(const cSocket * a_Socket)
|
bool cSocketThreads::cSocketThread::QueueClose(const cCallback * a_Client)
|
||||||
{
|
{
|
||||||
// Returns true if socket handled by this thread
|
// Returns true if socket handled by this thread
|
||||||
for (int i = m_NumSlots - 1; i >= 0; --i)
|
for (int i = m_NumSlots - 1; i >= 0; --i)
|
||||||
{
|
{
|
||||||
if (m_Slots[i].m_Socket == a_Socket)
|
if (m_Slots[i].m_Client == a_Client)
|
||||||
{
|
{
|
||||||
ASSERT(m_Slots[i].m_Client == NULL); // Should have stopped reading first
|
|
||||||
m_Slots[i].m_ShouldClose = true;
|
m_Slots[i].m_ShouldClose = true;
|
||||||
|
|
||||||
// Notify the thread that there's a close queued (in case its conditions are already met):
|
// Notify the thread that there's a close queued (in case its conditions are already met):
|
||||||
@ -561,11 +547,11 @@ void cSocketThreads::cSocketThread::PrepareSet(fd_set * a_Set, cSocket::xSocket
|
|||||||
cCSLock Lock(m_Parent->m_CS);
|
cCSLock Lock(m_Parent->m_CS);
|
||||||
for (int i = m_NumSlots - 1; i >= 0; --i)
|
for (int i = m_NumSlots - 1; i >= 0; --i)
|
||||||
{
|
{
|
||||||
if (!m_Slots[i].m_Socket->IsValid())
|
if (!m_Slots[i].m_Socket.IsValid())
|
||||||
{
|
{
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
cSocket::xSocket s = m_Slots[i].m_Socket->GetSocket();
|
cSocket::xSocket s = m_Slots[i].m_Socket.GetSocket();
|
||||||
FD_SET(s, a_Set);
|
FD_SET(s, a_Set);
|
||||||
if (s > a_Highest)
|
if (s > a_Highest)
|
||||||
{
|
{
|
||||||
@ -593,24 +579,24 @@ void cSocketThreads::cSocketThread::ReadFromSockets(fd_set * a_Read)
|
|||||||
cCSLock Lock(m_Parent->m_CS);
|
cCSLock Lock(m_Parent->m_CS);
|
||||||
for (int i = m_NumSlots - 1; i >= 0; --i)
|
for (int i = m_NumSlots - 1; i >= 0; --i)
|
||||||
{
|
{
|
||||||
if (!FD_ISSET(m_Slots[i].m_Socket->GetSocket(), a_Read))
|
if (!FD_ISSET(m_Slots[i].m_Socket.GetSocket(), a_Read))
|
||||||
{
|
{
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
char Buffer[1024];
|
char Buffer[1024];
|
||||||
int Received = m_Slots[i].m_Socket->Receive(Buffer, ARRAYCOUNT(Buffer), 0);
|
int Received = m_Slots[i].m_Socket.Receive(Buffer, ARRAYCOUNT(Buffer), 0);
|
||||||
if (Received == 0)
|
if (Received == 0)
|
||||||
{
|
{
|
||||||
// The socket has been closed by the remote party, close our socket and let it be removed after we process all reading
|
// The socket has been closed by the remote party, close our socket and let it be removed after we process all reading
|
||||||
m_Slots[i].m_Socket->CloseSocket();
|
m_Slots[i].m_Socket.CloseSocket();
|
||||||
if (m_Slots[i].m_Client != NULL)
|
if (m_Slots[i].m_ShouldCallClient)
|
||||||
{
|
{
|
||||||
m_Slots[i].m_Client->SocketClosed();
|
m_Slots[i].m_Client->SocketClosed();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else if (Received > 0)
|
else if (Received > 0)
|
||||||
{
|
{
|
||||||
if (m_Slots[i].m_Client != NULL)
|
if (m_Slots[i].m_ShouldCallClient)
|
||||||
{
|
{
|
||||||
m_Slots[i].m_Client->DataReceived(Buffer, Received);
|
m_Slots[i].m_Client->DataReceived(Buffer, Received);
|
||||||
}
|
}
|
||||||
@ -618,8 +604,8 @@ void cSocketThreads::cSocketThread::ReadFromSockets(fd_set * a_Read)
|
|||||||
else
|
else
|
||||||
{
|
{
|
||||||
// The socket has encountered an error, close it and let it be removed after we process all reading
|
// The socket has encountered an error, close it and let it be removed after we process all reading
|
||||||
m_Slots[i].m_Socket->CloseSocket();
|
m_Slots[i].m_Socket.CloseSocket();
|
||||||
if (m_Slots[i].m_Client != NULL)
|
if (m_Slots[i].m_ShouldCallClient)
|
||||||
{
|
{
|
||||||
m_Slots[i].m_Client->SocketClosed();
|
m_Slots[i].m_Client->SocketClosed();
|
||||||
}
|
}
|
||||||
@ -637,38 +623,41 @@ void cSocketThreads::cSocketThread::WriteToSockets(fd_set * a_Write)
|
|||||||
cCSLock Lock(m_Parent->m_CS);
|
cCSLock Lock(m_Parent->m_CS);
|
||||||
for (int i = m_NumSlots - 1; i >= 0; --i)
|
for (int i = m_NumSlots - 1; i >= 0; --i)
|
||||||
{
|
{
|
||||||
cSocket Socket(*(m_Slots[i].m_Socket));
|
if (!m_Slots[i].m_Socket.IsValid() || !FD_ISSET(m_Slots[i].m_Socket.GetSocket(), a_Write))
|
||||||
if (!Socket.IsValid() || !FD_ISSET(Socket.GetSocket(), a_Write))
|
|
||||||
{
|
{
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
if (m_Slots[i].m_Outgoing.empty())
|
if (m_Slots[i].m_Outgoing.empty())
|
||||||
{
|
{
|
||||||
// Request another chunk of outgoing data:
|
// Request another chunk of outgoing data:
|
||||||
if (m_Slots[i].m_Client != NULL)
|
if (m_Slots[i].m_ShouldCallClient)
|
||||||
{
|
{
|
||||||
m_Slots[i].m_Client->GetOutgoingData(m_Slots[i].m_Outgoing);
|
m_Slots[i].m_Client->GetOutgoingData(m_Slots[i].m_Outgoing);
|
||||||
}
|
}
|
||||||
if (m_Slots[i].m_Outgoing.empty())
|
if (m_Slots[i].m_Outgoing.empty())
|
||||||
{
|
{
|
||||||
// Nothing ready
|
// Nothing ready
|
||||||
if ((m_Slots[i].m_Client == NULL) && m_Slots[i].m_ShouldClose)
|
if (m_Slots[i].m_ShouldClose)
|
||||||
{
|
{
|
||||||
// Socket was queued for closing and there's no more data to send, close it now:
|
// Socket was queued for closing and there's no more data to send, close it now:
|
||||||
m_Slots[i].m_Socket->CloseSocket();
|
|
||||||
m_Slots[i] = m_Slots[--m_NumSlots];
|
// DEBUG
|
||||||
|
LOGD("Socket was queued for closing, closing now. Slot %d, client %p, socket %d", i, m_Slots[i].m_Client, m_Slots[i].m_Socket.GetSocket());
|
||||||
|
|
||||||
|
m_Slots[i].m_Socket.CloseSocket();
|
||||||
|
// The slot must be freed actively by the client, using RemoveClient()
|
||||||
}
|
}
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
} // if (outgoing data is empty)
|
} // if (outgoing data is empty)
|
||||||
|
|
||||||
int Sent = m_Slots[i].m_Socket->Send(m_Slots[i].m_Outgoing.data(), m_Slots[i].m_Outgoing.size());
|
int Sent = m_Slots[i].m_Socket.Send(m_Slots[i].m_Outgoing.data(), m_Slots[i].m_Outgoing.size());
|
||||||
if (Sent < 0)
|
if (Sent < 0)
|
||||||
{
|
{
|
||||||
int Err = cSocket::GetLastError();
|
int Err = cSocket::GetLastError();
|
||||||
LOGWARNING("Error %d while writing to client \"%s\", disconnecting. \"%s\"", Err, m_Slots[i].m_Socket->GetIPString().c_str(), cSocket::GetErrorString(Err).c_str());
|
LOGWARNING("Error %d while writing to client \"%s\", disconnecting. \"%s\"", Err, m_Slots[i].m_Socket.GetIPString().c_str(), cSocket::GetErrorString(Err).c_str());
|
||||||
m_Slots[i].m_Socket->CloseSocket();
|
m_Slots[i].m_Socket.CloseSocket();
|
||||||
if (m_Slots[i].m_Client != NULL)
|
if (m_Slots[i].m_ShouldCallClient)
|
||||||
{
|
{
|
||||||
m_Slots[i].m_Client->SocketClosed();
|
m_Slots[i].m_Client->SocketClosed();
|
||||||
}
|
}
|
||||||
@ -695,12 +684,12 @@ void cSocketThreads::cSocketThread::WriteToSockets(fd_set * a_Write)
|
|||||||
|
|
||||||
void cSocketThreads::cSocketThread::RemoveClosedSockets(void)
|
void cSocketThreads::cSocketThread::RemoveClosedSockets(void)
|
||||||
{
|
{
|
||||||
// Removes sockets that have closed from m_Slots[]
|
// Removes sockets that have been queued for closing from m_Slots[]
|
||||||
|
|
||||||
cCSLock Lock(m_Parent->m_CS);
|
cCSLock Lock(m_Parent->m_CS);
|
||||||
for (int i = m_NumSlots - 1; i >= 0; --i)
|
for (int i = m_NumSlots - 1; i >= 0; --i)
|
||||||
{
|
{
|
||||||
if (m_Slots[i].m_Socket->IsValid())
|
if (!m_Slots[i].m_ShouldClose || !m_Slots[i].m_Outgoing.empty())
|
||||||
{
|
{
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
@ -76,25 +76,22 @@ public:
|
|||||||
~cSocketThreads();
|
~cSocketThreads();
|
||||||
|
|
||||||
/// Add a (socket, client) pair for processing, data from a_Socket is to be sent to a_Client; returns true if successful
|
/// Add a (socket, client) pair for processing, data from a_Socket is to be sent to a_Client; returns true if successful
|
||||||
bool AddClient(cSocket * a_Socket, cCallback * a_Client);
|
bool AddClient(const cSocket & a_Socket, cCallback * a_Client);
|
||||||
|
|
||||||
/// Remove the socket (and associated client) from processing
|
/// Remove the associated socket and the client from processing. The socket is left to send its data and is removed only after all its m_OutgoingData is sent
|
||||||
void RemoveClient(const cSocket * a_Socket);
|
|
||||||
|
|
||||||
/// Remove the associated socket and the client from processing
|
|
||||||
void RemoveClient(const cCallback * a_Client);
|
void RemoveClient(const cCallback * a_Client);
|
||||||
|
|
||||||
/// Notify the thread responsible for a_Client that the client has something to write
|
/// Notify the thread responsible for a_Client that the client has something to write
|
||||||
void NotifyWrite(const cCallback * a_Client);
|
void NotifyWrite(const cCallback * a_Client);
|
||||||
|
|
||||||
/// Puts a_Data into outgoing data queue for a_Socket
|
/// Puts a_Data into outgoing data queue for a_Client
|
||||||
void Write(const cSocket * a_Socket, const AString & a_Data);
|
void Write(const cCallback * a_Client, const AString & a_Data);
|
||||||
|
|
||||||
/// Stops reading from the socket - when this call returns, no more calls to the callbacks are made
|
/// Stops reading from the client - when this call returns, no more calls to the callbacks are made
|
||||||
void StopReading(const cCallback * a_Client);
|
void StopReading(const cCallback * a_Client);
|
||||||
|
|
||||||
/// Queues the socket for closing, as soon as its outgoing data is sent
|
/// Queues the client for closing, as soon as its outgoing data is sent
|
||||||
void QueueClose(const cSocket * a_Socket);
|
void QueueClose(const cCallback * a_Client);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
|
||||||
@ -112,15 +109,15 @@ private:
|
|||||||
bool HasEmptySlot(void) const {return m_NumSlots < MAX_SLOTS; }
|
bool HasEmptySlot(void) const {return m_NumSlots < MAX_SLOTS; }
|
||||||
bool IsEmpty (void) const {return m_NumSlots == 0; }
|
bool IsEmpty (void) const {return m_NumSlots == 0; }
|
||||||
|
|
||||||
void AddClient (cSocket * a_Socket, cCallback * a_Client);
|
void AddClient (const cSocket & a_Socket, cCallback * a_Client); // Takes ownership of the socket
|
||||||
bool RemoveClient(const cCallback * a_Client); // Returns true if removed, false if not found
|
bool RemoveClient(const cCallback * a_Client); // Returns true if removed, false if not found
|
||||||
bool RemoveSocket(const cSocket * a_Socket); // Returns true if removed, false if not found
|
bool RemoveSocket(const cSocket * a_Socket); // Returns true if removed, false if not found
|
||||||
bool HasClient (const cCallback * a_Client) const;
|
bool HasClient (const cCallback * a_Client) const;
|
||||||
bool HasSocket (const cSocket * a_Socket) const;
|
bool HasSocket (const cSocket * a_Socket) const;
|
||||||
bool NotifyWrite (const cCallback * a_Client); // Returns true if client handled by this thread
|
bool NotifyWrite (const cCallback * a_Client); // Returns true if client handled by this thread
|
||||||
bool Write (const cSocket * a_Socket, const AString & a_Data); // Returns true if socket handled by this thread
|
bool Write (const cCallback * a_Client, const AString & a_Data); // Returns true if client handled by this thread
|
||||||
bool StopReading (const cCallback * a_Client); // Returns true if client handled by this thread
|
bool StopReading (const cCallback * a_Client); // Returns true if client handled by this thread
|
||||||
bool QueueClose (const cSocket * a_Socket); // Returns true if socket handled by this thread
|
bool QueueClose (const cCallback * a_Client); // Returns true if client handled by this thread
|
||||||
|
|
||||||
bool Start(void); // Hide the cIsThread's Start method, we need to provide our own startup to create the control socket
|
bool Start(void); // Hide the cIsThread's Start method, we need to provide our own startup to create the control socket
|
||||||
|
|
||||||
@ -138,10 +135,11 @@ private:
|
|||||||
// Manipulation with these assumes that the parent's m_CS is locked
|
// Manipulation with these assumes that the parent's m_CS is locked
|
||||||
struct sSlot
|
struct sSlot
|
||||||
{
|
{
|
||||||
cSocket * m_Socket;
|
cSocket m_Socket; // The socket is primarily owned by this
|
||||||
cCallback * m_Client;
|
cCallback * m_Client;
|
||||||
AString m_Outgoing; // If sending writes only partial data, the rest is stored here for another send
|
AString m_Outgoing; // If sending writes only partial data, the rest is stored here for another send
|
||||||
bool m_ShouldClose; // If true, the socket is to be closed after sending all outgoing data
|
bool m_ShouldClose; // If true, the socket is to be closed after sending all outgoing data
|
||||||
|
bool m_ShouldCallClient; // If true, the client callbacks are called. Set to false in StopReading()
|
||||||
} ;
|
} ;
|
||||||
sSlot m_Slots[MAX_SLOTS];
|
sSlot m_Slots[MAX_SLOTS];
|
||||||
int m_NumSlots; // Number of slots actually used
|
int m_NumSlots; // Number of slots actually used
|
||||||
|
@ -37,6 +37,7 @@ public:
|
|||||||
m_Client(a_Client)
|
m_Client(a_Client)
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
virtual ~cProtocol() {}
|
||||||
|
|
||||||
/// Called when client sends some data
|
/// Called when client sends some data
|
||||||
virtual void DataReceived(const char * a_Data, int a_Size) = 0;
|
virtual void DataReceived(const char * a_Data, int a_Size) = 0;
|
||||||
|
@ -150,6 +150,7 @@ cProtocol132::cProtocol132(cClientHandle * a_Client) :
|
|||||||
super(a_Client),
|
super(a_Client),
|
||||||
m_IsEncrypted(false)
|
m_IsEncrypted(false)
|
||||||
{
|
{
|
||||||
|
LOGD("Created cProtocol132 at %p", this);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@ -162,6 +163,7 @@ cProtocol132::~cProtocol132()
|
|||||||
{
|
{
|
||||||
LOGD("There are %d unsent bytes while deleting cProtocol132", m_DataToSend.size());
|
LOGD("There are %d unsent bytes while deleting cProtocol132", m_DataToSend.size());
|
||||||
}
|
}
|
||||||
|
LOGD("Deleted cProtocol132 at %p", this);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -24,8 +24,6 @@ class cProtocol132 :
|
|||||||
public:
|
public:
|
||||||
|
|
||||||
cProtocol132(cClientHandle * a_Client);
|
cProtocol132(cClientHandle * a_Client);
|
||||||
|
|
||||||
// DEBUG:
|
|
||||||
virtual ~cProtocol132();
|
virtual ~cProtocol132();
|
||||||
|
|
||||||
/// Called when client sends some data:
|
/// Called when client sends some data:
|
||||||
|
@ -29,6 +29,14 @@ cProtocolRecognizer::cProtocolRecognizer(cClientHandle * a_Client) :
|
|||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
cProtocolRecognizer::~cProtocolRecognizer()
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
void cProtocolRecognizer::DataReceived(const char * a_Data, int a_Size)
|
void cProtocolRecognizer::DataReceived(const char * a_Data, int a_Size)
|
||||||
{
|
{
|
||||||
if (m_Protocol == NULL)
|
if (m_Protocol == NULL)
|
||||||
|
@ -32,6 +32,7 @@ class cProtocolRecognizer :
|
|||||||
|
|
||||||
public:
|
public:
|
||||||
cProtocolRecognizer(cClientHandle * a_Client);
|
cProtocolRecognizer(cClientHandle * a_Client);
|
||||||
|
virtual ~cProtocolRecognizer();
|
||||||
|
|
||||||
/// Called when client sends some data:
|
/// Called when client sends some data:
|
||||||
virtual void DataReceived(const char * a_Data, int a_Size) override;
|
virtual void DataReceived(const char * a_Data, int a_Size) override;
|
||||||
|
@ -113,27 +113,27 @@ void cServer::NotifyClientWrite(const cClientHandle * a_Client)
|
|||||||
|
|
||||||
|
|
||||||
|
|
||||||
void cServer::WriteToClient(const cSocket * a_Socket, const AString & a_Data)
|
void cServer::WriteToClient(const cClientHandle * a_Client, const AString & a_Data)
|
||||||
{
|
{
|
||||||
m_SocketThreads.Write(a_Socket, a_Data);
|
m_SocketThreads.Write(a_Client, a_Data);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
void cServer::QueueClientClose(const cSocket * a_Socket)
|
void cServer::QueueClientClose(const cClientHandle * a_Client)
|
||||||
{
|
{
|
||||||
m_SocketThreads.QueueClose(a_Socket);
|
m_SocketThreads.QueueClose(a_Client);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
void cServer::RemoveClient(const cSocket * a_Socket)
|
void cServer::RemoveClient(const cClientHandle * a_Client)
|
||||||
{
|
{
|
||||||
m_SocketThreads.RemoveClient(a_Socket);
|
m_SocketThreads.RemoveClient(a_Client);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@ -340,8 +340,8 @@ void cServer::StartListenClient()
|
|||||||
|
|
||||||
LOG("Client \"%s\" connected!", ClientIP.c_str());
|
LOG("Client \"%s\" connected!", ClientIP.c_str());
|
||||||
|
|
||||||
cClientHandle *NewHandle = new cClientHandle(SClient, m_ClientViewDistance);
|
cClientHandle * NewHandle = new cClientHandle(&SClient, m_ClientViewDistance);
|
||||||
if (!m_SocketThreads.AddClient(&(NewHandle->GetSocket()), NewHandle))
|
if (!m_SocketThreads.AddClient(SClient, NewHandle))
|
||||||
{
|
{
|
||||||
// For some reason SocketThreads have rejected the handle, clean it up
|
// For some reason SocketThreads have rejected the handle, clean it up
|
||||||
LOGERROR("Client \"%s\" cannot be handled, server probably unstable", SClient.GetIPString().c_str());
|
LOGERROR("Client \"%s\" cannot be handled, server probably unstable", SClient.GetIPString().c_str());
|
||||||
|
@ -62,11 +62,11 @@ public: //tolua_export
|
|||||||
|
|
||||||
void NotifyClientWrite(const cClientHandle * a_Client); // Notifies m_SocketThreads that client has something to be written
|
void NotifyClientWrite(const cClientHandle * a_Client); // Notifies m_SocketThreads that client has something to be written
|
||||||
|
|
||||||
void WriteToClient(const cSocket * a_Socket, const AString & a_Data); // Queues outgoing data for the socket through m_SocketThreads
|
void WriteToClient(const cClientHandle * a_Client, const AString & a_Data); // Queues outgoing data for the client through m_SocketThreads
|
||||||
|
|
||||||
void QueueClientClose(const cSocket * a_Socket); // Queues the socket to close when all its outgoing data is sent
|
void QueueClientClose(const cClientHandle * a_Client); // Queues the clienthandle to close when all its outgoing data is sent
|
||||||
|
|
||||||
void RemoveClient(const cSocket * a_Socket); // Removes the socket from m_SocketThreads
|
void RemoveClient(const cClientHandle * a_Client); // Removes the clienthandle from m_SocketThreads
|
||||||
|
|
||||||
CryptoPP::RSA::PrivateKey & GetPrivateKey(void) { return m_PrivateKey; }
|
CryptoPP::RSA::PrivateKey & GetPrivateKey(void) { return m_PrivateKey; }
|
||||||
CryptoPP::RSA::PublicKey & GetPublicKey (void) { return m_PublicKey; }
|
CryptoPP::RSA::PublicKey & GetPublicKey (void) { return m_PublicKey; }
|
||||||
|
@ -26,6 +26,7 @@ class cSlotArea
|
|||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
cSlotArea(int a_NumSlots, cWindow & a_ParentWindow);
|
cSlotArea(int a_NumSlots, cWindow & a_ParentWindow);
|
||||||
|
virtual ~cSlotArea() {} // force a virtual destructor in all subclasses
|
||||||
|
|
||||||
int GetNumSlots(void) const { return m_NumSlots; }
|
int GetNumSlots(void) const { return m_NumSlots; }
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user