summaryrefslogtreecommitdiffstats
path: root/src/Net/Connection.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/Net/Connection.cpp')
-rw-r--r--src/Net/Connection.cpp39
1 files changed, 19 insertions, 20 deletions
diff --git a/src/Net/Connection.cpp b/src/Net/Connection.cpp
index 305e1de..036c3d8 100644
--- a/src/Net/Connection.cpp
+++ b/src/Net/Connection.cpp
@@ -61,7 +61,7 @@ void Connection::handleShutdown(const boost::system::error_code& error) {
boost::lock_guard<boost::shared_mutex> lock(connectionLock);
if(error) {
- application->logf(Core::LoggerBase::VERBOSE, "Shutdown error: %s", error.message().c_str());
+ application->logf(Core::LoggerBase::LOG_VERBOSE, "Shutdown error: %s", error.message().c_str());
}
_setState(DISCONNECTED);
@@ -77,14 +77,14 @@ void Connection::enterReceiveLoop() {
return;
}
- rawReceive(sizeof(Packet::Data), boost::bind(&Connection::handleHeaderReceive, this, _1));
+ rawReceive(sizeof(Packet::Header), boost::bind(&Connection::handleHeaderReceive, this, _1));
}
-void Connection::handleHeaderReceive(const std::vector<boost::uint8_t> &data) {
+void Connection::handleHeaderReceive(const boost::shared_array<boost::uint8_t> &data) {
{
boost::lock_guard<boost::shared_mutex> lock(connectionLock);
- header = *reinterpret_cast<const Packet::Data*>(data.data());
+ header = *reinterpret_cast<const Packet::Header*>(data.get());
}
if(header.length == 0) {
@@ -97,35 +97,34 @@ void Connection::handleHeaderReceive(const std::vector<boost::uint8_t> &data) {
}
}
-void Connection::handleDataReceive(const std::vector<boost::uint8_t> &data) {
+void Connection::handleDataReceive(const boost::shared_array<boost::uint8_t> &data) {
{
boost::upgrade_lock<boost::shared_mutex> lock(connectionLock);
- Packet packet();
- receiveSignal.emit(boost::shared_ptr<Packet>(new Packet(ntohs(header.requestId), data.data(), ntohs(header.length))));
+ receiveSignal.emit(boost::shared_ptr<Packet>(new Packet(ntohs(header.requestId), data.get(), ntohs(header.length))));
}
enterReceiveLoop();
}
-void Connection::handleRead(const boost::system::error_code& error, std::size_t bytes_transferred, std::size_t length, const boost::function1<void, const std::vector<boost::uint8_t>& > &notify) {
+void Connection::handleRead(const boost::system::error_code& error, std::size_t bytes_transferred, std::size_t length, const boost::function1<void, const boost::shared_array<boost::uint8_t>& > &notify) {
if(error || (bytes_transferred+received) < length) {
- application->logf(Core::LoggerBase::VERBOSE, "Read error: %s", error.message().c_str());
+ application->logf(Core::LoggerBase::LOG_VERBOSE, "Read error: %s", error.message().c_str());
// TODO Error
doDisconnect();
return;
}
- std::vector<boost::uint8_t> buffer;
+ boost::shared_array<boost::uint8_t> buffer(new boost::uint8_t[length]);
{
boost::shared_lock<boost::shared_mutex> lock(connectionLock);
if(state != CONNECTED || !receiving)
return;
-
- buffer.insert(buffer.end(), receiveBuffer.data(), receiveBuffer.data()+length);
+
+ std::memcpy(buffer.get(), receiveBuffer->data(), length);
}
{
@@ -135,13 +134,13 @@ void Connection::handleRead(const boost::system::error_code& error, std::size_t
received = received + bytes_transferred - length;
if(received)
- std::memmove(receiveBuffer.data(), receiveBuffer.data()+length, received);
+ std::memmove(receiveBuffer->data(), receiveBuffer->data()+length, received);
}
notify(buffer);
}
-void Connection::rawReceive(std::size_t length, const boost::function1<void, const std::vector<boost::uint8_t>& > &notify) {
+void Connection::rawReceive(std::size_t length, const boost::function1<void, const boost::shared_array<boost::uint8_t>& > &notify) {
boost::upgrade_lock<boost::shared_mutex> lock(connectionLock);
if(!_isConnected())
@@ -156,7 +155,7 @@ void Connection::rawReceive(std::size_t length, const boost::function1<void, con
receiving = true;
if(length > received) {
- boost::asio::async_read(socket, boost::asio::buffer(receiveBuffer.data()+received, receiveBuffer.size()-received), boost::asio::transfer_at_least(length),
+ boost::asio::async_read(*socket, boost::asio::buffer(receiveBuffer->data()+received, receiveBuffer->size()-received), boost::asio::transfer_at_least(length),
boost::bind(&Connection::handleRead, this, boost::asio::placeholders::error, boost::asio::placeholders::bytes_transferred,
length, notify));
@@ -183,14 +182,14 @@ void Connection::handleWrite(const boost::system::error_code& error, std::size_t
}
if(error) {
- application->logf(Core::LoggerBase::VERBOSE, "Write error: %s", error.message().c_str());
+ application->logf(Core::LoggerBase::LOG_VERBOSE, "Write error: %s", error.message().c_str());
// TODO Error
doDisconnect();
}
}
-void Connection::rawSend(const uint8_t *data, std::size_t length) {
+void Connection::rawSend(const boost::uint8_t *data, std::size_t length) {
boost::upgrade_lock<boost::shared_mutex> lock(connectionLock);
if(!_isConnected())
@@ -200,7 +199,7 @@ void Connection::rawSend(const uint8_t *data, std::size_t length) {
boost::upgrade_to_unique_lock<boost::shared_mutex> upgradeLock(lock);
sending++;
- boost::asio::async_write(socket, Buffer(data, length), boost::bind(&Connection::handleWrite, this, boost::asio::placeholders::error, boost::asio::placeholders::bytes_transferred));
+ boost::asio::async_write(*socket, Buffer(data, length), boost::bind(&Connection::handleWrite, this, boost::asio::placeholders::error, boost::asio::placeholders::bytes_transferred));
}
}
@@ -211,7 +210,7 @@ bool Connection::send(const Packet &packet) {
return false;
}
- rawSend((const uint8_t*)packet.getRawData(), packet.getRawDataLength());
+ rawSend((const boost::uint8_t*)packet.getRawData(), packet.getRawDataLength());
return true;
}
@@ -233,7 +232,7 @@ void Connection::disconnect() {
void Connection::doDisconnect() {
boost::lock_guard<boost::shared_mutex> lock(connectionLock);
- socket.async_shutdown(boost::bind(&Connection::handleShutdown, this, boost::asio::placeholders::error));
+ socket->async_shutdown(boost::bind(&Connection::handleShutdown, this, boost::asio::placeholders::error));
}
}