Adding websocket server code, so that a gateway is no longer required. Still kind of untested. Requires websocketpp.
This commit is contained in:
@@ -1,6 +1,6 @@
|
||||
/*****************************************************************************
|
||||
* PokerTH - The open source texas holdem engine *
|
||||
* Copyright (C) 2006-2012 Felix Hammer, Florian Thauer, Lothar May *
|
||||
* Copyright (C) 2006-2013 Felix Hammer, Florian Thauer, Lothar May *
|
||||
* *
|
||||
* This program is free software: you can redistribute it and/or modify *
|
||||
* it under the terms of the GNU Affero General Public License as *
|
||||
@@ -30,7 +30,7 @@
|
||||
*****************************************************************************/
|
||||
|
||||
#include <net/chatcleanermanager.h>
|
||||
#include <net/sendbuffer.h>
|
||||
#include <net/asiosendbuffer.h>
|
||||
#include <boost/bind.hpp>
|
||||
#include <core/loghelper.h>
|
||||
#include <third_party/protobuf/chatcleaner.pb.h>
|
||||
@@ -49,7 +49,7 @@ ChatCleanerManager::ChatCleanerManager(ChatCleanerCallback &cb, boost::shared_pt
|
||||
m_resolver.reset(
|
||||
new boost::asio::ip::tcp::resolver(*m_ioService));
|
||||
m_sendManager.reset(
|
||||
new SendBuffer);
|
||||
new AsioSendBuffer);
|
||||
}
|
||||
|
||||
ChatCleanerManager::~ChatCleanerManager()
|
||||
@@ -273,7 +273,7 @@ ChatCleanerManager::SendMessageToServer(ChatCleanerMessage &msg)
|
||||
google::protobuf::uint8 *buf = new google::protobuf::uint8[packetSize + CLEANER_NET_HEADER_SIZE];
|
||||
*((uint32_t *)buf) = htonl(packetSize);
|
||||
msg.SerializeWithCachedSizesToArray(&buf[CLEANER_NET_HEADER_SIZE]);
|
||||
SendBuffer::EncodeToBuf(buf, packetSize + CLEANER_NET_HEADER_SIZE, m_sendManager.get());
|
||||
m_sendManager->EncodeToBuf(NULL, buf, packetSize + CLEANER_NET_HEADER_SIZE);
|
||||
delete[] buf;
|
||||
|
||||
m_sendManager->AsyncSendNextPacket(m_socket);
|
||||
|
||||
@@ -39,6 +39,7 @@
|
||||
#include <net/clientexception.h>
|
||||
#include <net/socket_msg.h>
|
||||
#include <net/net_helper.h>
|
||||
#include <net/asioreceivebuffer.h>
|
||||
#include <core/avatarmanager.h>
|
||||
#include <core/loghelper.h>
|
||||
#include <clientenginefactory.h>
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
/*****************************************************************************
|
||||
* PokerTH - The open source texas holdem engine *
|
||||
* Copyright (C) 2006-2012 Felix Hammer, Florian Thauer, Lothar May *
|
||||
* Copyright (C) 2006-2013 Felix Hammer, Florian Thauer, Lothar May *
|
||||
* *
|
||||
* This program is free software: you can redistribute it and/or modify *
|
||||
* it under the terms of the GNU Affero General Public License as *
|
||||
@@ -29,118 +29,11 @@
|
||||
* as that of the covered work. *
|
||||
*****************************************************************************/
|
||||
|
||||
#include <boost/asio.hpp>
|
||||
#include <boost/bind.hpp>
|
||||
|
||||
#include <net/receivebuffer.h>
|
||||
#include <net/sessiondata.h>
|
||||
#include <core/loghelper.h>
|
||||
#include <boost/swap.hpp>
|
||||
|
||||
using namespace std;
|
||||
|
||||
NetPacketValidator ReceiveBuffer::validator;
|
||||
|
||||
ReceiveBuffer::ReceiveBuffer()
|
||||
: recvBufUsed(0)
|
||||
ReceiveBuffer::~ReceiveBuffer()
|
||||
{
|
||||
recvBuf[0] = 0;
|
||||
}
|
||||
|
||||
void
|
||||
ReceiveBuffer::StartAsyncRead(boost::shared_ptr<SessionData> session)
|
||||
{
|
||||
session->GetAsioSocket()->async_read_some(
|
||||
boost::asio::buffer(recvBuf + recvBufUsed, RECV_BUF_SIZE - recvBufUsed),
|
||||
boost::bind(
|
||||
&ReceiveBuffer::HandleRead,
|
||||
shared_from_this(),
|
||||
session,
|
||||
boost::asio::placeholders::error,
|
||||
boost::asio::placeholders::bytes_transferred));
|
||||
}
|
||||
|
||||
void
|
||||
ReceiveBuffer::HandleRead(boost::shared_ptr<SessionData> session, const boost::system::error_code &error, size_t bytesRead)
|
||||
{
|
||||
if (error != boost::asio::error::operation_aborted) {
|
||||
try {
|
||||
if (!error) {
|
||||
recvBufUsed += bytesRead;
|
||||
ScanPackets(session);
|
||||
ProcessPackets(session);
|
||||
StartAsyncRead(session);
|
||||
} else if (error == boost::asio::error::interrupted || error == boost::asio::error::try_again) {
|
||||
LOG_ERROR("Session " << session->GetId() << " - recv interrupted: " << error);
|
||||
StartAsyncRead(session);
|
||||
} else {
|
||||
LOG_ERROR("Session " << session->GetId() << " - Connection closed: " << error);
|
||||
// On error: Close this session.
|
||||
session->Close();
|
||||
}
|
||||
} catch (const exception &e) {
|
||||
LOG_ERROR("Session " << session->GetId() << " - unhandled exception in HandleRead: " << e.what());
|
||||
throw;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void
|
||||
ReceiveBuffer::ScanPackets(boost::shared_ptr<SessionData> session)
|
||||
{
|
||||
bool dataAvailable = true;
|
||||
do {
|
||||
boost::shared_ptr<NetPacket> tmpPacket;
|
||||
// This is necessary, because we use TCP.
|
||||
// Packets may be received in multiple chunks or
|
||||
// several packets may be received at once.
|
||||
if (recvBufUsed >= NET_HEADER_SIZE) {
|
||||
// Read the size of the packet (first 4 bytes in network byte order).
|
||||
uint32_t nativeVal;
|
||||
memcpy(&nativeVal, &recvBuf[0], sizeof(uint32_t));
|
||||
size_t packetSize = ntohl(nativeVal);
|
||||
if (packetSize > MAX_PACKET_SIZE) {
|
||||
recvBufUsed = 0;
|
||||
LOG_ERROR("Session " << session->GetId() << " - Invalid packet size: " << packetSize);
|
||||
} else if (recvBufUsed >= packetSize + NET_HEADER_SIZE) {
|
||||
try {
|
||||
tmpPacket = NetPacket::Create(&recvBuf[NET_HEADER_SIZE], packetSize);
|
||||
if (tmpPacket) {
|
||||
recvBufUsed -= (packetSize + NET_HEADER_SIZE);
|
||||
if (recvBufUsed) {
|
||||
memmove(recvBuf, recvBuf + packetSize + NET_HEADER_SIZE, recvBufUsed);
|
||||
}
|
||||
}
|
||||
} catch (const exception &e) {
|
||||
// Reset buffer on error.
|
||||
recvBufUsed = 0;
|
||||
LOG_ERROR("Session " << session->GetId() << " - " << e.what());
|
||||
}
|
||||
}
|
||||
}
|
||||
if (tmpPacket) {
|
||||
if (validator.IsValidPacket(*tmpPacket)) {
|
||||
receivedPackets.push_back(tmpPacket);
|
||||
} else {
|
||||
LOG_ERROR("Session " << session->GetId() << " - Invalid packet: " << tmpPacket->GetMsg()->messagetype());
|
||||
}
|
||||
} else {
|
||||
dataAvailable = false;
|
||||
}
|
||||
} while(dataAvailable);
|
||||
}
|
||||
|
||||
void
|
||||
ReceiveBuffer::ProcessPackets(boost::shared_ptr<SessionData> session)
|
||||
{
|
||||
while (!receivedPackets.empty()) {
|
||||
boost::shared_ptr<NetPacket> p = receivedPackets.front();
|
||||
receivedPackets.pop_front();
|
||||
session->HandlePacket(p);
|
||||
}
|
||||
if (recvBufUsed >= RECV_BUF_SIZE) {
|
||||
LOG_ERROR("Session " << session->GetId() << " - Receive buf full: " << recvBufUsed);
|
||||
recvBufUsed = 0;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
/*****************************************************************************
|
||||
* PokerTH - The open source texas holdem engine *
|
||||
* Copyright (C) 2006-2012 Felix Hammer, Florian Thauer, Lothar May *
|
||||
* Copyright (C) 2006-2013 Felix Hammer, Florian Thauer, Lothar May *
|
||||
* *
|
||||
* This program is free software: you can redistribute it and/or modify *
|
||||
* it under the terms of the GNU Affero General Public License as *
|
||||
@@ -29,75 +29,12 @@
|
||||
* as that of the covered work. *
|
||||
*****************************************************************************/
|
||||
|
||||
#include <boost/asio.hpp>
|
||||
#include <boost/bind.hpp>
|
||||
|
||||
#include <net/sendbuffer.h>
|
||||
#include <boost/swap.hpp>
|
||||
|
||||
using namespace std;
|
||||
|
||||
|
||||
SendBuffer::SendBuffer()
|
||||
: sendBuf(NULL), curWriteBuf(NULL), sendBufAllocated(0), sendBufUsed(0),
|
||||
curWriteBufAllocated(0), curWriteBufUsed(0), closeAfterSend(false)
|
||||
{
|
||||
}
|
||||
|
||||
SendBuffer::~SendBuffer()
|
||||
{
|
||||
free(sendBuf);
|
||||
free(curWriteBuf);
|
||||
}
|
||||
|
||||
void
|
||||
SendBuffer::HandleWrite(boost::shared_ptr<boost::asio::ip::tcp::socket> socket, const boost::system::error_code &error)
|
||||
{
|
||||
if (!error) {
|
||||
// Successfully sent the data.
|
||||
boost::mutex::scoped_lock lock(dataMutex);
|
||||
curWriteBufUsed = 0;
|
||||
// Send more data, if available.
|
||||
AsyncSendNextPacket(socket);
|
||||
}
|
||||
}
|
||||
|
||||
void
|
||||
SendBuffer::AsyncSendNextPacket(boost::shared_ptr<boost::asio::ip::tcp::socket> socket)
|
||||
{
|
||||
if (!curWriteBufUsed) {
|
||||
// Swap buffers and send data.
|
||||
boost::swap(curWriteBuf, sendBuf);
|
||||
boost::swap(curWriteBufAllocated, sendBufAllocated);
|
||||
boost::swap(curWriteBufUsed, sendBufUsed);
|
||||
if (curWriteBufUsed) {
|
||||
boost::asio::async_write(
|
||||
*socket,
|
||||
boost::asio::buffer(curWriteBuf, curWriteBufUsed),
|
||||
boost::bind(&SendBuffer::HandleWrite,
|
||||
shared_from_this(),
|
||||
socket,
|
||||
boost::asio::placeholders::error));
|
||||
} else if (closeAfterSend) {
|
||||
socket->close();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
int
|
||||
SendBuffer::EncodeToBuf(const void *data, size_t size, void *arg)
|
||||
{
|
||||
SendBuffer *m = static_cast<SendBuffer *>(arg);
|
||||
|
||||
// Realloc buffer if necessary.
|
||||
while (m->GetSendBufLeft() < size) {
|
||||
if (!m->ReallocSendBuf()) {
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
|
||||
m->AppendToSendBufWithoutCheck((const char*)data, size);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
/*****************************************************************************
|
||||
* PokerTH - The open source texas holdem engine *
|
||||
* Copyright (C) 2006-2012 Felix Hammer, Florian Thauer, Lothar May *
|
||||
* Copyright (C) 2006-2013 Felix Hammer, Florian Thauer, Lothar May *
|
||||
* *
|
||||
* This program is free software: you can redistribute it and/or modify *
|
||||
* it under the terms of the GNU Affero General Public License as *
|
||||
@@ -56,9 +56,9 @@ SenderHelper::Send(boost::shared_ptr<SessionData> session, boost::shared_ptr<Net
|
||||
SendBuffer &tmpBuffer = session->GetSendBuffer();
|
||||
// Add packet to specific queue.
|
||||
boost::mutex::scoped_lock lock(tmpBuffer.dataMutex);
|
||||
InternalStorePacket(tmpBuffer, packet);
|
||||
tmpBuffer.InternalStorePacket(session, packet);
|
||||
// Activate async send, if needed.
|
||||
tmpBuffer.AsyncSendNextPacket(session->GetAsioSocket());
|
||||
tmpBuffer.AsyncSendNextPacket(session);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -73,11 +73,11 @@ SenderHelper::Send(boost::shared_ptr<SessionData> session, const NetPacketList &
|
||||
NetPacketList::const_iterator end = packetList.end();
|
||||
while (i != end) {
|
||||
if (*i)
|
||||
InternalStorePacket(tmpBuffer, *i);
|
||||
tmpBuffer.InternalStorePacket(session, *i);
|
||||
++i;
|
||||
}
|
||||
// Activate async send, if needed.
|
||||
tmpBuffer.AsyncSendNextPacket(session->GetAsioSocket());
|
||||
tmpBuffer.AsyncSendNextPacket(session);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -90,17 +90,6 @@ SenderHelper::SetCloseAfterSend(boost::shared_ptr<SessionData> session)
|
||||
// Mark that the socket should be closed after the send operation.
|
||||
tmpBuffer.SetCloseAfterSend();
|
||||
// Activate async send, if needed.
|
||||
tmpBuffer.AsyncSendNextPacket(session->GetAsioSocket());
|
||||
}
|
||||
|
||||
void
|
||||
SenderHelper::InternalStorePacket(SendBuffer &tmpBuffer, boost::shared_ptr<NetPacket> packet)
|
||||
{
|
||||
uint32_t packetSize = packet->GetMsg()->ByteSize();
|
||||
google::protobuf::uint8 *buf = new google::protobuf::uint8[packetSize + NET_HEADER_SIZE];
|
||||
*((uint32_t *)buf) = htonl(packetSize);
|
||||
packet->GetMsg()->SerializeWithCachedSizesToArray(&buf[NET_HEADER_SIZE]);
|
||||
SendBuffer::EncodeToBuf(buf, packetSize + NET_HEADER_SIZE, &tmpBuffer);
|
||||
delete[] buf;
|
||||
tmpBuffer.AsyncSendNextPacket(session);
|
||||
}
|
||||
|
||||
|
||||
@@ -1,39 +0,0 @@
|
||||
/*****************************************************************************
|
||||
* PokerTH - The open source texas holdem engine *
|
||||
* Copyright (C) 2006-2012 Felix Hammer, Florian Thauer, Lothar May *
|
||||
* *
|
||||
* This program is free software: you can redistribute it and/or modify *
|
||||
* it under the terms of the GNU Affero General Public License as *
|
||||
* published by the Free Software Foundation, either version 3 of the *
|
||||
* License, or (at your option) any later version. *
|
||||
* *
|
||||
* This program is distributed in the hope that it will be useful, *
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
|
||||
* GNU Affero General Public License for more details. *
|
||||
* *
|
||||
* You should have received a copy of the GNU Affero General Public License *
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>. *
|
||||
* *
|
||||
* *
|
||||
* Additional permission under GNU AGPL version 3 section 7 *
|
||||
* *
|
||||
* If you modify this program, or any covered work, by linking or *
|
||||
* combining it with the OpenSSL project's OpenSSL library (or a *
|
||||
* modified version of that library), containing parts covered by the *
|
||||
* terms of the OpenSSL or SSLeay licenses, the authors of PokerTH *
|
||||
* (Felix Hammer, Florian Thauer, Lothar May) grant you additional *
|
||||
* permission to convey the resulting work. *
|
||||
* Corresponding Source for a non-source form of such a combination *
|
||||
* shall include the source code for the parts of OpenSSL used as well *
|
||||
* as that of the covered work. *
|
||||
*****************************************************************************/
|
||||
|
||||
#include <net/serveraccepthelper.h>
|
||||
|
||||
|
||||
ServerAcceptInterface::~ServerAcceptInterface()
|
||||
{
|
||||
}
|
||||
|
||||
|
||||
@@ -267,10 +267,9 @@ ServerLobbyThread::SignalTermination()
|
||||
}
|
||||
|
||||
void
|
||||
ServerLobbyThread::AddConnection(boost::shared_ptr<tcp::socket> sock)
|
||||
ServerLobbyThread::AddConnection(boost::shared_ptr<SessionData> sessionData)
|
||||
{
|
||||
// Create a new session.
|
||||
boost::shared_ptr<SessionData> sessionData(new SessionData(sock, m_curSessionId++, *m_internalServerCallback, GetIOService()));
|
||||
m_sessionManager.AddSession(sessionData);
|
||||
|
||||
LOG_VERBOSE("Accepted connection - session #" << sessionData->GetId() << ".");
|
||||
@@ -284,42 +283,37 @@ ServerLobbyThread::AddConnection(boost::shared_ptr<tcp::socket> sock)
|
||||
if (numLobbySessions <= SERVER_MAX_NUM_LOBBY_SESSIONS
|
||||
&& numLobbySessions + numGameSessions <= SERVER_MAX_NUM_TOTAL_SESSIONS) {
|
||||
bool hasClientIp = false;
|
||||
boost::system::error_code errCode;
|
||||
tcp::endpoint clientEndpoint = sock->remote_endpoint(errCode);
|
||||
if (!errCode) {
|
||||
string ipAddress = clientEndpoint.address().to_string(errCode);
|
||||
if (!errCode && !ipAddress.empty()) {
|
||||
sessionData->SetClientAddr(ipAddress);
|
||||
hasClientIp = true;
|
||||
string ipAddress = sessionData->GetRemoteIPAddressFromSocket();
|
||||
if (!ipAddress.empty()) {
|
||||
sessionData->SetClientAddr(ipAddress);
|
||||
hasClientIp = true;
|
||||
|
||||
boost::shared_ptr<NetPacket> packet(new NetPacket);
|
||||
packet->GetMsg()->set_messagetype(PokerTHMessage::Type_AnnounceMessage);
|
||||
AnnounceMessage *netAnnounce = packet->GetMsg()->mutable_announcemessage();
|
||||
netAnnounce->mutable_protocolversion()->set_majorversion(NET_VERSION_MAJOR);
|
||||
netAnnounce->mutable_protocolversion()->set_minorversion(NET_VERSION_MINOR);
|
||||
netAnnounce->mutable_latestgameversion()->set_majorversion(POKERTH_VERSION_MAJOR);
|
||||
netAnnounce->mutable_latestgameversion()->set_minorversion(POKERTH_VERSION_MINOR);
|
||||
netAnnounce->set_latestbetarevision(POKERTH_BETA_REVISION);
|
||||
switch (GetServerMode()) {
|
||||
case SERVER_MODE_LAN:
|
||||
netAnnounce->set_servertype(AnnounceMessage::serverTypeLAN);
|
||||
break;
|
||||
case SERVER_MODE_INTERNET_NOAUTH:
|
||||
netAnnounce->set_servertype(AnnounceMessage::serverTypeInternetNoAuth);
|
||||
break;
|
||||
case SERVER_MODE_INTERNET_AUTH:
|
||||
netAnnounce->set_servertype(AnnounceMessage::serverTypeInternetAuth);
|
||||
break;
|
||||
}
|
||||
{
|
||||
boost::mutex::scoped_lock lock(m_statMutex);
|
||||
netAnnounce->set_numplayersonserver(m_statData.numberOfPlayersOnServer);
|
||||
}
|
||||
GetSender().Send(sessionData, packet);
|
||||
sessionData->GetReceiveBuffer().StartAsyncRead(sessionData);
|
||||
boost::shared_ptr<NetPacket> packet(new NetPacket);
|
||||
packet->GetMsg()->set_messagetype(PokerTHMessage::Type_AnnounceMessage);
|
||||
AnnounceMessage *netAnnounce = packet->GetMsg()->mutable_announcemessage();
|
||||
netAnnounce->mutable_protocolversion()->set_majorversion(NET_VERSION_MAJOR);
|
||||
netAnnounce->mutable_protocolversion()->set_minorversion(NET_VERSION_MINOR);
|
||||
netAnnounce->mutable_latestgameversion()->set_majorversion(POKERTH_VERSION_MAJOR);
|
||||
netAnnounce->mutable_latestgameversion()->set_minorversion(POKERTH_VERSION_MINOR);
|
||||
netAnnounce->set_latestbetarevision(POKERTH_BETA_REVISION);
|
||||
switch (GetServerMode()) {
|
||||
case SERVER_MODE_LAN:
|
||||
netAnnounce->set_servertype(AnnounceMessage::serverTypeLAN);
|
||||
break;
|
||||
case SERVER_MODE_INTERNET_NOAUTH:
|
||||
netAnnounce->set_servertype(AnnounceMessage::serverTypeInternetNoAuth);
|
||||
break;
|
||||
case SERVER_MODE_INTERNET_AUTH:
|
||||
netAnnounce->set_servertype(AnnounceMessage::serverTypeInternetAuth);
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (!hasClientIp) {
|
||||
{
|
||||
boost::mutex::scoped_lock lock(m_statMutex);
|
||||
netAnnounce->set_numplayersonserver(m_statData.numberOfPlayersOnServer);
|
||||
}
|
||||
GetSender().Send(sessionData, packet);
|
||||
sessionData->GetReceiveBuffer().StartAsyncRead(sessionData);
|
||||
} else {
|
||||
// We do not accept sessions if we cannot
|
||||
// retrieve the client address.
|
||||
SessionError(sessionData, ERR_NET_INVALID_SESSION);
|
||||
@@ -406,8 +400,11 @@ ServerLobbyThread::CloseSession(boost::shared_ptr<SessionData> session)
|
||||
UpdateStatisticsNumberOfPlayers();
|
||||
|
||||
// Ignore error when shutting down the socket.
|
||||
boost::system::error_code ec;
|
||||
session->GetAsioSocket()->shutdown(boost::asio::ip::tcp::socket::shutdown_receive, ec);
|
||||
boost::shared_ptr<boost::asio::ip::tcp::socket> sock = session->GetAsioSocket();
|
||||
if (sock) {
|
||||
boost::system::error_code ec;
|
||||
session->GetAsioSocket()->shutdown(boost::asio::ip::tcp::socket::shutdown_receive, ec);
|
||||
}
|
||||
// Close this session after send.
|
||||
GetSender().SetCloseAfterSend(session);
|
||||
// Cancel all timers of the session.
|
||||
@@ -789,6 +786,18 @@ ServerLobbyThread::GetBanManager()
|
||||
return *m_banManager;
|
||||
}
|
||||
|
||||
SessionDataCallback &
|
||||
ServerLobbyThread::GetSessionDataCallback()
|
||||
{
|
||||
return *m_internalServerCallback;
|
||||
}
|
||||
|
||||
u_int32_t
|
||||
ServerLobbyThread::GetNextSessionId()
|
||||
{
|
||||
return m_curSessionId++;
|
||||
}
|
||||
|
||||
u_int32_t
|
||||
ServerLobbyThread::GetNextUniquePlayerId()
|
||||
{
|
||||
|
||||
@@ -34,6 +34,7 @@
|
||||
#include <net/socket_helper.h>
|
||||
#include <net/serverlobbythread.h>
|
||||
#include <net/serveraccepthelper.h>
|
||||
#include <net/serveracceptwebhelper.h>
|
||||
#include <net/serverexception.h>
|
||||
#include <net/socket_msg.h>
|
||||
#include <net/socket_startup.h>
|
||||
@@ -92,6 +93,11 @@ ServerManager::Init(unsigned serverPort, bool ipv6, ServerTransportProtocol prot
|
||||
sctpAcceptHelper->Listen(serverPort, ipv6, logDir, m_lobbyThread);
|
||||
m_acceptHelperPool.push_back(sctpAcceptHelper);
|
||||
}*/
|
||||
{
|
||||
boost::shared_ptr<ServerAcceptInterface> webAcceptHelper(new ServerAcceptWebHelper(GetGui(), m_ioService));
|
||||
webAcceptHelper->Listen(7233, true, logDir, m_lobbyThread);
|
||||
m_acceptHelperPool.push_back(webAcceptHelper);
|
||||
}
|
||||
}
|
||||
|
||||
void
|
||||
|
||||
@@ -30,25 +30,40 @@
|
||||
*****************************************************************************/
|
||||
|
||||
#include <net/sessiondata.h>
|
||||
#include <net/receivebuffer.h>
|
||||
#include <net/sendbuffer.h>
|
||||
#include <net/asioreceivebuffer.h>
|
||||
#include <net/webreceivebuffer.h>
|
||||
#include <net/asiosendbuffer.h>
|
||||
#include <net/websendbuffer.h>
|
||||
#include <net/socket_msg.h>
|
||||
#include <net/websocketdata.h>
|
||||
#include <gsasl.h>
|
||||
|
||||
using namespace std;
|
||||
using boost::asio::ip::tcp;
|
||||
|
||||
SessionData::SessionData(boost::shared_ptr<boost::asio::ip::tcp::socket> sock, SessionId id, SessionDataCallback &cb, boost::asio::io_service &ioService)
|
||||
: m_socket(sock), m_id(id), m_state(SessionData::Init), m_readyFlag(false), m_wantsLobbyMsg(true),
|
||||
m_activityTimeoutSec(0), m_activityWarningRemainingSec(0), m_initTimeoutTimer(ioService), m_globalTimeoutTimer(ioService),
|
||||
m_activityTimeoutTimer(ioService), m_callback(cb), m_authSession(NULL), m_curAuthStep(0)
|
||||
{
|
||||
m_receiveBuffer.reset(new ReceiveBuffer);
|
||||
m_sendBuffer.reset(new SendBuffer);
|
||||
m_receiveBuffer.reset(new AsioReceiveBuffer);
|
||||
m_sendBuffer.reset(new AsioSendBuffer);
|
||||
}
|
||||
|
||||
SessionData::SessionData(boost::shared_ptr<WebSocketData> webData, SessionId id, SessionDataCallback &cb, boost::asio::io_service &ioService, int filler)
|
||||
: m_webData(webData), m_id(id), m_state(SessionData::Init), m_readyFlag(false), m_wantsLobbyMsg(true),
|
||||
m_activityTimeoutSec(0), m_activityWarningRemainingSec(0), m_initTimeoutTimer(ioService), m_globalTimeoutTimer(ioService),
|
||||
m_activityTimeoutTimer(ioService), m_callback(cb), m_authSession(NULL), m_curAuthStep(0)
|
||||
{
|
||||
m_receiveBuffer.reset(new WebReceiveBuffer);
|
||||
m_sendBuffer.reset(new WebSendBuffer(webData));
|
||||
}
|
||||
|
||||
SessionData::~SessionData()
|
||||
{
|
||||
InternalClearAuthSession();
|
||||
// Web Socket handle needs to be manually closed, asio socket is closed automatically.
|
||||
CloseWebSocketHandle();
|
||||
}
|
||||
|
||||
SessionId
|
||||
@@ -278,6 +293,24 @@ SessionData::SetClientAddr(const std::string &addr)
|
||||
m_clientAddr = addr;
|
||||
}
|
||||
|
||||
void
|
||||
SessionData::CloseSocketHandle()
|
||||
{
|
||||
if (m_socket) {
|
||||
boost::system::error_code ec;
|
||||
m_socket->close(ec);
|
||||
}
|
||||
}
|
||||
|
||||
void
|
||||
SessionData::CloseWebSocketHandle()
|
||||
{
|
||||
if (m_webData) {
|
||||
boost::system::error_code ec;
|
||||
m_webData->webSocketServer->close(m_webData->webHandle, websocketpp::close::status::normal, "PokerTH server closed the connection.", ec);
|
||||
}
|
||||
}
|
||||
|
||||
void
|
||||
SessionData::ResetActivityTimer()
|
||||
{
|
||||
@@ -348,3 +381,20 @@ SessionData::GetPlayerData()
|
||||
return m_playerData;
|
||||
}
|
||||
|
||||
string
|
||||
SessionData::GetRemoteIPAddressFromSocket() const
|
||||
{
|
||||
string ipAddress;
|
||||
if (m_socket) {
|
||||
boost::system::error_code errCode;
|
||||
tcp::endpoint clientEndpoint = m_socket->remote_endpoint(errCode);
|
||||
if (!errCode) {
|
||||
ipAddress = clientEndpoint.address().to_string(errCode);
|
||||
}
|
||||
} else {
|
||||
server::connection_ptr con = m_webData->webSocketServer->get_con_from_hdl(m_webData->webHandle);
|
||||
ipAddress = con->get_remote_endpoint();
|
||||
}
|
||||
return ipAddress;
|
||||
}
|
||||
|
||||
|
||||
@@ -281,7 +281,9 @@ SessionManager::Clear()
|
||||
|
||||
boost::system::error_code ec;
|
||||
while (i != end) {
|
||||
i->second->GetAsioSocket()->close(ec);
|
||||
// Close all raw handles.
|
||||
i->second->CloseSocketHandle();
|
||||
i->second->CloseWebSocketHandle();
|
||||
++i;
|
||||
}
|
||||
m_sessionMap.clear();
|
||||
|
||||
Reference in New Issue
Block a user