fix tcp congestion

This commit is contained in:
Eduardo Bart 2012-02-01 20:46:31 -02:00
parent 20390d6684
commit 289efe34cf
5 changed files with 33 additions and 8 deletions

View File

@ -33,7 +33,7 @@ public:
Event(const SimpleCallback& callback) : m_callback(callback), m_canceled(false), m_executed(false) { } Event(const SimpleCallback& callback) : m_callback(callback), m_canceled(false), m_executed(false) { }
void execute() { void execute() {
if(!m_canceled) { if(!m_canceled && !m_executed) {
m_callback(); m_callback();
m_executed = true; m_executed = true;
} }

View File

@ -35,6 +35,7 @@ Connection::Connection() :
{ {
m_connected = false; m_connected = false;
m_connecting = false; m_connecting = false;
m_sendBufferSize = 0;
} }
Connection::~Connection() Connection::~Connection()
@ -104,17 +105,30 @@ void Connection::close()
void Connection::write(uint8* buffer, uint16 size) void Connection::write(uint8* buffer, uint16 size)
{ {
m_writeTimer.cancel();
if(!m_connected) if(!m_connected)
return; return;
// we can't send right, otherwise we could create tcp congestion
memcpy(m_sendBuffer + m_sendBufferSize, buffer, size);
m_sendBufferSize += size;
if(!m_sendEvent || m_sendEvent->isExecuted() || m_sendEvent->isCanceled()) {
auto weakSelf = ConnectionWeakPtr(shared_from_this());
m_sendEvent = g_dispatcher.scheduleEvent([=] {
if(!weakSelf.lock())
return;
//m_writeTimer.cancel();
asio::async_write(m_socket, asio::async_write(m_socket,
asio::buffer(buffer, size), asio::buffer(m_sendBuffer, m_sendBufferSize),
std::bind(&Connection::onWrite, shared_from_this(), _1, _2)); std::bind(&Connection::onWrite, shared_from_this(), _1, _2));
m_writeTimer.expires_from_now(boost::posix_time::seconds(WRITE_TIMEOUT)); m_writeTimer.expires_from_now(boost::posix_time::seconds(WRITE_TIMEOUT));
m_writeTimer.async_wait(std::bind(&Connection::onTimeout, shared_from_this(), _1)); m_writeTimer.async_wait(std::bind(&Connection::onTimeout, shared_from_this(), _1));
m_sendBufferSize = 0;
}, SEND_INTERVAL);
}
} }
void Connection::read(uint16 bytes, const RecvCallback& callback) void Connection::read(uint16 bytes, const RecvCallback& callback)
@ -158,6 +172,7 @@ void Connection::onConnect(const boost::system::error_code& error)
m_connected = true; m_connected = true;
// disable nagle's algorithm // disable nagle's algorithm
//TODO: implement custom cache
boost::asio::ip::tcp::no_delay option(true); boost::asio::ip::tcp::no_delay option(true);
m_socket.set_option(option); m_socket.set_option(option);

View File

@ -25,6 +25,8 @@
#include "declarations.h" #include "declarations.h"
#include <boost/asio.hpp> #include <boost/asio.hpp>
#include <framework/core/timer.h>
#include <framework/core/declarations.h>
class Connection : public std::enable_shared_from_this<Connection>, boost::noncopyable class Connection : public std::enable_shared_from_this<Connection>, boost::noncopyable
{ {
@ -34,6 +36,8 @@ class Connection : public std::enable_shared_from_this<Connection>, boost::nonco
enum { enum {
READ_TIMEOUT = 30, READ_TIMEOUT = 30,
WRITE_TIMEOUT = 30, WRITE_TIMEOUT = 30,
SEND_INTERVAL = 1,
SEND_BUFFER_SIZE = 65536,
RECV_BUFFER_SIZE = 65536 RECV_BUFFER_SIZE = 65536
}; };
@ -72,9 +76,13 @@ protected:
asio::ip::tcp::resolver m_resolver; asio::ip::tcp::resolver m_resolver;
asio::ip::tcp::socket m_socket; asio::ip::tcp::socket m_socket;
uint8 m_sendBuffer[SEND_BUFFER_SIZE];
uint8 m_recvBuffer[RECV_BUFFER_SIZE]; uint8 m_recvBuffer[RECV_BUFFER_SIZE];
bool m_connected; bool m_connected;
bool m_connecting; bool m_connecting;
int m_sendBufferSize;
Timer m_sendTimer;
ScheduledEventPtr m_sendEvent;
friend class Server; friend class Server;
}; };

View File

@ -307,6 +307,7 @@ std::vector<CreaturePtr> Map::getSpectatorsInRangeEx(const Position& centerPos,
} }
//TODO: get creatures from other floors corretly //TODO: get creatures from other floors corretly
//TODO: delivery creatures in distance order
for(int iz=-minZRange; iz<=maxZRange; ++iz) { for(int iz=-minZRange; iz<=maxZRange; ++iz) {
for(int iy=-minYRange; iy<=maxYRange; ++iy) { for(int iy=-minYRange; iy<=maxYRange; ++iy) {

View File

@ -138,6 +138,7 @@ void OTClient::registerLuaFunctions()
g_lua.registerClass<AnimatedText, Thing>(); g_lua.registerClass<AnimatedText, Thing>();
g_lua.registerClass<Player, Creature>(); g_lua.registerClass<Player, Creature>();
g_lua.bindClassMemberFunction<Creature>("isWalking", &Creature::isWalking);
g_lua.registerClass<Npc, Creature>(); g_lua.registerClass<Npc, Creature>();
g_lua.registerClass<Monster, Creature>(); g_lua.registerClass<Monster, Creature>();
g_lua.registerClass<LocalPlayer, Player>(); g_lua.registerClass<LocalPlayer, Player>();