3 Copyright (C) 2010 celeron55, Perttu Ahola <celeron55@gmail.com>
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License along
16 with this program; if not, write to the Free Software Foundation, Inc.,
17 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21 #include "common_irrlicht.h"
26 #include "heightmap.h"
28 #include "connection.h"
30 #include "serialization.h"
36 #define sleep_ms(x) Sleep(x)
39 #define sleep_ms(x) usleep(x*1000)
43 Asserts that the exception occurs
45 #define EXCEPTION_CHECK(EType, code)\
47 bool exception_thrown = false;\
49 catch(EType &e) { exception_thrown = true; }\
50 assert(exception_thrown);\
57 /*dstream<<"wrapDegrees(100.0) = "<<wrapDegrees(100.0)<<std::endl;
58 dstream<<"wrapDegrees(720.5) = "<<wrapDegrees(720.5)<<std::endl;
59 dstream<<"wrapDegrees(-0.5) = "<<wrapDegrees(-0.5)<<std::endl;*/
60 assert(fabs(wrapDegrees(100.0) - 100.0) < 0.001);
61 assert(fabs(wrapDegrees(720.5) - 0.5) < 0.001);
62 assert(fabs(wrapDegrees(-0.5) - (-0.5)) < 0.001);
63 assert(fabs(wrapDegrees(-365.5) - (-5.5)) < 0.001);
64 assert(lowercase("Foo bAR") == "foo bar");
65 assert(is_yes("YeS") == true);
66 assert(is_yes("") == false);
67 assert(is_yes("FAlse") == false);
75 SharedBuffer<u8> fromdata(4);
81 std::ostringstream os(std::ios_base::binary);
82 compress(fromdata, os, 0);
84 std::string str_out = os.str();
86 dstream<<"str_out.size()="<<str_out.size()<<std::endl;
87 dstream<<"TestCompress: 1,5,5,1 -> ";
88 for(u32 i=0; i<str_out.size(); i++)
90 dstream<<(u32)str_out[i]<<",";
94 assert(str_out.size() == 10);
96 assert(str_out[0] == 0);
97 assert(str_out[1] == 0);
98 assert(str_out[2] == 0);
99 assert(str_out[3] == 4);
100 assert(str_out[4] == 0);
101 assert(str_out[5] == 1);
102 assert(str_out[6] == 1);
103 assert(str_out[7] == 5);
104 assert(str_out[8] == 0);
105 assert(str_out[9] == 1);
107 std::istringstream is(str_out, std::ios_base::binary);
108 std::ostringstream os2(std::ios_base::binary);
110 decompress(is, os2, 0);
111 std::string str_out2 = os2.str();
113 dstream<<"decompress: ";
114 for(u32 i=0; i<str_out2.size(); i++)
116 dstream<<(u32)str_out2[i]<<",";
120 assert(str_out2.size() == fromdata.getSize());
122 for(u32 i=0; i<str_out2.size(); i++)
124 assert(str_out2[i] == fromdata[i]);
136 assert(n.d == CONTENT_AIR);
137 assert(n.getLight() == 0);
141 assert(n.light_propagates() == true);
143 assert(n.light_propagates() == false);
147 struct TestVoxelManipulator
155 VoxelArea a(v3s16(-1,-1,-1), v3s16(1,1,1));
156 assert(a.index(0,0,0) == 1*3*3 + 1*3 + 1);
157 assert(a.index(-1,-1,-1) == 0);
159 VoxelArea c(v3s16(-2,-2,-2), v3s16(2,2,2));
160 // An area that is 1 bigger in x+ and z-
161 VoxelArea d(v3s16(-2,-2,-3), v3s16(3,2,2));
163 core::list<VoxelArea> aa;
167 core::array<VoxelArea> results;
168 results.push_back(VoxelArea(v3s16(-2,-2,-3),v3s16(3,2,-3)));
169 results.push_back(VoxelArea(v3s16(3,-2,-2),v3s16(3,2,2)));
171 assert(aa.size() == results.size());
173 dstream<<"Result of diff:"<<std::endl;
174 for(core::list<VoxelArea>::Iterator
175 i = aa.begin(); i != aa.end(); i++)
180 s32 j = results.linear_search(*i);
194 dstream<<"*** Setting (-1,0,-1)=2 ***"<<std::endl;
196 v.setNodeNoRef(v3s16(-1,0,-1), MapNode(2));
200 assert(v.getNode(v3s16(-1,0,-1)).d == 2);
202 dstream<<"*** Reading from inexistent (0,0,-1) ***"<<std::endl;
204 EXCEPTION_CHECK(InvalidPositionException, v.getNode(v3s16(0,0,-1)));
208 dstream<<"*** Adding area ***"<<std::endl;
214 assert(v.getNode(v3s16(-1,0,-1)).d == 2);
215 EXCEPTION_CHECK(InvalidPositionException, v.getNode(v3s16(0,1,1)));
223 const char *content =
235 v3s16 size(12, 4, 2);
236 VoxelArea area(v3s16(0,0,0), size-v3s16(1,1,1));
238 const char *p = content;
239 for(s16 z=0; z<size.Z; z++)
240 for(s16 y=size.Y-1; y>=0; y--)
241 for(s16 x=0; x<size.X; x++)
244 //n.pressure = size.Y - y;
253 v.setNode(v3s16(x,y,z), n);
257 v.print(dstream, VOXELPRINT_WATERPRESSURE);
259 core::map<v3s16, u8> active_nodes;
260 v.updateAreaWaterPressure(area, active_nodes);
262 v.print(dstream, VOXELPRINT_WATERPRESSURE);
264 s16 highest_y = -32768;
266 NOTE: These are commented out because this behaviour is changed
269 //assert(v.getWaterPressure(v3s16(7, 1, 1), highest_y, 0) == -1);
270 //assert(highest_y == 3);
271 /*assert(v.getWaterPressure(v3s16(7, 1, 1), highest_y, 0) == 3);
272 //assert(highest_y == 3);*/
274 active_nodes.clear();
275 active_nodes[v3s16(9,1,0)] = 1;
276 //v.flowWater(active_nodes, 0, false);
277 v.flowWater(active_nodes, 0, true, 1000);
279 dstream<<"Final result of flowWater:"<<std::endl;
280 v.print(dstream, VOXELPRINT_WATERPRESSURE);
288 class TC : public NodeContainer
294 core::list<v3s16> validity_exceptions;
298 position_valid = true;
301 virtual bool isValidPosition(v3s16 p)
303 //return position_valid ^ (p==position_valid_exception);
304 bool exception = false;
305 for(core::list<v3s16>::Iterator i=validity_exceptions.begin();
306 i != validity_exceptions.end(); i++)
314 return exception ? !position_valid : position_valid;
317 virtual MapNode getNode(v3s16 p)
319 if(isValidPosition(p) == false)
320 throw InvalidPositionException();
324 virtual void setNode(v3s16 p, MapNode & n)
326 if(isValidPosition(p) == false)
327 throw InvalidPositionException();
330 virtual u16 nodeContainerId() const
340 MapBlock b(&parent, v3s16(1,1,1));
341 v3s16 relpos(MAP_BLOCKSIZE, MAP_BLOCKSIZE, MAP_BLOCKSIZE);
343 assert(b.getPosRelative() == relpos);
345 assert(b.getBox().MinEdge.X == MAP_BLOCKSIZE);
346 assert(b.getBox().MaxEdge.X == MAP_BLOCKSIZE*2-1);
347 assert(b.getBox().MinEdge.Y == MAP_BLOCKSIZE);
348 assert(b.getBox().MaxEdge.Y == MAP_BLOCKSIZE*2-1);
349 assert(b.getBox().MinEdge.Z == MAP_BLOCKSIZE);
350 assert(b.getBox().MaxEdge.Z == MAP_BLOCKSIZE*2-1);
352 assert(b.isValidPosition(v3s16(0,0,0)) == true);
353 assert(b.isValidPosition(v3s16(-1,0,0)) == false);
354 assert(b.isValidPosition(v3s16(-1,-142,-2341)) == false);
355 assert(b.isValidPosition(v3s16(-124,142,2341)) == false);
356 assert(b.isValidPosition(v3s16(MAP_BLOCKSIZE-1,MAP_BLOCKSIZE-1,MAP_BLOCKSIZE-1)) == true);
357 assert(b.isValidPosition(v3s16(MAP_BLOCKSIZE-1,MAP_BLOCKSIZE,MAP_BLOCKSIZE-1)) == false);
360 TODO: this method should probably be removed
361 if the block size isn't going to be set variable
363 /*assert(b.getSizeNodes() == v3s16(MAP_BLOCKSIZE,
364 MAP_BLOCKSIZE, MAP_BLOCKSIZE));*/
366 // Changed flag should be initially set
367 assert(b.getChangedFlag() == true);
368 b.resetChangedFlag();
369 assert(b.getChangedFlag() == false);
371 // All nodes should have been set to
372 // .d=CONTENT_AIR and .getLight() = 0
373 for(u16 z=0; z<MAP_BLOCKSIZE; z++)
374 for(u16 y=0; y<MAP_BLOCKSIZE; y++)
375 for(u16 x=0; x<MAP_BLOCKSIZE; x++){
376 assert(b.getNode(v3s16(x,y,z)).d == CONTENT_AIR);
377 assert(b.getNode(v3s16(x,y,z)).getLight() == 0);
381 Parent fetch functions
383 parent.position_valid = false;
388 // Positions in the block should still be valid
389 assert(b.isValidPositionParent(v3s16(0,0,0)) == true);
390 assert(b.isValidPositionParent(v3s16(MAP_BLOCKSIZE-1,MAP_BLOCKSIZE-1,MAP_BLOCKSIZE-1)) == true);
391 n = b.getNodeParent(v3s16(0,MAP_BLOCKSIZE-1,0));
392 assert(n.d == CONTENT_AIR);
394 // ...but outside the block they should be invalid
395 assert(b.isValidPositionParent(v3s16(-121,2341,0)) == false);
396 assert(b.isValidPositionParent(v3s16(-1,0,0)) == false);
397 assert(b.isValidPositionParent(v3s16(MAP_BLOCKSIZE-1,MAP_BLOCKSIZE-1,MAP_BLOCKSIZE)) == false);
400 bool exception_thrown = false;
402 // This should throw an exception
403 MapNode n = b.getNodeParent(v3s16(0,0,-1));
405 catch(InvalidPositionException &e)
407 exception_thrown = true;
409 assert(exception_thrown);
412 parent.position_valid = true;
413 // Now the positions outside should be valid
414 assert(b.isValidPositionParent(v3s16(-121,2341,0)) == true);
415 assert(b.isValidPositionParent(v3s16(-1,0,0)) == true);
416 assert(b.isValidPositionParent(v3s16(MAP_BLOCKSIZE-1,MAP_BLOCKSIZE-1,MAP_BLOCKSIZE)) == true);
417 n = b.getNodeParent(v3s16(0,0,MAP_BLOCKSIZE));
426 assert(b.getNode(p).d == 4);
427 assert(b.getNodeTile(p) == 4);
428 assert(b.getNodeTile(v3s16(-1,-1,0)) == 5);
433 // Set lighting of all nodes to 0
434 for(u16 z=0; z<MAP_BLOCKSIZE; z++){
435 for(u16 y=0; y<MAP_BLOCKSIZE; y++){
436 for(u16 x=0; x<MAP_BLOCKSIZE; x++){
437 MapNode n = b.getNode(v3s16(x,y,z));
439 b.setNode(v3s16(x,y,z), n);
445 Check how the block handles being a lonely sky block
447 parent.position_valid = true;
448 b.setIsUnderground(false);
449 parent.node.d = CONTENT_AIR;
450 parent.node.setLight(LIGHT_SUN);
451 core::map<v3s16, bool> light_sources;
452 // The bottom block is invalid, because we have a shadowing node
453 assert(b.propagateSunlight(light_sources) == false);
454 assert(b.getNode(v3s16(1,4,0)).getLight() == LIGHT_SUN);
455 assert(b.getNode(v3s16(1,3,0)).getLight() == LIGHT_SUN);
456 assert(b.getNode(v3s16(1,2,0)).getLight() == 0);
457 assert(b.getNode(v3s16(1,1,0)).getLight() == 0);
458 assert(b.getNode(v3s16(1,0,0)).getLight() == 0);
459 assert(b.getNode(v3s16(1,2,3)).getLight() == LIGHT_SUN);
460 assert(b.getFaceLight(p, v3s16(0,1,0)) == LIGHT_SUN);
461 assert(b.getFaceLight(p, v3s16(0,-1,0)) == 0);
462 // According to MapBlock::getFaceLight,
463 // The face on the z+ side should have double-diminished light
464 assert(b.getFaceLight(p, v3s16(0,0,1)) == diminish_light(diminish_light(LIGHT_MAX)));
467 Check how the block handles being in between blocks with some non-sunlight
468 while being underground
471 // Make neighbours to exist and set some non-sunlight to them
472 parent.position_valid = true;
473 b.setIsUnderground(true);
474 parent.node.setLight(LIGHT_MAX/2);
475 core::map<v3s16, bool> light_sources;
476 // The block below should be valid because there shouldn't be
477 // sunlight in there either
478 assert(b.propagateSunlight(light_sources) == true);
479 // Should not touch nodes that are not affected (that is, all of them)
480 //assert(b.getNode(v3s16(1,2,3)).getLight() == LIGHT_SUN);
481 // Should set light of non-sunlighted blocks to 0.
482 assert(b.getNode(v3s16(1,2,3)).getLight() == 0);
485 Set up a situation where:
486 - There is only air in this block
487 - There is a valid non-sunlighted block at the bottom, and
488 - Invalid blocks elsewhere.
489 - the block is not underground.
491 This should result in bottom block invalidity
494 b.setIsUnderground(false);
496 for(u16 z=0; z<MAP_BLOCKSIZE; z++){
497 for(u16 y=0; y<MAP_BLOCKSIZE; y++){
498 for(u16 x=0; x<MAP_BLOCKSIZE; x++){
502 b.setNode(v3s16(x,y,z), n);
506 // Make neighbours invalid
507 parent.position_valid = false;
508 // Add exceptions to the top of the bottom block
509 for(u16 x=0; x<MAP_BLOCKSIZE; x++)
510 for(u16 z=0; z<MAP_BLOCKSIZE; z++)
512 parent.validity_exceptions.push_back(v3s16(MAP_BLOCKSIZE+x, MAP_BLOCKSIZE-1, MAP_BLOCKSIZE+z));
514 // Lighting value for the valid nodes
515 parent.node.setLight(LIGHT_MAX/2);
516 core::map<v3s16, bool> light_sources;
517 // Bottom block is not valid
518 assert(b.propagateSunlight(light_sources) == false);
525 class TC : public NodeContainer
534 position_valid = true;
537 virtual bool isValidPosition(v3s16 p)
539 return position_valid;
542 virtual MapNode getNode(v3s16 p)
544 if(position_valid == false)
545 throw InvalidPositionException();
549 virtual void setNode(v3s16 p, MapNode & n)
551 if(position_valid == false)
552 throw InvalidPositionException();
555 virtual u16 nodeContainerId() const
564 parent.position_valid = false;
566 // Create one with no heightmaps
567 ServerMapSector sector(&parent, v2s16(1,1), 0);
568 //ConstantGenerator *dummyheightmap = new ConstantGenerator();
569 //sector->setHeightmap(dummyheightmap);
571 EXCEPTION_CHECK(InvalidPositionException, sector.getBlockNoCreate(0));
572 EXCEPTION_CHECK(InvalidPositionException, sector.getBlockNoCreate(1));
574 MapBlock * bref = sector.createBlankBlock(-2);
576 EXCEPTION_CHECK(InvalidPositionException, sector.getBlockNoCreate(0));
577 assert(sector.getBlockNoCreate(-2) == bref);
579 //TODO: Check for AlreadyExistsException
581 /*bool exception_thrown = false;
585 catch(InvalidPositionException &e){
586 exception_thrown = true;
588 assert(exception_thrown);*/
595 void TestSingleFixed()
598 OneChildHeightmap hm1(BS1);
600 // Test that it is filled with < GROUNDHEIGHT_VALID_MINVALUE
601 for(s16 y=0; y<=BS1; y++){
602 for(s16 x=0; x<=BS1; x++){
604 assert(hm1.m_child.getGroundHeight(p)
605 < GROUNDHEIGHT_VALID_MINVALUE);
609 hm1.m_child.setGroundHeight(v2s16(1,0), 2.0);
610 //hm1.m_child.print();
611 assert(fabs(hm1.getGroundHeight(v2s16(1,0))-2.0)<0.001);
612 hm1.setGroundHeight(v2s16(0,1), 3.0);
613 assert(fabs(hm1.m_child.getGroundHeight(v2s16(0,1))-3.0)<0.001);
616 for(s16 y=0; y<=BS1; y++){
617 for(s16 x=0; x<=BS1; x++){
619 hm1.m_child.setGroundHeight(p, -1.0);
623 f32 corners[] = {0.0, 0.0, 1.0, 1.0};
624 hm1.m_child.generateContinued(0.0, 0.0, corners);
627 assert(fabs(hm1.m_child.getGroundHeight(v2s16(1,0))-0.2)<0.05);
628 assert(fabs(hm1.m_child.getGroundHeight(v2s16(4,3))-0.7)<0.05);
629 assert(fabs(hm1.m_child.getGroundHeight(v2s16(4,4))-1.0)<0.05);
634 //g_heightmap_debugprint = true;
636 UnlimitedHeightmap hm1(BS1,
637 new ConstantGenerator(0.0),
638 new ConstantGenerator(0.0),
639 new ConstantGenerator(5.0));
640 // Go through it so it generates itself
641 for(s16 y=0; y<=BS1; y++){
642 for(s16 x=0; x<=BS1; x++){
644 hm1.getGroundHeight(p);
648 dstream<<"UnlimitedHeightmap hm1:"<<std::endl;
651 dstream<<"testing UnlimitedHeightmap set/get"<<std::endl;
654 // Get first heightmap and try setGroundHeight
655 FixedHeightmap * href = hm1.getHeightmap(v2s16(0,0));
656 href->setGroundHeight(p1, v1);
657 // Read from UnlimitedHeightmap
658 assert(fabs(hm1.getGroundHeight(p1)-v1)<0.001);
663 dstream<<"Running random code (get a human to check this)"<<std::endl;
664 dstream<<"rand() values: ";
665 for(u16 i=0; i<5; i++)
666 dstream<<(u16)rand()<<" ";
670 UnlimitedHeightmap hm1(BS1,
671 new ConstantGenerator(10.0),
672 new ConstantGenerator(0.3),
673 new ConstantGenerator(0.0));
675 // Force hm1 to generate a some heightmap
676 hm1.getGroundHeight(v2s16(0,0));
677 hm1.getGroundHeight(v2s16(0,BS1));
678 /*hm1.getGroundHeight(v2s16(BS1,-1));
679 hm1.getGroundHeight(v2s16(BS1-1,-1));*/
682 // Get the (0,0) and (1,0) heightmaps
683 /*FixedHeightmap * hr00 = hm1.getHeightmap(v2s16(0,0));
684 FixedHeightmap * hr01 = hm1.getHeightmap(v2s16(1,0));
685 f32 corners[] = {1.0, 1.0, 1.0, 1.0};
686 hr00->generateContinued(0.0, 0.0, corners);
694 //srand(7); // Get constant random
695 srand(time(0)); // Get better random
707 const int port = 30003;
711 const char sendbuffer[] = "hello world!";
712 socket.Send(Address(127,0,0,1,port), sendbuffer, sizeof(sendbuffer));
717 memset(rcvbuffer, 0, sizeof(rcvbuffer));
721 int bytes_read = socket.Receive(sender, rcvbuffer, sizeof(rcvbuffer));
725 //FIXME: This fails on some systems
726 assert(strncmp(sendbuffer, rcvbuffer, sizeof(sendbuffer))==0);
727 assert(sender.getAddress() == Address(127,0,0,1, 0).getAddress());
731 struct TestConnection
736 Test helper functions
739 // Some constants for testing
740 u32 proto_id = 0x12345678;
743 SharedBuffer<u8> data1(1);
745 Address a(127,0,0,1, 10);
748 con::BufferedPacket p1 = con::makePacket(a, data1,
749 proto_id, peer_id, channel);
751 We should now have a packet with this data:
754 [4] u16 sender_peer_id
759 assert(readU32(&p1.data[0]) == proto_id);
760 assert(readU16(&p1.data[4]) == peer_id);
761 assert(readU8(&p1.data[6]) == channel);
762 assert(readU8(&p1.data[7]) == data1[0]);
764 //dstream<<"initial data1[0]="<<((u32)data1[0]&0xff)<<std::endl;
766 SharedBuffer<u8> p2 = con::makeReliablePacket(data1, seqnum);
768 /*dstream<<"p2.getSize()="<<p2.getSize()<<", data1.getSize()="
769 <<data1.getSize()<<std::endl;
770 dstream<<"readU8(&p2[3])="<<readU8(&p2[3])
771 <<" p2[3]="<<((u32)p2[3]&0xff)<<std::endl;
772 dstream<<"data1[0]="<<((u32)data1[0]&0xff)<<std::endl;*/
774 assert(p2.getSize() == 3 + data1.getSize());
775 assert(readU8(&p2[0]) == TYPE_RELIABLE);
776 assert(readU16(&p2[1]) == seqnum);
777 assert(readU8(&p2[3]) == data1[0]);
780 struct Handler : public con::PeerHandler
782 Handler(const char *a_name)
788 void peerAdded(con::Peer *peer)
790 dstream<<"Handler("<<name<<")::peerAdded(): "
791 "id="<<peer->id<<std::endl;
795 void deletingPeer(con::Peer *peer, bool timeout)
797 dstream<<"Handler("<<name<<")::deletingPeer(): "
799 <<", timeout="<<timeout<<std::endl;
811 DSTACK("TestConnection::Run");
816 Test some real connections
818 u32 proto_id = 0xad26846a;
820 Handler hand_server("server");
821 Handler hand_client("client");
823 dstream<<"** Creating server Connection"<<std::endl;
824 con::Connection server(proto_id, 512, 5.0, &hand_server);
827 dstream<<"** Creating client Connection"<<std::endl;
828 con::Connection client(proto_id, 512, 5.0, &hand_client);
830 assert(hand_server.count == 0);
831 assert(hand_client.count == 0);
835 Address server_address(127,0,0,1, 30001);
836 dstream<<"** running client.Connect()"<<std::endl;
837 client.Connect(server_address);
841 // Client should have added server now
842 assert(hand_client.count == 1);
843 assert(hand_client.last_id == 1);
844 // But server should not have added client
845 assert(hand_server.count == 0);
851 dstream<<"** running server.Receive()"<<std::endl;
852 u32 size = server.Receive(peer_id, data, 100);
853 dstream<<"** Server received: peer_id="<<peer_id
857 catch(con::NoIncomingDataException &e)
859 // No actual data received, but the client has
860 // probably been connected
863 // Client should be the same
864 assert(hand_client.count == 1);
865 assert(hand_client.last_id == 1);
866 // Server should have the client
867 assert(hand_server.count == 1);
868 assert(hand_server.last_id == 2);
872 while(client.Connected() == false)
878 dstream<<"** running client.Receive()"<<std::endl;
879 u32 size = client.Receive(peer_id, data, 100);
880 dstream<<"** Client received: peer_id="<<peer_id
884 catch(con::NoIncomingDataException &e)
896 dstream<<"** running server.Receive()"<<std::endl;
897 u32 size = server.Receive(peer_id, data, 100);
898 dstream<<"** Server received: peer_id="<<peer_id
902 catch(con::NoIncomingDataException &e)
907 /*u8 data[] = "Hello World!";
908 u32 datasize = sizeof(data);*/
909 SharedBuffer<u8> data = SharedBufferFromString("Hello World!");
911 dstream<<"** running client.Send()"<<std::endl;
912 client.Send(PEER_ID_SERVER, 0, data, true);
918 dstream<<"** running server.Receive()"<<std::endl;
919 u32 size = server.Receive(peer_id, recvdata, 100);
920 dstream<<"** Server received: peer_id="<<peer_id
924 assert(memcmp(*data, recvdata, data.getSize()) == 0);
927 u16 peer_id_client = 2;
931 Send consequent packets in different order
933 //u8 data1[] = "hello1";
934 //u8 data2[] = "hello2";
935 SharedBuffer<u8> data1 = SharedBufferFromString("hello1");
936 SharedBuffer<u8> data2 = SharedBufferFromString("Hello2");
938 Address client_address =
939 server.GetPeer(peer_id_client)->address;
941 dstream<<"*** Sending packets in wrong order (2,1,2)"
945 con::Channel *ch = &server.GetPeer(peer_id_client)->channels[chn];
946 u16 sn = ch->next_outgoing_seqnum;
947 ch->next_outgoing_seqnum = sn+1;
948 server.Send(peer_id_client, chn, data2, true);
949 ch->next_outgoing_seqnum = sn;
950 server.Send(peer_id_client, chn, data1, true);
951 ch->next_outgoing_seqnum = sn+1;
952 server.Send(peer_id_client, chn, data2, true);
956 dstream<<"*** Receiving the packets"<<std::endl;
962 dstream<<"** running client.Receive()"<<std::endl;
964 size = client.Receive(peer_id, recvdata, 20);
965 dstream<<"** Client received: peer_id="<<peer_id
967 <<", data="<<recvdata
969 assert(size == data1.getSize());
970 assert(memcmp(*data1, recvdata, data1.getSize()) == 0);
971 assert(peer_id == PEER_ID_SERVER);
973 dstream<<"** running client.Receive()"<<std::endl;
975 size = client.Receive(peer_id, recvdata, 20);
976 dstream<<"** Client received: peer_id="<<peer_id
978 <<", data="<<recvdata
980 assert(size == data2.getSize());
981 assert(memcmp(*data2, recvdata, data2.getSize()) == 0);
982 assert(peer_id == PEER_ID_SERVER);
984 bool got_exception = false;
987 dstream<<"** running client.Receive()"<<std::endl;
989 size = client.Receive(peer_id, recvdata, 20);
990 dstream<<"** Client received: peer_id="<<peer_id
992 <<", data="<<recvdata
995 catch(con::NoIncomingDataException &e)
997 dstream<<"** No incoming data for client"<<std::endl;
998 got_exception = true;
1000 assert(got_exception);
1004 SharedBuffer<u8> data1(1100);
1005 for(u16 i=0; i<1100; i++){
1009 dstream<<"Sending data (size="<<1100<<"):";
1010 for(int i=0; i<1100 && i<20; i++){
1011 if(i%2==0) printf(" ");
1012 printf("%.2X", ((int)((const char*)*data1)[i])&0xff);
1018 server.Send(peer_id_client, 0, data1, true);
1023 dstream<<"** running client.Receive()"<<std::endl;
1025 u16 size = client.Receive(peer_id, recvdata, 2000);
1026 dstream<<"** Client received: peer_id="<<peer_id
1030 dstream<<"Received data (size="<<size<<"):";
1031 for(int i=0; i<size && i<20; i++){
1032 if(i%2==0) printf(" ");
1033 printf("%.2X", ((int)((const char*)recvdata)[i])&0xff);
1039 assert(memcmp(*data1, recvdata, data1.getSize()) == 0);
1040 assert(peer_id == PEER_ID_SERVER);
1043 // Check peer handlers
1044 assert(hand_client.count == 1);
1045 assert(hand_client.last_id == 1);
1046 assert(hand_server.count == 1);
1047 assert(hand_server.last_id == 2);
1056 dstream<<"Running " #X <<std::endl;\
1062 DSTACK(__FUNCTION_NAME);
1063 dstream<<"run_tests() started"<<std::endl;
1064 TEST(TestUtilities);
1067 TEST(TestVoxelManipulator);
1069 TEST(TestMapSector);
1070 TEST(TestHeightmap);
1071 if(INTERNET_SIMULATOR == false){
1073 dout_con<<"=== BEGIN RUNNING UNIT TESTS FOR CONNECTION ==="<<std::endl;
1074 TEST(TestConnection);
1075 dout_con<<"=== END RUNNING UNIT TESTS FOR CONNECTION ==="<<std::endl;
1077 dstream<<"run_tests() passed"<<std::endl;