mirror of
https://github.com/moparisthebest/minetest
synced 2024-12-23 16:08:51 -05:00
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
This commit is contained in:
parent
28660b4c1a
commit
1c98ec94da
@ -665,14 +665,13 @@ void Client::ReceiveAll()
|
|||||||
void Client::Receive()
|
void Client::Receive()
|
||||||
{
|
{
|
||||||
DSTACK(__FUNCTION_NAME);
|
DSTACK(__FUNCTION_NAME);
|
||||||
u32 data_maxsize = 200000;
|
SharedBuffer<u8> data;
|
||||||
Buffer<u8> data(data_maxsize);
|
|
||||||
u16 sender_peer_id;
|
u16 sender_peer_id;
|
||||||
u32 datasize;
|
u32 datasize;
|
||||||
{
|
{
|
||||||
//TimeTaker t1("con mutex and receive", m_device);
|
//TimeTaker t1("con mutex and receive", m_device);
|
||||||
//JMutexAutoLock lock(m_con_mutex); //bulk comment-out
|
//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);
|
//TimeTaker t1("ProcessData", m_device);
|
||||||
ProcessData(*data, datasize, sender_peer_id);
|
ProcessData(*data, datasize, sender_peer_id);
|
||||||
|
@ -1577,7 +1577,7 @@ void Connection::Disconnect()
|
|||||||
putCommand(c);
|
putCommand(c);
|
||||||
}
|
}
|
||||||
|
|
||||||
u32 Connection::Receive(u16 &peer_id, u8 *data, u32 datasize)
|
u32 Connection::Receive(u16 &peer_id, SharedBuffer<u8> &data)
|
||||||
{
|
{
|
||||||
for(;;){
|
for(;;){
|
||||||
ConnectionEvent e = waitEvent(m_bc_receive_timeout);
|
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");
|
throw NoIncomingDataException("No incoming data");
|
||||||
case CONNEVENT_DATA_RECEIVED:
|
case CONNEVENT_DATA_RECEIVED:
|
||||||
peer_id = e.peer_id;
|
peer_id = e.peer_id;
|
||||||
memcpy(data, *e.data, e.data.getSize());
|
data = SharedBuffer<u8>(e.data);
|
||||||
return e.data.getSize();
|
return e.data.getSize();
|
||||||
case CONNEVENT_PEER_ADDED: {
|
case CONNEVENT_PEER_ADDED: {
|
||||||
Peer tmp(e.peer_id, e.address);
|
Peer tmp(e.peer_id, e.address);
|
||||||
|
@ -551,7 +551,7 @@ public:
|
|||||||
void Connect(Address address);
|
void Connect(Address address);
|
||||||
bool Connected();
|
bool Connected();
|
||||||
void Disconnect();
|
void Disconnect();
|
||||||
u32 Receive(u16 &peer_id, u8 *data, u32 datasize);
|
u32 Receive(u16 &peer_id, SharedBuffer<u8> &data);
|
||||||
void SendToAll(u8 channelnum, SharedBuffer<u8> data, bool reliable);
|
void SendToAll(u8 channelnum, SharedBuffer<u8> data, bool reliable);
|
||||||
void Send(u16 peer_id, u8 channelnum, SharedBuffer<u8> data, bool reliable);
|
void Send(u16 peer_id, u8 channelnum, SharedBuffer<u8> data, bool reliable);
|
||||||
void RunTimeouts(float dtime); // dummy
|
void RunTimeouts(float dtime); // dummy
|
||||||
|
@ -1777,14 +1777,13 @@ void Server::AsyncRunStep()
|
|||||||
void Server::Receive()
|
void Server::Receive()
|
||||||
{
|
{
|
||||||
DSTACK(__FUNCTION_NAME);
|
DSTACK(__FUNCTION_NAME);
|
||||||
u32 data_maxsize = 10000;
|
SharedBuffer<u8> data;
|
||||||
Buffer<u8> data(data_maxsize);
|
|
||||||
u16 peer_id;
|
u16 peer_id;
|
||||||
u32 datasize;
|
u32 datasize;
|
||||||
try{
|
try{
|
||||||
{
|
{
|
||||||
JMutexAutoLock conlock(m_con_mutex);
|
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
|
// This has to be called so that the client list gets synced
|
||||||
|
52
src/test.cpp
52
src/test.cpp
@ -852,9 +852,9 @@ struct TestConnection
|
|||||||
try
|
try
|
||||||
{
|
{
|
||||||
u16 peer_id;
|
u16 peer_id;
|
||||||
u8 data[100];
|
SharedBuffer<u8> data;
|
||||||
infostream<<"** running client.Receive()"<<std::endl;
|
infostream<<"** running client.Receive()"<<std::endl;
|
||||||
u32 size = client.Receive(peer_id, data, 100);
|
u32 size = client.Receive(peer_id, data);
|
||||||
infostream<<"** Client received: peer_id="<<peer_id
|
infostream<<"** Client received: peer_id="<<peer_id
|
||||||
<<", size="<<size
|
<<", size="<<size
|
||||||
<<std::endl;
|
<<std::endl;
|
||||||
@ -874,9 +874,9 @@ struct TestConnection
|
|||||||
try
|
try
|
||||||
{
|
{
|
||||||
u16 peer_id;
|
u16 peer_id;
|
||||||
u8 data[100];
|
SharedBuffer<u8> data;
|
||||||
infostream<<"** running server.Receive()"<<std::endl;
|
infostream<<"** running server.Receive()"<<std::endl;
|
||||||
u32 size = server.Receive(peer_id, data, 100);
|
u32 size = server.Receive(peer_id, data);
|
||||||
infostream<<"** Server received: peer_id="<<peer_id
|
infostream<<"** Server received: peer_id="<<peer_id
|
||||||
<<", size="<<size
|
<<", size="<<size
|
||||||
<<std::endl;
|
<<std::endl;
|
||||||
@ -901,9 +901,9 @@ struct TestConnection
|
|||||||
try
|
try
|
||||||
{
|
{
|
||||||
u16 peer_id;
|
u16 peer_id;
|
||||||
u8 data[100];
|
SharedBuffer<u8> data;
|
||||||
infostream<<"** running client.Receive()"<<std::endl;
|
infostream<<"** running client.Receive()"<<std::endl;
|
||||||
u32 size = client.Receive(peer_id, data, 100);
|
u32 size = client.Receive(peer_id, data);
|
||||||
infostream<<"** Client received: peer_id="<<peer_id
|
infostream<<"** Client received: peer_id="<<peer_id
|
||||||
<<", size="<<size
|
<<", size="<<size
|
||||||
<<std::endl;
|
<<std::endl;
|
||||||
@ -919,9 +919,9 @@ struct TestConnection
|
|||||||
try
|
try
|
||||||
{
|
{
|
||||||
u16 peer_id;
|
u16 peer_id;
|
||||||
u8 data[100];
|
SharedBuffer<u8> data;
|
||||||
infostream<<"** running server.Receive()"<<std::endl;
|
infostream<<"** running server.Receive()"<<std::endl;
|
||||||
u32 size = server.Receive(peer_id, data, 100);
|
u32 size = server.Receive(peer_id, data);
|
||||||
infostream<<"** Server received: peer_id="<<peer_id
|
infostream<<"** Server received: peer_id="<<peer_id
|
||||||
<<", size="<<size
|
<<", size="<<size
|
||||||
<<std::endl;
|
<<std::endl;
|
||||||
@ -944,14 +944,14 @@ struct TestConnection
|
|||||||
sleep_ms(50);
|
sleep_ms(50);
|
||||||
|
|
||||||
u16 peer_id;
|
u16 peer_id;
|
||||||
u8 recvdata[100];
|
SharedBuffer<u8> recvdata;
|
||||||
infostream<<"** running server.Receive()"<<std::endl;
|
infostream<<"** running server.Receive()"<<std::endl;
|
||||||
u32 size = server.Receive(peer_id, recvdata, 100);
|
u32 size = server.Receive(peer_id, recvdata);
|
||||||
infostream<<"** Server received: peer_id="<<peer_id
|
infostream<<"** Server received: peer_id="<<peer_id
|
||||||
<<", size="<<size
|
<<", size="<<size
|
||||||
<<", data="<<*data
|
<<", data="<<*data
|
||||||
<<std::endl;
|
<<std::endl;
|
||||||
assert(memcmp(*data, recvdata, data.getSize()) == 0);
|
assert(memcmp(*data, *recvdata, data.getSize()) == 0);
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
u16 peer_id_client = 2;
|
u16 peer_id_client = 2;
|
||||||
@ -987,29 +987,29 @@ struct TestConnection
|
|||||||
infostream<<"*** Receiving the packets"<<std::endl;
|
infostream<<"*** Receiving the packets"<<std::endl;
|
||||||
|
|
||||||
u16 peer_id;
|
u16 peer_id;
|
||||||
u8 recvdata[20];
|
SharedBuffer<u8> recvdata;
|
||||||
u32 size;
|
u32 size;
|
||||||
|
|
||||||
infostream<<"** running client.Receive()"<<std::endl;
|
infostream<<"** running client.Receive()"<<std::endl;
|
||||||
peer_id = 132;
|
peer_id = 132;
|
||||||
size = client.Receive(peer_id, recvdata, 20);
|
size = client.Receive(peer_id, recvdata);
|
||||||
infostream<<"** Client received: peer_id="<<peer_id
|
infostream<<"** Client received: peer_id="<<peer_id
|
||||||
<<", size="<<size
|
<<", size="<<size
|
||||||
<<", data="<<recvdata
|
<<", data="<<*recvdata
|
||||||
<<std::endl;
|
<<std::endl;
|
||||||
assert(size == data1.getSize());
|
assert(size == data1.getSize());
|
||||||
assert(memcmp(*data1, recvdata, data1.getSize()) == 0);
|
assert(memcmp(*data1, *recvdata, data1.getSize()) == 0);
|
||||||
assert(peer_id == PEER_ID_SERVER);
|
assert(peer_id == PEER_ID_SERVER);
|
||||||
|
|
||||||
infostream<<"** running client.Receive()"<<std::endl;
|
infostream<<"** running client.Receive()"<<std::endl;
|
||||||
peer_id = 132;
|
peer_id = 132;
|
||||||
size = client.Receive(peer_id, recvdata, 20);
|
size = client.Receive(peer_id, recvdata);
|
||||||
infostream<<"** Client received: peer_id="<<peer_id
|
infostream<<"** Client received: peer_id="<<peer_id
|
||||||
<<", size="<<size
|
<<", size="<<size
|
||||||
<<", data="<<recvdata
|
<<", data="<<*recvdata
|
||||||
<<std::endl;
|
<<std::endl;
|
||||||
assert(size == data2.getSize());
|
assert(size == data2.getSize());
|
||||||
assert(memcmp(*data2, recvdata, data2.getSize()) == 0);
|
assert(memcmp(*data2, *recvdata, data2.getSize()) == 0);
|
||||||
assert(peer_id == PEER_ID_SERVER);
|
assert(peer_id == PEER_ID_SERVER);
|
||||||
|
|
||||||
bool got_exception = false;
|
bool got_exception = false;
|
||||||
@ -1017,10 +1017,10 @@ struct TestConnection
|
|||||||
{
|
{
|
||||||
infostream<<"** running client.Receive()"<<std::endl;
|
infostream<<"** running client.Receive()"<<std::endl;
|
||||||
peer_id = 132;
|
peer_id = 132;
|
||||||
size = client.Receive(peer_id, recvdata, 20);
|
size = client.Receive(peer_id, recvdata);
|
||||||
infostream<<"** Client received: peer_id="<<peer_id
|
infostream<<"** Client received: peer_id="<<peer_id
|
||||||
<<", size="<<size
|
<<", size="<<size
|
||||||
<<", data="<<recvdata
|
<<", data="<<*recvdata
|
||||||
<<std::endl;
|
<<std::endl;
|
||||||
}
|
}
|
||||||
catch(con::NoIncomingDataException &e)
|
catch(con::NoIncomingDataException &e)
|
||||||
@ -1056,12 +1056,12 @@ struct TestConnection
|
|||||||
//int receivetimes = myrand_range(1,20);
|
//int receivetimes = myrand_range(1,20);
|
||||||
int receivetimes = 20;
|
int receivetimes = 20;
|
||||||
for(int i=0; i<receivetimes; i++){
|
for(int i=0; i<receivetimes; i++){
|
||||||
u8 recvdata[100000];
|
SharedBuffer<u8> recvdata;
|
||||||
u16 peer_id = 132;
|
u16 peer_id = 132;
|
||||||
u16 size = 0;
|
u16 size = 0;
|
||||||
bool received = false;
|
bool received = false;
|
||||||
try{
|
try{
|
||||||
size = client.Receive(peer_id, recvdata, 100000);
|
size = client.Receive(peer_id, recvdata);
|
||||||
received = true;
|
received = true;
|
||||||
}catch(con::NoIncomingDataException &e){
|
}catch(con::NoIncomingDataException &e){
|
||||||
}
|
}
|
||||||
@ -1092,7 +1092,7 @@ struct TestConnection
|
|||||||
|
|
||||||
sleep_ms(3000);
|
sleep_ms(3000);
|
||||||
|
|
||||||
u8 recvdata[datasize + 1000];
|
SharedBuffer<u8> recvdata;
|
||||||
infostream<<"** running client.Receive()"<<std::endl;
|
infostream<<"** running client.Receive()"<<std::endl;
|
||||||
u16 peer_id = 132;
|
u16 peer_id = 132;
|
||||||
u16 size = 0;
|
u16 size = 0;
|
||||||
@ -1102,7 +1102,7 @@ struct TestConnection
|
|||||||
if(porting::getTimeMs() - timems0 > 5000)
|
if(porting::getTimeMs() - timems0 > 5000)
|
||||||
break;
|
break;
|
||||||
try{
|
try{
|
||||||
size = client.Receive(peer_id, recvdata, datasize + 1000);
|
size = client.Receive(peer_id, recvdata);
|
||||||
received = true;
|
received = true;
|
||||||
}catch(con::NoIncomingDataException &e){
|
}catch(con::NoIncomingDataException &e){
|
||||||
}
|
}
|
||||||
@ -1116,13 +1116,13 @@ struct TestConnection
|
|||||||
infostream<<"Received data (size="<<size<<"):";
|
infostream<<"Received data (size="<<size<<"):";
|
||||||
for(int i=0; i<size && i<20; i++){
|
for(int i=0; i<size && i<20; i++){
|
||||||
if(i%2==0) DEBUGPRINT(" ");
|
if(i%2==0) DEBUGPRINT(" ");
|
||||||
DEBUGPRINT("%.2X", ((int)((const char*)recvdata)[i])&0xff);
|
DEBUGPRINT("%.2X", ((int)(recvdata[i]))&0xff);
|
||||||
}
|
}
|
||||||
if(size>20)
|
if(size>20)
|
||||||
infostream<<"...";
|
infostream<<"...";
|
||||||
infostream<<std::endl;
|
infostream<<std::endl;
|
||||||
|
|
||||||
assert(memcmp(*data1, recvdata, data1.getSize()) == 0);
|
assert(memcmp(*data1, *recvdata, data1.getSize()) == 0);
|
||||||
assert(peer_id == PEER_ID_SERVER);
|
assert(peer_id == PEER_ID_SERVER);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user