#include "debug.h"
#include "map.h"
#include "player.h"
-#include "main.h"
#include "socket.h"
-#include "connection.h"
+#include "network/connection.h"
#include "serialization.h"
#include "voxel.h"
#include "collision.h"
#include "util/serialize.h"
#include "noise.h" // PseudoRandom used for random data for compression
#include "network/networkprotocol.h" // LATEST_PROTOCOL_VERSION
+#include "profiler.h"
#include <algorithm>
/*
struct TestUtilities: public TestBase
{
+ inline float ref_WrapDegrees180(float f)
+ {
+ // This is a slower alternative to the wrapDegrees_180() function;
+ // used as a reference for testing
+ float value = fmodf(f + 180, 360);
+ if (value < 0)
+ value += 360;
+ return value - 180;
+ }
+
+ inline float ref_WrapDegrees_0_360(float f)
+ {
+ // This is a slower alternative to the wrapDegrees_0_360() function;
+ // used as a reference for testing
+ float value = fmodf(f, 360);
+ if (value < 0)
+ value += 360;
+ return value < 0 ? value + 360 : value;
+ }
+
+
void Run()
{
- /*infostream<<"wrapDegrees(100.0) = "<<wrapDegrees(100.0)<<std::endl;
- infostream<<"wrapDegrees(720.5) = "<<wrapDegrees(720.5)<<std::endl;
- infostream<<"wrapDegrees(-0.5) = "<<wrapDegrees(-0.5)<<std::endl;*/
- UASSERT(fabs(wrapDegrees(100.0) - 100.0) < 0.001);
- UASSERT(fabs(wrapDegrees(720.5) - 0.5) < 0.001);
- UASSERT(fabs(wrapDegrees(-0.5) - (-0.5)) < 0.001);
- UASSERT(fabs(wrapDegrees(-365.5) - (-5.5)) < 0.001);
+ UASSERT(fabs(modulo360f(100.0) - 100.0) < 0.001);
+ UASSERT(fabs(modulo360f(720.5) - 0.5) < 0.001);
+ UASSERT(fabs(modulo360f(-0.5) - (-0.5)) < 0.001);
+ UASSERT(fabs(modulo360f(-365.5) - (-5.5)) < 0.001);
+
+ for (float f = -720; f <= -360; f += 0.25) {
+ UASSERT(fabs(modulo360f(f) - modulo360f(f + 360)) < 0.001);
+ }
+
+ for (float f = -1440; f <= 1440; f += 0.25) {
+ UASSERT(fabs(modulo360f(f) - fmodf(f, 360)) < 0.001);
+ UASSERT(fabs(wrapDegrees_180(f) - ref_WrapDegrees180(f)) < 0.001);
+ UASSERT(fabs(wrapDegrees_0_360(f) - ref_WrapDegrees_0_360(f)) < 0.001);
+ UASSERT(wrapDegrees_0_360(fabs(wrapDegrees_180(f) - wrapDegrees_0_360(f))) < 0.001);
+ }
+
UASSERT(lowercase("Foo bAR") == "foo bar");
UASSERT(trim("\n \t\r Foo bAR \r\n\t\t ") == "Foo bAR");
UASSERT(trim("\n \t\r \r\n\t\t ") == "");
Handler hand_server("server");
Handler hand_client("client");
+ Address address(0, 0, 0, 0, 30001);
+ Address bind_addr(0, 0, 0, 0, 30001);
+ /*
+ * Try to use the bind_address for servers with no localhost address
+ * For example: FreeBSD jails
+ */
+ std::string bind_str = g_settings->get("bind_address");
+ try {
+ bind_addr.Resolve(bind_str.c_str());
+
+ if (!bind_addr.isIPv6()) {
+ address = bind_addr;
+ }
+ } catch (ResolveError &e) {
+ }
+
infostream<<"** Creating server Connection"<<std::endl;
con::Connection server(proto_id, 512, 5.0, false, &hand_server);
- Address address(0,0,0,0, 30001);
server.Serve(address);
infostream<<"** Creating client Connection"<<std::endl;
sleep_ms(50);
- Address server_address(127,0,0,1, 30001);
+ Address server_address(127, 0, 0, 1, 30001);
+ if (address != Address(0, 0, 0, 0, 30001)) {
+ server_address = bind_addr;
+ }
+
infostream<<"** running client.Connect()"<<std::endl;
client.Connect(server_address);
try
{
- u16 peer_id;
- SharedBuffer<u8> data;
- infostream<<"** running client.Receive()"<<std::endl;
- u32 size = client.Receive(peer_id, data);
- infostream<<"** Client received: peer_id="<<peer_id
- <<", size="<<size
- <<std::endl;
+ NetworkPacket pkt;
+ infostream << "** running client.Receive()" << std::endl;
+ client.Receive(&pkt);
+ infostream << "** Client received: peer_id=" << pkt.getPeerId()
+ << ", size=" << pkt.getSize()
+ << std::endl;
}
catch(con::NoIncomingDataException &e)
{
try
{
- u16 peer_id;
- SharedBuffer<u8> data;
- infostream<<"** running server.Receive()"<<std::endl;
- u32 size = server.Receive(peer_id, data);
- infostream<<"** Server received: peer_id="<<peer_id
- <<", size="<<size
- <<std::endl;
+ NetworkPacket pkt;
+ infostream << "** running server.Receive()" << std::endl;
+ server.Receive(&pkt);
+ infostream<<"** Server received: peer_id=" << pkt.getPeerId()
+ << ", size=" << pkt.getSize()
+ << std::endl;
}
catch(con::NoIncomingDataException &e)
{
{
try
{
- u16 peer_id;
- SharedBuffer<u8> data;
- infostream<<"** running client.Receive()"<<std::endl;
- u32 size = client.Receive(peer_id, data);
- infostream<<"** Client received: peer_id="<<peer_id
- <<", size="<<size
- <<std::endl;
+ NetworkPacket pkt;
+ infostream << "** running client.Receive()" << std::endl;
+ client.Receive(&pkt);
+ infostream << "** Client received: peer_id=" << pkt.getPeerId()
+ << ", size=" << pkt.getSize()
+ << std::endl;
}
catch(con::NoIncomingDataException &e)
{
try
{
- u16 peer_id;
- SharedBuffer<u8> data;
- infostream<<"** running server.Receive()"<<std::endl;
- u32 size = server.Receive(peer_id, data);
- infostream<<"** Server received: peer_id="<<peer_id
- <<", size="<<size
- <<std::endl;
+ NetworkPacket pkt;
+ infostream << "** running server.Receive()" << std::endl;
+ server.Receive(&pkt);
+ infostream << "** Server received: peer_id=" << pkt.getPeerId()
+ << ", size=" << pkt.getSize()
+ << std::endl;
}
catch(con::NoIncomingDataException &e)
{
}
-#if 1
- /*
- Simple send-receive test
- */
- {
- /*u8 data[] = "Hello World!";
- u32 datasize = sizeof(data);*/
- SharedBuffer<u8> data = SharedBufferFromString("Hello World!");
- infostream<<"** running client.Send()"<<std::endl;
- client.Send(PEER_ID_SERVER, 0, data, true);
-
- sleep_ms(50);
-
- u16 peer_id;
- SharedBuffer<u8> recvdata;
- infostream<<"** running server.Receive()"<<std::endl;
- u32 size = server.Receive(peer_id, recvdata);
- infostream<<"** Server received: peer_id="<<peer_id
- <<", size="<<size
- <<", data="<<*data
- <<std::endl;
- UASSERT(memcmp(*data, *recvdata, data.getSize()) == 0);
- }
-#endif
- u16 peer_id_client = 2;
-#if 0
/*
- Send consequent packets in different order
- Not compatible with new Connection, thus commented out.
+ Simple send-receive test
*/
{
- //u8 data1[] = "hello1";
- //u8 data2[] = "hello2";
- SharedBuffer<u8> data1 = SharedBufferFromString("hello1");
- SharedBuffer<u8> data2 = SharedBufferFromString("Hello2");
+ NetworkPacket pkt;
+ pkt.putRawPacket((u8*) "Hello World !", 14, 0);
- Address client_address =
- server.GetPeerAddress(peer_id_client);
+ Buffer<u8> sentdata = pkt.oldForgePacket();
- infostream<<"*** Sending packets in wrong order (2,1,2)"
- <<std::endl;
-
- u8 chn = 0;
- con::Channel *ch = &server.getPeer(peer_id_client)->channels[chn];
- u16 sn = ch->next_outgoing_seqnum;
- ch->next_outgoing_seqnum = sn+1;
- server.Send(peer_id_client, chn, data2, true);
- ch->next_outgoing_seqnum = sn;
- server.Send(peer_id_client, chn, data1, true);
- ch->next_outgoing_seqnum = sn+1;
- server.Send(peer_id_client, chn, data2, true);
+ infostream<<"** running client.Send()"<<std::endl;
+ client.Send(PEER_ID_SERVER, 0, &pkt, true);
sleep_ms(50);
- infostream<<"*** Receiving the packets"<<std::endl;
+ NetworkPacket recvpacket;
+ infostream << "** running server.Receive()" << std::endl;
+ server.Receive(&recvpacket);
+ infostream << "** Server received: peer_id=" << pkt.getPeerId()
+ << ", size=" << pkt.getSize()
+ << ", data=" << (const char*)pkt.getU8Ptr(0)
+ << std::endl;
- u16 peer_id;
- SharedBuffer<u8> recvdata;
- u32 size;
-
- infostream<<"** running client.Receive()"<<std::endl;
- peer_id = 132;
- size = client.Receive(peer_id, recvdata);
- infostream<<"** Client received: peer_id="<<peer_id
- <<", size="<<size
- <<", data="<<*recvdata
- <<std::endl;
- UASSERT(size == data1.getSize());
- UASSERT(memcmp(*data1, *recvdata, data1.getSize()) == 0);
- UASSERT(peer_id == PEER_ID_SERVER);
+ Buffer<u8> recvdata = pkt.oldForgePacket();
- infostream<<"** running client.Receive()"<<std::endl;
- peer_id = 132;
- size = client.Receive(peer_id, recvdata);
- infostream<<"** Client received: peer_id="<<peer_id
- <<", size="<<size
- <<", data="<<*recvdata
- <<std::endl;
- UASSERT(size == data2.getSize());
- UASSERT(memcmp(*data2, *recvdata, data2.getSize()) == 0);
- UASSERT(peer_id == PEER_ID_SERVER);
-
- bool got_exception = false;
- try
- {
- infostream<<"** running client.Receive()"<<std::endl;
- peer_id = 132;
- size = client.Receive(peer_id, recvdata);
- infostream<<"** Client received: peer_id="<<peer_id
- <<", size="<<size
- <<", data="<<*recvdata
- <<std::endl;
- }
- catch(con::NoIncomingDataException &e)
- {
- infostream<<"** No incoming data for client"<<std::endl;
- got_exception = true;
- }
- UASSERT(got_exception);
- }
-#endif
-#if 0
- /*
- Send large amounts of packets (infinite test)
- Commented out because of infinity.
- */
- {
- infostream<<"Sending large amounts of packets (infinite test)"<<std::endl;
- int sendcount = 0;
- for(;;){
- int datasize = myrand_range(0,5)==0?myrand_range(100,10000):myrand_range(0,100);
- infostream<<"datasize="<<datasize<<std::endl;
- SharedBuffer<u8> data1(datasize);
- for(u16 i=0; i<datasize; i++)
- data1[i] = i/4;
-
- int sendtimes = myrand_range(1,10);
- for(int i=0; i<sendtimes; i++){
- server.Send(peer_id_client, 0, data1, true);
- sendcount++;
- }
- infostream<<"sendcount="<<sendcount<<std::endl;
-
- //int receivetimes = myrand_range(1,20);
- int receivetimes = 20;
- for(int i=0; i<receivetimes; i++){
- SharedBuffer<u8> recvdata;
- u16 peer_id = 132;
- u16 size = 0;
- bool received = false;
- try{
- size = client.Receive(peer_id, recvdata);
- received = true;
- }catch(con::NoIncomingDataException &e){
- }
- }
- }
+ UASSERT(memcmp(*sentdata, *recvdata, recvdata.getSize()) == 0);
}
-#endif
+
+ u16 peer_id_client = 2;
/*
Send a large packet
*/
{
const int datasize = 30000;
- SharedBuffer<u8> data1(datasize);
- for(u16 i=0; i<datasize; i++){
- data1[i] = i/4;
+ NetworkPacket pkt(0, datasize);
+ for (u16 i=0; i<datasize; i++) {
+ pkt << (u8) i/4;
}
infostream<<"Sending data (size="<<datasize<<"):";
- for(int i=0; i<datasize && i<20; i++){
+ for(int i=0; i<datasize && i<20; i++) {
if(i%2==0) infostream<<" ";
char buf[10];
- snprintf(buf, 10, "%.2X", ((int)((const char*)*data1)[i])&0xff);
+ snprintf(buf, 10, "%.2X", ((int)((const char*)pkt.getU8Ptr(0))[i])&0xff);
infostream<<buf;
}
- if(datasize>20)
- infostream<<"...";
- infostream<<std::endl;
+ if(datasize > 20)
+ infostream << "...";
+ infostream << std::endl;
+
+ Buffer<u8> sentdata = pkt.oldForgePacket();
- server.Send(peer_id_client, 0, data1, true);
+ server.Send(peer_id_client, 0, &pkt, true);
//sleep_ms(3000);
- SharedBuffer<u8> recvdata;
- infostream<<"** running client.Receive()"<<std::endl;
+ Buffer<u8> recvdata;
+ infostream << "** running client.Receive()" << std::endl;
u16 peer_id = 132;
u16 size = 0;
bool received = false;
u32 timems0 = porting::getTimeMs();
- for(;;){
+ for(;;) {
if(porting::getTimeMs() - timems0 > 5000 || received)
break;
- try{
- size = client.Receive(peer_id, recvdata);
+ try {
+ NetworkPacket pkt;
+ client.Receive(&pkt);
+ size = pkt.getSize();
+ peer_id = pkt.getPeerId();
+ recvdata = pkt.oldForgePacket();
received = true;
- }catch(con::NoIncomingDataException &e){
+ } catch(con::NoIncomingDataException &e) {
}
sleep_ms(10);
}
infostream<<"...";
infostream<<std::endl;
- UASSERT(memcmp(*data1, *recvdata, data1.getSize()) == 0);
+ UASSERT(memcmp(*sentdata, *recvdata, recvdata.getSize()) == 0);
UASSERT(peer_id == PEER_ID_SERVER);
}
UASSERT(hand_client.last_id == 1);
UASSERT(hand_server.count == 1);
UASSERT(hand_server.last_id == 2);
+ }
+};
+
+struct TestProfiler : public TestBase
+{
+ void Run()
+ {
+ Profiler p;
+
+ p.avg("Test1", 1.f);
+ UASSERT(p.getValue("Test1") == 1.f);
+
+ p.avg("Test1", 2.f);
+ UASSERT(p.getValue("Test1") == 1.5f);
+
+ p.avg("Test1", 3.f);
+ UASSERT(p.getValue("Test1") == 2.f);
+
+ p.avg("Test1", 486.f);
+ UASSERT(p.getValue("Test1") == 123.f);
+
+ p.avg("Test1", 8);
+ UASSERT(p.getValue("Test1") == 100.f);
+
+ p.avg("Test1", 700);
+ UASSERT(p.getValue("Test1") == 200.f);
+
+ p.avg("Test1", 10000);
+ UASSERT(p.getValue("Test1") == 1600.f);
+
+ p.avg("Test2", 123.56);
+ p.avg("Test2", 123.58);
- //assert(0);
+ UASSERT(p.getValue("Test2") == 123.57f);
}
};
TEST(TestCompress);
TEST(TestSerialization);
TEST(TestNodedefSerialization);
+ TEST(TestProfiler);
TESTPARAMS(TestMapNode, ndef);
TESTPARAMS(TestVoxelManipulator, ndef);
TESTPARAMS(TestVoxelAlgorithms, ndef);
TEST(TestCollision);
if(INTERNET_SIMULATOR == false){
TEST(TestSocket);
- dout_con<<"=== BEGIN RUNNING UNIT TESTS FOR CONNECTION ==="<<std::endl;
+ dout_con << "=== BEGIN RUNNING UNIT TESTS FOR CONNECTION ===" << std::endl;
TEST(TestConnection);
- dout_con<<"=== END RUNNING UNIT TESTS FOR CONNECTION ==="<<std::endl;
+ dout_con << "=== END RUNNING UNIT TESTS FOR CONNECTION ===" << std::endl;
}
log_set_lev_silence(LMT_ERROR, false);
delete idef;
delete ndef;
- if(tests_failed == 0){
- infostream<<"run_tests(): "<<tests_failed<<" / "<<tests_run<<" tests failed."<<std::endl;
- infostream<<"run_tests() passed."<<std::endl;
+ if(tests_failed == 0) {
+ actionstream << "run_tests(): " << tests_failed << " / " << tests_run << " tests failed." << std::endl;
+ actionstream << "run_tests() passed." << std::endl;
return;
} else {
- errorstream<<"run_tests(): "<<tests_failed<<" / "<<tests_run<<" tests failed."<<std::endl;
- errorstream<<"run_tests() aborting."<<std::endl;
+ errorstream << "run_tests(): " << tests_failed << " / " << tests_run << " tests failed." << std::endl;
+ errorstream << "run_tests() aborting." << std::endl;
abort();
}
}