Adding websocket server code, so that a gateway is no longer required. Still kind of untested. Requires websocketpp.

This commit is contained in:
lotodore
2013-09-15 22:54:45 +02:00
parent b0e4018883
commit 8e39432754
19 changed files with 175 additions and 376 deletions
+11 -57
View File
@@ -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 *
@@ -28,77 +28,31 @@
* shall include the source code for the parts of OpenSSL used as well *
* as that of the covered work. *
*****************************************************************************/
/* Buffer for sending network data. */
/* Buffer interface for sending network data. */
#ifndef _SENDBUFFER_H_
#define _SENDBUFFER_H_
#include <boost/asio.hpp>
#include <boost/thread.hpp>
#include <net/websocket_defs.h>
#include <boost/enable_shared_from_this.hpp>
#include <cstdlib>
#define SEND_BUF_FIRST_ALLOC_CHUNKSIZE 4096
#define MAX_SEND_BUF_SIZE SEND_BUF_FIRST_ALLOC_CHUNKSIZE * 256
#include <boost/thread.hpp>
class SessionData;
class NetPacket;
class SendBuffer : public boost::enable_shared_from_this<SendBuffer>
{
public:
SendBuffer();
~SendBuffer();
virtual ~SendBuffer();
inline size_t GetSendBufLeft() const {
int bytesLeft = (int)(sendBufAllocated - sendBufUsed);
return bytesLeft < 0 ? (size_t)0 : (size_t)bytesLeft;
}
virtual void SetCloseAfterSend() = 0;
inline size_t GetAllocated() const {
return sendBufAllocated;
}
virtual void AsyncSendNextPacket(boost::shared_ptr<SessionData> session) = 0;
virtual void InternalStorePacket(boost::shared_ptr<SessionData> session, boost::shared_ptr<NetPacket> packet) = 0;
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;
}
inline void SetCloseAfterSend() {
closeAfterSend = true;
}
void HandleWrite(boost::shared_ptr<boost::asio::ip::tcp::socket> socket, const boost::system::error_code &error);
void AsyncSendNextPacket(boost::shared_ptr<boost::asio::ip::tcp::socket> socket);
static int EncodeToBuf(const void *data, size_t size, void *arg);
virtual void HandleWrite(boost::shared_ptr<boost::asio::ip::tcp::socket> socket, const boost::system::error_code &error) = 0;
mutable boost::mutex dataMutex;
private:
char *sendBuf;
char *curWriteBuf;
size_t sendBufAllocated;
size_t sendBufUsed;
size_t curWriteBufAllocated;
size_t curWriteBufUsed;
bool closeAfterSend;
};
#endif