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"
27 #include "connection.h"
29 #include "serialization.h"
33 #include "content_mapnode.h"
34 #include "mapsector.h"
37 Asserts that the exception occurs
39 #define EXCEPTION_CHECK(EType, code)\
41 bool exception_thrown = false;\
43 catch(EType &e) { exception_thrown = true; }\
44 assert(exception_thrown);\
51 /*dstream<<"wrapDegrees(100.0) = "<<wrapDegrees(100.0)<<std::endl;
52 dstream<<"wrapDegrees(720.5) = "<<wrapDegrees(720.5)<<std::endl;
53 dstream<<"wrapDegrees(-0.5) = "<<wrapDegrees(-0.5)<<std::endl;*/
54 assert(fabs(wrapDegrees(100.0) - 100.0) < 0.001);
55 assert(fabs(wrapDegrees(720.5) - 0.5) < 0.001);
56 assert(fabs(wrapDegrees(-0.5) - (-0.5)) < 0.001);
57 assert(fabs(wrapDegrees(-365.5) - (-5.5)) < 0.001);
58 assert(lowercase("Foo bAR") == "foo bar");
59 assert(is_yes("YeS") == true);
60 assert(is_yes("") == false);
61 assert(is_yes("FAlse") == false);
70 // Test reading of settings
71 s.parseConfigLine("leet = 1337");
72 s.parseConfigLine("leetleet = 13371337");
73 s.parseConfigLine("leetleet_neg = -13371337");
74 s.parseConfigLine("floaty_thing = 1.1");
75 s.parseConfigLine("stringy_thing = asd /( ¤%&(/\" BLÖÄRP");
76 s.parseConfigLine("coord = (1, 2, 4.5)");
77 assert(s.getS32("leet") == 1337);
78 assert(s.getS16("leetleet") == 32767);
79 assert(s.getS16("leetleet_neg") == -32768);
80 // Not sure if 1.1 is an exact value as a float, but doesn't matter
81 assert(fabs(s.getFloat("floaty_thing") - 1.1) < 0.001);
82 assert(s.get("stringy_thing") == "asd /( ¤%&(/\" BLÖÄRP");
83 assert(fabs(s.getV3F("coord").X - 1.0) < 0.001);
84 assert(fabs(s.getV3F("coord").Y - 2.0) < 0.001);
85 assert(fabs(s.getV3F("coord").Z - 4.5) < 0.001);
86 // Test the setting of settings too
87 s.setFloat("floaty_thing_2", 1.2);
88 s.setV3F("coord2", v3f(1, 2, 3.3));
89 assert(s.get("floaty_thing_2").substr(0,3) == "1.2");
90 assert(fabs(s.getFloat("floaty_thing_2") - 1.2) < 0.001);
91 assert(fabs(s.getV3F("coord2").X - 1.0) < 0.001);
92 assert(fabs(s.getV3F("coord2").Y - 2.0) < 0.001);
93 assert(fabs(s.getV3F("coord2").Z - 3.3) < 0.001);
103 SharedBuffer<u8> fromdata(4);
109 std::ostringstream os(std::ios_base::binary);
110 compress(fromdata, os, 0);
112 std::string str_out = os.str();
114 dstream<<"str_out.size()="<<str_out.size()<<std::endl;
115 dstream<<"TestCompress: 1,5,5,1 -> ";
116 for(u32 i=0; i<str_out.size(); i++)
118 dstream<<(u32)str_out[i]<<",";
122 assert(str_out.size() == 10);
124 assert(str_out[0] == 0);
125 assert(str_out[1] == 0);
126 assert(str_out[2] == 0);
127 assert(str_out[3] == 4);
128 assert(str_out[4] == 0);
129 assert(str_out[5] == 1);
130 assert(str_out[6] == 1);
131 assert(str_out[7] == 5);
132 assert(str_out[8] == 0);
133 assert(str_out[9] == 1);
135 std::istringstream is(str_out, std::ios_base::binary);
136 std::ostringstream os2(std::ios_base::binary);
138 decompress(is, os2, 0);
139 std::string str_out2 = os2.str();
141 dstream<<"decompress: ";
142 for(u32 i=0; i<str_out2.size(); i++)
144 dstream<<(u32)str_out2[i]<<",";
148 assert(str_out2.size() == fromdata.getSize());
150 for(u32 i=0; i<str_out2.size(); i++)
152 assert(str_out2[i] == fromdata[i]);
159 SharedBuffer<u8> fromdata(4);
165 std::ostringstream os(std::ios_base::binary);
166 compress(fromdata, os, SER_FMT_VER_HIGHEST);
168 std::string str_out = os.str();
170 dstream<<"str_out.size()="<<str_out.size()<<std::endl;
171 dstream<<"TestCompress: 1,5,5,1 -> ";
172 for(u32 i=0; i<str_out.size(); i++)
174 dstream<<(u32)str_out[i]<<",";
178 /*assert(str_out.size() == 10);
180 assert(str_out[0] == 0);
181 assert(str_out[1] == 0);
182 assert(str_out[2] == 0);
183 assert(str_out[3] == 4);
184 assert(str_out[4] == 0);
185 assert(str_out[5] == 1);
186 assert(str_out[6] == 1);
187 assert(str_out[7] == 5);
188 assert(str_out[8] == 0);
189 assert(str_out[9] == 1);*/
191 std::istringstream is(str_out, std::ios_base::binary);
192 std::ostringstream os2(std::ios_base::binary);
194 decompress(is, os2, SER_FMT_VER_HIGHEST);
195 std::string str_out2 = os2.str();
197 dstream<<"decompress: ";
198 for(u32 i=0; i<str_out2.size(); i++)
200 dstream<<(u32)str_out2[i]<<",";
204 assert(str_out2.size() == fromdata.getSize());
206 for(u32 i=0; i<str_out2.size(); i++)
208 assert(str_out2[i] == fromdata[i]);
222 assert(n.getContent() == CONTENT_AIR);
223 assert(n.getLight(LIGHTBANK_DAY) == 0);
224 assert(n.getLight(LIGHTBANK_NIGHT) == 0);
227 n.setContent(CONTENT_AIR);
228 assert(n.light_propagates() == true);
229 n.setContent(CONTENT_STONE);
230 assert(n.light_propagates() == false);
234 struct TestVoxelManipulator
242 VoxelArea a(v3s16(-1,-1,-1), v3s16(1,1,1));
243 assert(a.index(0,0,0) == 1*3*3 + 1*3 + 1);
244 assert(a.index(-1,-1,-1) == 0);
246 VoxelArea c(v3s16(-2,-2,-2), v3s16(2,2,2));
247 // An area that is 1 bigger in x+ and z-
248 VoxelArea d(v3s16(-2,-2,-3), v3s16(3,2,2));
250 core::list<VoxelArea> aa;
254 core::array<VoxelArea> results;
255 results.push_back(VoxelArea(v3s16(-2,-2,-3),v3s16(3,2,-3)));
256 results.push_back(VoxelArea(v3s16(3,-2,-2),v3s16(3,2,2)));
258 assert(aa.size() == results.size());
260 dstream<<"Result of diff:"<<std::endl;
261 for(core::list<VoxelArea>::Iterator
262 i = aa.begin(); i != aa.end(); i++)
267 s32 j = results.linear_search(*i);
281 dstream<<"*** Setting (-1,0,-1)=2 ***"<<std::endl;
283 v.setNodeNoRef(v3s16(-1,0,-1), MapNode(2));
287 assert(v.getNode(v3s16(-1,0,-1)).getContent() == 2);
289 dstream<<"*** Reading from inexistent (0,0,-1) ***"<<std::endl;
291 EXCEPTION_CHECK(InvalidPositionException, v.getNode(v3s16(0,0,-1)));
295 dstream<<"*** Adding area ***"<<std::endl;
301 assert(v.getNode(v3s16(-1,0,-1)).getContent() == 2);
302 EXCEPTION_CHECK(InvalidPositionException, v.getNode(v3s16(0,1,1)));
311 const char *content =
323 v3s16 size(12, 4, 2);
324 VoxelArea area(v3s16(0,0,0), size-v3s16(1,1,1));
326 const char *p = content;
327 for(s16 z=0; z<size.Z; z++)
328 for(s16 y=size.Y-1; y>=0; y--)
329 for(s16 x=0; x<size.X; x++)
332 //n.pressure = size.Y - y;
334 n.setContent(CONTENT_STONE);
336 n.setContent(CONTENT_WATER);
338 n.setContent(CONTENT_AIR);
341 v.setNode(v3s16(x,y,z), n);
345 v.print(dstream, VOXELPRINT_WATERPRESSURE);
347 core::map<v3s16, u8> active_nodes;
348 v.updateAreaWaterPressure(area, active_nodes);
350 v.print(dstream, VOXELPRINT_WATERPRESSURE);
352 //s16 highest_y = -32768;
354 NOTE: These are commented out because this behaviour is changed
357 //assert(v.getWaterPressure(v3s16(7, 1, 1), highest_y, 0) == -1);
358 //assert(highest_y == 3);
359 /*assert(v.getWaterPressure(v3s16(7, 1, 1), highest_y, 0) == 3);
360 //assert(highest_y == 3);*/
362 active_nodes.clear();
363 active_nodes[v3s16(9,1,0)] = 1;
364 //v.flowWater(active_nodes, 0, true, 1000);
365 v.flowWater(active_nodes, 0, false, 1000);
367 dstream<<"Final result of flowWater:"<<std::endl;
368 v.print(dstream, VOXELPRINT_WATERPRESSURE);
376 NOTE: These tests became non-working then NodeContainer was removed.
377 These should be redone, utilizing some kind of a virtual
378 interface for Map (IMap would be fine).
383 class TC : public NodeContainer
389 core::list<v3s16> validity_exceptions;
393 position_valid = true;
396 virtual bool isValidPosition(v3s16 p)
398 //return position_valid ^ (p==position_valid_exception);
399 bool exception = false;
400 for(core::list<v3s16>::Iterator i=validity_exceptions.begin();
401 i != validity_exceptions.end(); i++)
409 return exception ? !position_valid : position_valid;
412 virtual MapNode getNode(v3s16 p)
414 if(isValidPosition(p) == false)
415 throw InvalidPositionException();
419 virtual void setNode(v3s16 p, MapNode & n)
421 if(isValidPosition(p) == false)
422 throw InvalidPositionException();
425 virtual u16 nodeContainerId() const
435 MapBlock b(&parent, v3s16(1,1,1));
436 v3s16 relpos(MAP_BLOCKSIZE, MAP_BLOCKSIZE, MAP_BLOCKSIZE);
438 assert(b.getPosRelative() == relpos);
440 assert(b.getBox().MinEdge.X == MAP_BLOCKSIZE);
441 assert(b.getBox().MaxEdge.X == MAP_BLOCKSIZE*2-1);
442 assert(b.getBox().MinEdge.Y == MAP_BLOCKSIZE);
443 assert(b.getBox().MaxEdge.Y == MAP_BLOCKSIZE*2-1);
444 assert(b.getBox().MinEdge.Z == MAP_BLOCKSIZE);
445 assert(b.getBox().MaxEdge.Z == MAP_BLOCKSIZE*2-1);
447 assert(b.isValidPosition(v3s16(0,0,0)) == true);
448 assert(b.isValidPosition(v3s16(-1,0,0)) == false);
449 assert(b.isValidPosition(v3s16(-1,-142,-2341)) == false);
450 assert(b.isValidPosition(v3s16(-124,142,2341)) == false);
451 assert(b.isValidPosition(v3s16(MAP_BLOCKSIZE-1,MAP_BLOCKSIZE-1,MAP_BLOCKSIZE-1)) == true);
452 assert(b.isValidPosition(v3s16(MAP_BLOCKSIZE-1,MAP_BLOCKSIZE,MAP_BLOCKSIZE-1)) == false);
455 TODO: this method should probably be removed
456 if the block size isn't going to be set variable
458 /*assert(b.getSizeNodes() == v3s16(MAP_BLOCKSIZE,
459 MAP_BLOCKSIZE, MAP_BLOCKSIZE));*/
461 // Changed flag should be initially set
462 assert(b.getChangedFlag() == true);
463 b.resetChangedFlag();
464 assert(b.getChangedFlag() == false);
466 // All nodes should have been set to
467 // .d=CONTENT_IGNORE and .getLight() = 0
468 for(u16 z=0; z<MAP_BLOCKSIZE; z++)
469 for(u16 y=0; y<MAP_BLOCKSIZE; y++)
470 for(u16 x=0; x<MAP_BLOCKSIZE; x++)
472 //assert(b.getNode(v3s16(x,y,z)).getContent() == CONTENT_AIR);
473 assert(b.getNode(v3s16(x,y,z)).getContent() == CONTENT_IGNORE);
474 assert(b.getNode(v3s16(x,y,z)).getLight(LIGHTBANK_DAY) == 0);
475 assert(b.getNode(v3s16(x,y,z)).getLight(LIGHTBANK_NIGHT) == 0);
479 MapNode n(CONTENT_AIR);
480 for(u16 z=0; z<MAP_BLOCKSIZE; z++)
481 for(u16 y=0; y<MAP_BLOCKSIZE; y++)
482 for(u16 x=0; x<MAP_BLOCKSIZE; x++)
484 b.setNode(v3s16(x,y,z), n);
489 Parent fetch functions
491 parent.position_valid = false;
492 parent.node.setContent(5);
496 // Positions in the block should still be valid
497 assert(b.isValidPositionParent(v3s16(0,0,0)) == true);
498 assert(b.isValidPositionParent(v3s16(MAP_BLOCKSIZE-1,MAP_BLOCKSIZE-1,MAP_BLOCKSIZE-1)) == true);
499 n = b.getNodeParent(v3s16(0,MAP_BLOCKSIZE-1,0));
500 assert(n.getContent() == CONTENT_AIR);
502 // ...but outside the block they should be invalid
503 assert(b.isValidPositionParent(v3s16(-121,2341,0)) == false);
504 assert(b.isValidPositionParent(v3s16(-1,0,0)) == false);
505 assert(b.isValidPositionParent(v3s16(MAP_BLOCKSIZE-1,MAP_BLOCKSIZE-1,MAP_BLOCKSIZE)) == false);
508 bool exception_thrown = false;
510 // This should throw an exception
511 MapNode n = b.getNodeParent(v3s16(0,0,-1));
513 catch(InvalidPositionException &e)
515 exception_thrown = true;
517 assert(exception_thrown);
520 parent.position_valid = true;
521 // Now the positions outside should be valid
522 assert(b.isValidPositionParent(v3s16(-121,2341,0)) == true);
523 assert(b.isValidPositionParent(v3s16(-1,0,0)) == true);
524 assert(b.isValidPositionParent(v3s16(MAP_BLOCKSIZE-1,MAP_BLOCKSIZE-1,MAP_BLOCKSIZE)) == true);
525 n = b.getNodeParent(v3s16(0,0,MAP_BLOCKSIZE));
526 assert(n.getContent() == 5);
534 assert(b.getNode(p).getContent() == 4);
535 //TODO: Update to new system
536 /*assert(b.getNodeTile(p) == 4);
537 assert(b.getNodeTile(v3s16(-1,-1,0)) == 5);*/
542 // Set lighting of all nodes to 0
543 for(u16 z=0; z<MAP_BLOCKSIZE; z++){
544 for(u16 y=0; y<MAP_BLOCKSIZE; y++){
545 for(u16 x=0; x<MAP_BLOCKSIZE; x++){
546 MapNode n = b.getNode(v3s16(x,y,z));
547 n.setLight(LIGHTBANK_DAY, 0);
548 n.setLight(LIGHTBANK_NIGHT, 0);
549 b.setNode(v3s16(x,y,z), n);
555 Check how the block handles being a lonely sky block
557 parent.position_valid = true;
558 b.setIsUnderground(false);
559 parent.node.setContent(CONTENT_AIR);
560 parent.node.setLight(LIGHTBANK_DAY, LIGHT_SUN);
561 parent.node.setLight(LIGHTBANK_NIGHT, 0);
562 core::map<v3s16, bool> light_sources;
563 // The bottom block is invalid, because we have a shadowing node
564 assert(b.propagateSunlight(light_sources) == false);
565 assert(b.getNode(v3s16(1,4,0)).getLight(LIGHTBANK_DAY) == LIGHT_SUN);
566 assert(b.getNode(v3s16(1,3,0)).getLight(LIGHTBANK_DAY) == LIGHT_SUN);
567 assert(b.getNode(v3s16(1,2,0)).getLight(LIGHTBANK_DAY) == 0);
568 assert(b.getNode(v3s16(1,1,0)).getLight(LIGHTBANK_DAY) == 0);
569 assert(b.getNode(v3s16(1,0,0)).getLight(LIGHTBANK_DAY) == 0);
570 assert(b.getNode(v3s16(1,2,3)).getLight(LIGHTBANK_DAY) == LIGHT_SUN);
571 assert(b.getFaceLight2(1000, p, v3s16(0,1,0)) == LIGHT_SUN);
572 assert(b.getFaceLight2(1000, p, v3s16(0,-1,0)) == 0);
573 assert(b.getFaceLight2(0, p, v3s16(0,-1,0)) == 0);
574 // According to MapBlock::getFaceLight,
575 // The face on the z+ side should have double-diminished light
576 //assert(b.getFaceLight(p, v3s16(0,0,1)) == diminish_light(diminish_light(LIGHT_MAX)));
577 // The face on the z+ side should have diminished light
578 assert(b.getFaceLight2(1000, p, v3s16(0,0,1)) == diminish_light(LIGHT_MAX));
581 Check how the block handles being in between blocks with some non-sunlight
582 while being underground
585 // Make neighbours to exist and set some non-sunlight to them
586 parent.position_valid = true;
587 b.setIsUnderground(true);
588 parent.node.setLight(LIGHTBANK_DAY, LIGHT_MAX/2);
589 core::map<v3s16, bool> light_sources;
590 // The block below should be valid because there shouldn't be
591 // sunlight in there either
592 assert(b.propagateSunlight(light_sources, true) == true);
593 // Should not touch nodes that are not affected (that is, all of them)
594 //assert(b.getNode(v3s16(1,2,3)).getLight() == LIGHT_SUN);
595 // Should set light of non-sunlighted blocks to 0.
596 assert(b.getNode(v3s16(1,2,3)).getLight(LIGHTBANK_DAY) == 0);
599 Set up a situation where:
600 - There is only air in this block
601 - There is a valid non-sunlighted block at the bottom, and
602 - Invalid blocks elsewhere.
603 - the block is not underground.
605 This should result in bottom block invalidity
608 b.setIsUnderground(false);
610 for(u16 z=0; z<MAP_BLOCKSIZE; z++){
611 for(u16 y=0; y<MAP_BLOCKSIZE; y++){
612 for(u16 x=0; x<MAP_BLOCKSIZE; x++){
614 n.setContent(CONTENT_AIR);
615 n.setLight(LIGHTBANK_DAY, 0);
616 b.setNode(v3s16(x,y,z), n);
620 // Make neighbours invalid
621 parent.position_valid = false;
622 // Add exceptions to the top of the bottom block
623 for(u16 x=0; x<MAP_BLOCKSIZE; x++)
624 for(u16 z=0; z<MAP_BLOCKSIZE; z++)
626 parent.validity_exceptions.push_back(v3s16(MAP_BLOCKSIZE+x, MAP_BLOCKSIZE-1, MAP_BLOCKSIZE+z));
628 // Lighting value for the valid nodes
629 parent.node.setLight(LIGHTBANK_DAY, LIGHT_MAX/2);
630 core::map<v3s16, bool> light_sources;
631 // Bottom block is not valid
632 assert(b.propagateSunlight(light_sources) == false);
639 class TC : public NodeContainer
648 position_valid = true;
651 virtual bool isValidPosition(v3s16 p)
653 return position_valid;
656 virtual MapNode getNode(v3s16 p)
658 if(position_valid == false)
659 throw InvalidPositionException();
663 virtual void setNode(v3s16 p, MapNode & n)
665 if(position_valid == false)
666 throw InvalidPositionException();
669 virtual u16 nodeContainerId() const
678 parent.position_valid = false;
680 // Create one with no heightmaps
681 ServerMapSector sector(&parent, v2s16(1,1));
683 assert(sector.getBlockNoCreateNoEx(0) == 0);
684 assert(sector.getBlockNoCreateNoEx(1) == 0);
686 MapBlock * bref = sector.createBlankBlock(-2);
688 assert(sector.getBlockNoCreateNoEx(0) == 0);
689 assert(sector.getBlockNoCreateNoEx(-2) == bref);
691 //TODO: Check for AlreadyExistsException
693 /*bool exception_thrown = false;
697 catch(InvalidPositionException &e){
698 exception_thrown = true;
700 assert(exception_thrown);*/
710 const int port = 30003;
714 const char sendbuffer[] = "hello world!";
715 socket.Send(Address(127,0,0,1,port), sendbuffer, sizeof(sendbuffer));
720 memset(rcvbuffer, 0, sizeof(rcvbuffer));
724 int bytes_read = socket.Receive(sender, rcvbuffer, sizeof(rcvbuffer));
728 //FIXME: This fails on some systems
729 assert(strncmp(sendbuffer, rcvbuffer, sizeof(sendbuffer))==0);
730 assert(sender.getAddress() == Address(127,0,0,1, 0).getAddress());
734 struct TestConnection
739 Test helper functions
742 // Some constants for testing
743 u32 proto_id = 0x12345678;
746 SharedBuffer<u8> data1(1);
748 Address a(127,0,0,1, 10);
751 con::BufferedPacket p1 = con::makePacket(a, data1,
752 proto_id, peer_id, channel);
754 We should now have a packet with this data:
757 [4] u16 sender_peer_id
762 assert(readU32(&p1.data[0]) == proto_id);
763 assert(readU16(&p1.data[4]) == peer_id);
764 assert(readU8(&p1.data[6]) == channel);
765 assert(readU8(&p1.data[7]) == data1[0]);
767 //dstream<<"initial data1[0]="<<((u32)data1[0]&0xff)<<std::endl;
769 SharedBuffer<u8> p2 = con::makeReliablePacket(data1, seqnum);
771 /*dstream<<"p2.getSize()="<<p2.getSize()<<", data1.getSize()="
772 <<data1.getSize()<<std::endl;
773 dstream<<"readU8(&p2[3])="<<readU8(&p2[3])
774 <<" p2[3]="<<((u32)p2[3]&0xff)<<std::endl;
775 dstream<<"data1[0]="<<((u32)data1[0]&0xff)<<std::endl;*/
777 assert(p2.getSize() == 3 + data1.getSize());
778 assert(readU8(&p2[0]) == TYPE_RELIABLE);
779 assert(readU16(&p2[1]) == seqnum);
780 assert(readU8(&p2[3]) == data1[0]);
783 struct Handler : public con::PeerHandler
785 Handler(const char *a_name)
791 void peerAdded(con::Peer *peer)
793 dstream<<"Handler("<<name<<")::peerAdded(): "
794 "id="<<peer->id<<std::endl;
798 void deletingPeer(con::Peer *peer, bool timeout)
800 dstream<<"Handler("<<name<<")::deletingPeer(): "
802 <<", timeout="<<timeout<<std::endl;
814 DSTACK("TestConnection::Run");
819 Test some real connections
821 u32 proto_id = 0xad26846a;
823 Handler hand_server("server");
824 Handler hand_client("client");
826 dstream<<"** Creating server Connection"<<std::endl;
827 con::Connection server(proto_id, 512, 5.0, &hand_server);
830 dstream<<"** Creating client Connection"<<std::endl;
831 con::Connection client(proto_id, 512, 5.0, &hand_client);
833 assert(hand_server.count == 0);
834 assert(hand_client.count == 0);
838 Address server_address(127,0,0,1, 30001);
839 dstream<<"** running client.Connect()"<<std::endl;
840 client.Connect(server_address);
844 // Client should have added server now
845 assert(hand_client.count == 1);
846 assert(hand_client.last_id == 1);
847 // But server should not have added client
848 assert(hand_server.count == 0);
854 dstream<<"** running server.Receive()"<<std::endl;
855 u32 size = server.Receive(peer_id, data, 100);
856 dstream<<"** Server received: peer_id="<<peer_id
860 catch(con::NoIncomingDataException &e)
862 // No actual data received, but the client has
863 // probably been connected
866 // Client should be the same
867 assert(hand_client.count == 1);
868 assert(hand_client.last_id == 1);
869 // Server should have the client
870 assert(hand_server.count == 1);
871 assert(hand_server.last_id == 2);
875 while(client.Connected() == false)
881 dstream<<"** running client.Receive()"<<std::endl;
882 u32 size = client.Receive(peer_id, data, 100);
883 dstream<<"** Client received: peer_id="<<peer_id
887 catch(con::NoIncomingDataException &e)
899 dstream<<"** running server.Receive()"<<std::endl;
900 u32 size = server.Receive(peer_id, data, 100);
901 dstream<<"** Server received: peer_id="<<peer_id
905 catch(con::NoIncomingDataException &e)
910 /*u8 data[] = "Hello World!";
911 u32 datasize = sizeof(data);*/
912 SharedBuffer<u8> data = SharedBufferFromString("Hello World!");
914 dstream<<"** running client.Send()"<<std::endl;
915 client.Send(PEER_ID_SERVER, 0, data, true);
921 dstream<<"** running server.Receive()"<<std::endl;
922 u32 size = server.Receive(peer_id, recvdata, 100);
923 dstream<<"** Server received: peer_id="<<peer_id
927 assert(memcmp(*data, recvdata, data.getSize()) == 0);
930 u16 peer_id_client = 2;
934 Send consequent packets in different order
936 //u8 data1[] = "hello1";
937 //u8 data2[] = "hello2";
938 SharedBuffer<u8> data1 = SharedBufferFromString("hello1");
939 SharedBuffer<u8> data2 = SharedBufferFromString("Hello2");
941 Address client_address =
942 server.GetPeer(peer_id_client)->address;
944 dstream<<"*** Sending packets in wrong order (2,1,2)"
948 con::Channel *ch = &server.GetPeer(peer_id_client)->channels[chn];
949 u16 sn = ch->next_outgoing_seqnum;
950 ch->next_outgoing_seqnum = sn+1;
951 server.Send(peer_id_client, chn, data2, true);
952 ch->next_outgoing_seqnum = sn;
953 server.Send(peer_id_client, chn, data1, true);
954 ch->next_outgoing_seqnum = sn+1;
955 server.Send(peer_id_client, chn, data2, true);
959 dstream<<"*** Receiving the packets"<<std::endl;
965 dstream<<"** running client.Receive()"<<std::endl;
967 size = client.Receive(peer_id, recvdata, 20);
968 dstream<<"** Client received: peer_id="<<peer_id
970 <<", data="<<recvdata
972 assert(size == data1.getSize());
973 assert(memcmp(*data1, recvdata, data1.getSize()) == 0);
974 assert(peer_id == PEER_ID_SERVER);
976 dstream<<"** running client.Receive()"<<std::endl;
978 size = client.Receive(peer_id, recvdata, 20);
979 dstream<<"** Client received: peer_id="<<peer_id
981 <<", data="<<recvdata
983 assert(size == data2.getSize());
984 assert(memcmp(*data2, recvdata, data2.getSize()) == 0);
985 assert(peer_id == PEER_ID_SERVER);
987 bool got_exception = false;
990 dstream<<"** running client.Receive()"<<std::endl;
992 size = client.Receive(peer_id, recvdata, 20);
993 dstream<<"** Client received: peer_id="<<peer_id
995 <<", data="<<recvdata
998 catch(con::NoIncomingDataException &e)
1000 dstream<<"** No incoming data for client"<<std::endl;
1001 got_exception = true;
1003 assert(got_exception);
1006 const int datasize = 30000;
1007 SharedBuffer<u8> data1(datasize);
1008 for(u16 i=0; i<datasize; i++){
1012 dstream<<"Sending data (size="<<datasize<<"):";
1013 for(int i=0; i<datasize && i<20; i++){
1014 if(i%2==0) DEBUGPRINT(" ");
1015 DEBUGPRINT("%.2X", ((int)((const char*)*data1)[i])&0xff);
1021 server.Send(peer_id_client, 0, data1, true);
1025 u8 recvdata[datasize + 1000];
1026 dstream<<"** running client.Receive()"<<std::endl;
1028 u16 size = client.Receive(peer_id, recvdata, datasize + 1000);
1029 dstream<<"** Client received: peer_id="<<peer_id
1033 dstream<<"Received data (size="<<size<<"):";
1034 for(int i=0; i<size && i<20; i++){
1035 if(i%2==0) DEBUGPRINT(" ");
1036 DEBUGPRINT("%.2X", ((int)((const char*)recvdata)[i])&0xff);
1042 assert(memcmp(*data1, recvdata, data1.getSize()) == 0);
1043 assert(peer_id == PEER_ID_SERVER);
1046 // Check peer handlers
1047 assert(hand_client.count == 1);
1048 assert(hand_client.last_id == 1);
1049 assert(hand_server.count == 1);
1050 assert(hand_server.last_id == 2);
1059 dstream<<"Running " #X <<std::endl;\
1065 DSTACK(__FUNCTION_NAME);
1066 dstream<<"run_tests() started"<<std::endl;
1067 TEST(TestUtilities);
1071 TEST(TestVoxelManipulator);
1072 //TEST(TestMapBlock);
1073 //TEST(TestMapSector);
1074 if(INTERNET_SIMULATOR == false){
1076 dout_con<<"=== BEGIN RUNNING UNIT TESTS FOR CONNECTION ==="<<std::endl;
1077 TEST(TestConnection);
1078 dout_con<<"=== END RUNNING UNIT TESTS FOR CONNECTION ==="<<std::endl;
1080 dstream<<"run_tests() passed"<<std::endl;