Adding missing files for websocket implementation.

This commit is contained in:
lotodore
2013-09-15 23:06:15 +02:00
parent e6b165fe59
commit 36eeaee5d7
14 changed files with 1055 additions and 0 deletions
+62
View File
@@ -0,0 +1,62 @@
/*****************************************************************************
* PokerTH - The open source texas holdem engine *
* 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 *
* 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. *
*****************************************************************************/
/* ASIO standard socket receive buffer. */
#ifndef _ASIORECEIVEBUFFER_H_
#define _ASIORECEIVEBUFFER_H_
#include <net/receivebuffer.h>
// MUST be larger than MAX_PACKET_SIZE
#define RECV_BUF_SIZE 5 * MAX_PACKET_SIZE
class AsioReceiveBuffer : public ReceiveBuffer
{
public:
AsioReceiveBuffer();
virtual void StartAsyncRead(boost::shared_ptr<SessionData> session);
virtual void HandleRead(boost::shared_ptr<SessionData> session, const boost::system::error_code &error, size_t bytesRead);
virtual void HandleMessage(boost::shared_ptr<SessionData> session, const std::string &msg);
protected:
void ScanPackets(boost::shared_ptr<SessionData> session);
void ProcessPackets(boost::shared_ptr<SessionData> session);
private:
NetPacketList receivedPackets;
char recvBuf[RECV_BUF_SIZE];
size_t recvBufUsed;
};
#endif
+101
View File
@@ -0,0 +1,101 @@
/*****************************************************************************
* PokerTH - The open source texas holdem engine *
* 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 *
* 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. *
*****************************************************************************/
/* Buffer for sending network data. */
#ifndef _ASIOSENDBUFFER_H_
#define _ASIOSENDBUFFER_H_
#include <net/sendbuffer.h>
#include <cstdlib>
#define SEND_BUF_FIRST_ALLOC_CHUNKSIZE 4096
#define MAX_SEND_BUF_SIZE SEND_BUF_FIRST_ALLOC_CHUNKSIZE * 256
class AsioSendBuffer : public SendBuffer
{
public:
AsioSendBuffer();
virtual ~AsioSendBuffer();
inline size_t GetSendBufLeft() const {
int bytesLeft = (int)(sendBufAllocated - sendBufUsed);
return bytesLeft < 0 ? (size_t)0 : (size_t)bytesLeft;
}
inline size_t GetAllocated() const {
return sendBufAllocated;
}
inline bool ReallocSendBuf() {
bool retVal = false;
size_t allocAmount = sendBufAllocated * 2;
if (0 == allocAmount) {
allocAmount = (size_t)SEND_BUF_FIRST_ALLOC_CHUNKSIZE;
}
if (allocAmount <= MAX_SEND_BUF_SIZE) {
char *tempBuf = (char *)std::realloc(sendBuf, allocAmount);
if (tempBuf) {
sendBuf = tempBuf;
sendBufAllocated = allocAmount;
retVal = true;
}
}
return retVal;
}
inline void AppendToSendBufWithoutCheck(const char *data, size_t size) {
std::memcpy(sendBuf + sendBufUsed, data, size);
sendBufUsed += size;
}
virtual void SetCloseAfterSend();
virtual void AsyncSendNextPacket(boost::shared_ptr<SessionData> session);
void AsyncSendNextPacket(boost::shared_ptr<boost::asio::ip::tcp::socket> socket);
virtual void InternalStorePacket(boost::shared_ptr<SessionData> session, boost::shared_ptr<NetPacket> packet);
int EncodeToBuf(boost::shared_ptr<SessionData> session, const void *data, size_t size);
virtual void HandleWrite(boost::shared_ptr<boost::asio::ip::tcp::socket> socket, const boost::system::error_code &error);
private:
char *sendBuf;
char *curWriteBuf;
size_t sendBufAllocated;
size_t sendBufUsed;
size_t curWriteBufAllocated;
size_t curWriteBufUsed;
bool closeAfterSend;
};
#endif
+150
View File
@@ -0,0 +1,150 @@
/*****************************************************************************
* 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 <boost/asio.hpp>
#include <boost/bind.hpp>
#include <net/asioreceivebuffer.h>
#include <net/sessiondata.h>
#include <core/loghelper.h>
#include <boost/swap.hpp>
using namespace std;
AsioReceiveBuffer::AsioReceiveBuffer()
: recvBufUsed(0)
{
recvBuf[0] = 0;
}
void
AsioReceiveBuffer::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
AsioReceiveBuffer::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
AsioReceiveBuffer::HandleMessage(boost::shared_ptr<SessionData> /*session*/, const string &/*msg*/)
{
LOG_ERROR("AsioReceiveBuffer::HandleMessage should never be called because TCP I/O is not message based.");
}
void
AsioReceiveBuffer::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
AsioReceiveBuffer::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;
}
}
+126
View File
@@ -0,0 +1,126 @@
/*****************************************************************************
* PokerTH - The open source texas holdem engine *
* 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 *
* 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 <boost/asio.hpp>
#include <boost/bind.hpp>
#include <net/asiosendbuffer.h>
#include <net/sessiondata.h>
#include <net/netpacket.h>
#include <boost/swap.hpp>
using namespace std;
AsioSendBuffer::AsioSendBuffer()
: sendBuf(NULL), curWriteBuf(NULL), sendBufAllocated(0), sendBufUsed(0),
curWriteBufAllocated(0), curWriteBufUsed(0), closeAfterSend(false)
{
}
AsioSendBuffer::~AsioSendBuffer()
{
free(sendBuf);
free(curWriteBuf);
}
void
AsioSendBuffer::SetCloseAfterSend()
{
closeAfterSend = true;
}
void
AsioSendBuffer::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
AsioSendBuffer::AsyncSendNextPacket(boost::shared_ptr<SessionData> session)
{
AsyncSendNextPacket(session->GetAsioSocket());
}
void
AsioSendBuffer::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();
}
}
}
void
AsioSendBuffer::InternalStorePacket(boost::shared_ptr<SessionData> session, 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]);
EncodeToBuf(session, buf, packetSize + NET_HEADER_SIZE);
delete[] buf;
}
int
AsioSendBuffer::EncodeToBuf(boost::shared_ptr<SessionData> /*session*/, const void *data, size_t size)
{
// Realloc buffer if necessary.
while (GetSendBufLeft() < size) {
if (!ReallocSendBuf()) {
return -1;
}
}
AppendToSendBufWithoutCheck((const char*)data, size);
return 0;
}
+39
View File
@@ -0,0 +1,39 @@
/*****************************************************************************
* PokerTH - The open source texas holdem engine *
* 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 *
* 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/serveracceptinterface.h>
ServerAcceptInterface::~ServerAcceptInterface()
{
}
+116
View File
@@ -0,0 +1,116 @@
/*****************************************************************************
* PokerTH - The open source texas holdem engine *
* 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 *
* 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/serveracceptwebhelper.h>
#include <net/sessiondata.h>
#include <net/webreceivebuffer.h>
#include <net/websocketdata.h>
using namespace std;
ServerAcceptWebHelper::ServerAcceptWebHelper(ServerCallback &serverCallback, boost::shared_ptr<boost::asio::io_service> ioService)
: m_serverCallback(serverCallback), m_ioService(ioService)
{
m_webSocketServer.reset(new server);
}
void
ServerAcceptWebHelper::Listen(unsigned serverPort, bool /*ipv6*/, const std::string &/*logDir*/, boost::shared_ptr<ServerLobbyThread> lobbyThread)
{
m_lobbyThread = lobbyThread;
// Set logging settings
#ifdef QT_NO_DEBUG
m_webSocketServer->clear_access_channels(websocketpp::log::alevel::all);
#else
m_webSocketServer->set_access_channels(websocketpp::log::alevel::all);
#endif
m_webSocketServer->init_asio(m_ioService.get());
m_webSocketServer->set_validate_handler(boost::bind(boost::mem_fn(&ServerAcceptWebHelper::validate), this, _1));
m_webSocketServer->set_open_handler(boost::bind(boost::mem_fn(&ServerAcceptWebHelper::on_open), this, _1));
m_webSocketServer->set_close_handler(boost::bind(boost::mem_fn(&ServerAcceptWebHelper::on_close), this, _1));
m_webSocketServer->set_message_handler(boost::bind(boost::mem_fn(&ServerAcceptWebHelper::on_message), this, _1, _2));
m_webSocketServer->listen(serverPort);
m_webSocketServer->start_accept();
}
void
ServerAcceptWebHelper::Close()
{
}
bool
ServerAcceptWebHelper::validate(websocketpp::connection_hdl hdl)
{
return true;
}
void
ServerAcceptWebHelper::on_open(websocketpp::connection_hdl hdl)
{
boost::shared_ptr<WebSocketData> webData(new WebSocketData);
webData->webSocketServer = m_webSocketServer;
webData->webHandle = hdl;
boost::shared_ptr<SessionData> sessionData(new SessionData(webData, m_lobbyThread->GetNextSessionId(), m_lobbyThread->GetSessionDataCallback(), *m_ioService, 0));
m_sessionMap.insert(make_pair(hdl, sessionData));
m_lobbyThread->AddConnection(sessionData);
}
void
ServerAcceptWebHelper::on_close(websocketpp::connection_hdl hdl)
{
SessionMap::iterator pos = m_sessionMap.find(hdl);
if (pos != m_sessionMap.end()) {
boost::shared_ptr<SessionData> tmpSession = pos->second.lock();
if (tmpSession) {
tmpSession->Close();
}
m_sessionMap.erase(pos);
}
}
void
ServerAcceptWebHelper::on_message(websocketpp::connection_hdl hdl, server::message_ptr msg)
{
if (msg->get_opcode() == websocketpp::frame::opcode::BINARY) {
SessionMap::iterator pos = m_sessionMap.find(hdl);
if (pos != m_sessionMap.end()) {
boost::shared_ptr<SessionData> tmpSession = pos->second.lock();
if (tmpSession) {
tmpSession->GetReceiveBuffer().HandleMessage(tmpSession, msg->get_payload());
}
}
}
}
+72
View File
@@ -0,0 +1,72 @@
/*****************************************************************************
* PokerTH - The open source texas holdem engine *
* 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 *
* 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/sessiondata.h>
#include <net/webreceivebuffer.h>
#include <core/loghelper.h>
using namespace std;
WebReceiveBuffer::WebReceiveBuffer()
{
}
void
WebReceiveBuffer::StartAsyncRead(boost::shared_ptr<SessionData> session)
{
// Nothing to do. This is handled internally by websocketpp.
}
void
WebReceiveBuffer::HandleRead(boost::shared_ptr<SessionData> /*session*/, const boost::system::error_code &/*error*/, size_t /*bytesRead*/)
{
LOG_ERROR("WebReceiveBuffer::HandleRead should never be called because Websocket I/O is message based.");
}
void
WebReceiveBuffer::HandleMessage(boost::shared_ptr<SessionData> session, const string &msg)
{
boost::shared_ptr<NetPacket> tmpPacket;
try {
tmpPacket = NetPacket::Create(msg.c_str(), msg.size());
if (!validator.IsValidPacket(*tmpPacket)) {
LOG_ERROR("Session " << session->GetId() << " - Invalid packet: " << tmpPacket->GetMsg()->messagetype());
tmpPacket.reset();
}
} catch (const exception &e) {
LOG_ERROR("Session " << session->GetId() << " - " << e.what());
}
if (tmpPacket) {
session->HandlePacket(tmpPacket);
}
}
+75
View File
@@ -0,0 +1,75 @@
/*****************************************************************************
* 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/websendbuffer.h>
#include <net/websocketdata.h>
#include <net/netpacket.h>
using namespace std;
WebSendBuffer::WebSendBuffer(boost::shared_ptr<WebSocketData> webData)
: closeAfterSend(false), m_webData(webData)
{
}
void
WebSendBuffer::SetCloseAfterSend()
{
closeAfterSend = true;
}
void
WebSendBuffer::HandleWrite(boost::shared_ptr<boost::asio::ip::tcp::socket> /*socket*/, const boost::system::error_code &/*error*/)
{
}
void
WebSendBuffer::AsyncSendNextPacket(boost::shared_ptr<SessionData> session)
{
if (closeAfterSend) {
boost::system::error_code ec;
m_webData->webSocketServer->close(m_webData->webHandle, websocketpp::close::status::normal, "PokerTH server closed the connection.", ec);
}
}
void
WebSendBuffer::InternalStorePacket(boost::shared_ptr<SessionData> session, boost::shared_ptr<NetPacket> packet)
{
uint32_t packetSize = packet->GetMsg()->ByteSize();
google::protobuf::uint8 *buf = new google::protobuf::uint8[packetSize];
packet->GetMsg()->SerializeWithCachedSizesToArray(buf);
m_webData->webSocketServer->send(m_webData->webHandle, string((const char *)buf, packetSize), websocketpp::frame::opcode::BINARY);
delete[] buf;
}
+53
View File
@@ -0,0 +1,53 @@
/*****************************************************************************
* PokerTH - The open source texas holdem engine *
* 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 *
* 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. *
*****************************************************************************/
/* Network server helper interface to accept connections. */
#ifndef _SERVERACCEPINTERFACE_H_
#define _SERVERACCEPINTERFACE_H_
#include <string>
#include <boost/shared_ptr.hpp>
class ServerLobbyThread;
class ServerAcceptInterface
{
public:
virtual ~ServerAcceptInterface();
virtual void Listen(unsigned serverPort, bool ipv6, const std::string &logDir,
boost::shared_ptr<ServerLobbyThread> lobbyThread) = 0;
virtual void Close() = 0;
};
#endif
+68
View File
@@ -0,0 +1,68 @@
/*****************************************************************************
* PokerTH - The open source texas holdem engine *
* 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 *
* 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. *
*****************************************************************************/
/* Network server helper to accept websocket connections. */
#ifndef _SERVERACCEPTWEBHELPER_H_
#define _SERVERACCEPTWEBHELPER_H_
#include <net/websocket_defs.h>
#include <net/serveracceptinterface.h>
#include <net/serverlobbythread.h>
class ServerAcceptWebHelper : public ServerAcceptInterface
{
public:
ServerAcceptWebHelper(ServerCallback &serverCallback, boost::shared_ptr<boost::asio::io_service> ioService);
virtual void Listen(unsigned serverPort, bool ipv6, const std::string &logDir,
boost::shared_ptr<ServerLobbyThread> lobbyThread);
virtual void Close();
protected:
typedef std::map<websocketpp::connection_hdl, boost::weak_ptr<SessionData> > SessionMap;
bool validate(websocketpp::connection_hdl hdl);
void on_open(websocketpp::connection_hdl hdl);
void on_close(websocketpp::connection_hdl hdl);
void on_message(websocketpp::connection_hdl hdl, server::message_ptr msg);
private:
boost::shared_ptr<boost::asio::io_service> m_ioService;
ServerCallback &m_serverCallback;
boost::shared_ptr<server> m_webSocketServer;
SessionMap m_sessionMap;
boost::shared_ptr<ServerLobbyThread> m_lobbyThread;
};
#endif
+48
View File
@@ -0,0 +1,48 @@
/*****************************************************************************
* PokerTH - The open source texas holdem engine *
* 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 *
* 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. *
*****************************************************************************/
/* Websocket receive buffer. */
#ifndef _WEBRECEIVEBUFFER_H_
#define _WEBRECEIVEBUFFER_H_
#include <net/receivebuffer.h>
class WebReceiveBuffer : public ReceiveBuffer
{
public:
WebReceiveBuffer();
virtual void StartAsyncRead(boost::shared_ptr<SessionData> session);
virtual void HandleRead(boost::shared_ptr<SessionData> session, const boost::system::error_code &error, size_t bytesRead);
virtual void HandleMessage(boost::shared_ptr<SessionData> session, const std::string &msg);
};
#endif
+59
View File
@@ -0,0 +1,59 @@
/*****************************************************************************
* PokerTH - The open source texas holdem engine *
* 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 *
* 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. *
*****************************************************************************/
/* Buffer for sending network data through a websocket. */
#ifndef _WEBSENDBUFFER_H_
#define _WEBSENDBUFFER_H_
#include <net/sendbuffer.h>
#include <cstdlib>
struct WebSocketData;
class WebSendBuffer : public SendBuffer
{
public:
WebSendBuffer(boost::shared_ptr<WebSocketData> webData);
virtual void SetCloseAfterSend();
virtual void AsyncSendNextPacket(boost::shared_ptr<SessionData> session);
virtual void InternalStorePacket(boost::shared_ptr<SessionData> session, boost::shared_ptr<NetPacket> packet);
virtual void HandleWrite(boost::shared_ptr<boost::asio::ip::tcp::socket> socket, const boost::system::error_code &error);
private:
bool closeAfterSend;
boost::shared_ptr<WebSocketData> m_webData;
};
#endif
+41
View File
@@ -0,0 +1,41 @@
/*****************************************************************************
* PokerTH - The open source texas holdem engine *
* 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 *
* 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. *
*****************************************************************************/
/* Definitions and includes for websocketpp. */
#ifndef _WEBSOCKET_DEFS_H_
#define _WEBSOCKET_DEFS_H_
#include <websocketpp/config/asio_no_tls.hpp>
#include <websocketpp/server.hpp>
typedef websocketpp::server<websocketpp::config::asio> server;
#endif
+45
View File
@@ -0,0 +1,45 @@
/*****************************************************************************
* PokerTH - The open source texas holdem engine *
* 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 *
* 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. *
*****************************************************************************/
/* Structure for web socket data for a session. */
#ifndef _WEBSOCKETDATA_H_
#define _WEBSOCKETDATA_H_
#include <net/websocket_defs.h>
struct WebSocketData
{
boost::shared_ptr<server> webSocketServer;
websocketpp::connection_hdl webHandle;
};
#endif