From 1c98ec94da18c97272e1b0a2c1e43baf0683a187 Mon Sep 17 00:00:00 2001 From: Kahrl Date: Mon, 7 Nov 2011 04:20:33 +0100 Subject: [PATCH] Make Connection::Receive return the data via a SharedBuffer reference, so the caller doesn't have to choose the right buffer size in advance. Conflicts: src/test.cpp --- src/client.cpp | 5 ++--- src/connection.cpp | 4 ++-- src/connection.h | 2 +- src/server.cpp | 5 ++--- src/test.cpp | 52 +++++++++++++++++++++++----------------------- 5 files changed, 33 insertions(+), 35 deletions(-) diff --git a/src/client.cpp b/src/client.cpp index a777293a3..69c91bc30 100644 --- a/src/client.cpp +++ b/src/client.cpp @@ -665,14 +665,13 @@ void Client::ReceiveAll() void Client::Receive() { DSTACK(__FUNCTION_NAME); - u32 data_maxsize = 200000; - Buffer data(data_maxsize); + SharedBuffer data; u16 sender_peer_id; u32 datasize; { //TimeTaker t1("con mutex and receive", m_device); //JMutexAutoLock lock(m_con_mutex); //bulk comment-out - datasize = m_con.Receive(sender_peer_id, *data, data_maxsize); + datasize = m_con.Receive(sender_peer_id, data); } //TimeTaker t1("ProcessData", m_device); ProcessData(*data, datasize, sender_peer_id); diff --git a/src/connection.cpp b/src/connection.cpp index 31c0f77a3..b9c5d2ac8 100644 --- a/src/connection.cpp +++ b/src/connection.cpp @@ -1577,7 +1577,7 @@ void Connection::Disconnect() putCommand(c); } -u32 Connection::Receive(u16 &peer_id, u8 *data, u32 datasize) +u32 Connection::Receive(u16 &peer_id, SharedBuffer &data) { for(;;){ ConnectionEvent e = waitEvent(m_bc_receive_timeout); @@ -1589,7 +1589,7 @@ u32 Connection::Receive(u16 &peer_id, u8 *data, u32 datasize) throw NoIncomingDataException("No incoming data"); case CONNEVENT_DATA_RECEIVED: peer_id = e.peer_id; - memcpy(data, *e.data, e.data.getSize()); + data = SharedBuffer(e.data); return e.data.getSize(); case CONNEVENT_PEER_ADDED: { Peer tmp(e.peer_id, e.address); diff --git a/src/connection.h b/src/connection.h index dc61394fa..6d26e2e35 100644 --- a/src/connection.h +++ b/src/connection.h @@ -551,7 +551,7 @@ public: void Connect(Address address); bool Connected(); void Disconnect(); - u32 Receive(u16 &peer_id, u8 *data, u32 datasize); + u32 Receive(u16 &peer_id, SharedBuffer &data); void SendToAll(u8 channelnum, SharedBuffer data, bool reliable); void Send(u16 peer_id, u8 channelnum, SharedBuffer data, bool reliable); void RunTimeouts(float dtime); // dummy diff --git a/src/server.cpp b/src/server.cpp index 37ba65a95..52e9dc879 100644 --- a/src/server.cpp +++ b/src/server.cpp @@ -1777,14 +1777,13 @@ void Server::AsyncRunStep() void Server::Receive() { DSTACK(__FUNCTION_NAME); - u32 data_maxsize = 10000; - Buffer data(data_maxsize); + SharedBuffer data; u16 peer_id; u32 datasize; try{ { JMutexAutoLock conlock(m_con_mutex); - datasize = m_con.Receive(peer_id, *data, data_maxsize); + datasize = m_con.Receive(peer_id, data); } // This has to be called so that the client list gets synced diff --git a/src/test.cpp b/src/test.cpp index db8db4ecc..37412d179 100644 --- a/src/test.cpp +++ b/src/test.cpp @@ -852,9 +852,9 @@ struct TestConnection try { u16 peer_id; - u8 data[100]; + SharedBuffer data; infostream<<"** running client.Receive()"< recvdata; u32 size; infostream<<"** running client.Receive()"< 5000) break; try{ - size = client.Receive(peer_id, recvdata, datasize + 1000); + size = client.Receive(peer_id, recvdata); received = true; }catch(con::NoIncomingDataException &e){ } @@ -1116,13 +1116,13 @@ struct TestConnection infostream<<"Received data (size="<20) infostream<<"..."; infostream<