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 Asserts that the exception occurs
38 #define EXCEPTION_CHECK(EType, code)\
40 bool exception_thrown = false;\
42 catch(EType &e) { exception_thrown = true; }\
43 assert(exception_thrown);\
50 /*dstream<<"wrapDegrees(100.0) = "<<wrapDegrees(100.0)<<std::endl;
51 dstream<<"wrapDegrees(720.5) = "<<wrapDegrees(720.5)<<std::endl;
52 dstream<<"wrapDegrees(-0.5) = "<<wrapDegrees(-0.5)<<std::endl;*/
53 assert(fabs(wrapDegrees(100.0) - 100.0) < 0.001);
54 assert(fabs(wrapDegrees(720.5) - 0.5) < 0.001);
55 assert(fabs(wrapDegrees(-0.5) - (-0.5)) < 0.001);
56 assert(fabs(wrapDegrees(-365.5) - (-5.5)) < 0.001);
57 assert(lowercase("Foo bAR") == "foo bar");
58 assert(is_yes("YeS") == true);
59 assert(is_yes("") == false);
60 assert(is_yes("FAlse") == false);
70 SharedBuffer<u8> fromdata(4);
76 std::ostringstream os(std::ios_base::binary);
77 compress(fromdata, os, 0);
79 std::string str_out = os.str();
81 dstream<<"str_out.size()="<<str_out.size()<<std::endl;
82 dstream<<"TestCompress: 1,5,5,1 -> ";
83 for(u32 i=0; i<str_out.size(); i++)
85 dstream<<(u32)str_out[i]<<",";
89 assert(str_out.size() == 10);
91 assert(str_out[0] == 0);
92 assert(str_out[1] == 0);
93 assert(str_out[2] == 0);
94 assert(str_out[3] == 4);
95 assert(str_out[4] == 0);
96 assert(str_out[5] == 1);
97 assert(str_out[6] == 1);
98 assert(str_out[7] == 5);
99 assert(str_out[8] == 0);
100 assert(str_out[9] == 1);
102 std::istringstream is(str_out, std::ios_base::binary);
103 std::ostringstream os2(std::ios_base::binary);
105 decompress(is, os2, 0);
106 std::string str_out2 = os2.str();
108 dstream<<"decompress: ";
109 for(u32 i=0; i<str_out2.size(); i++)
111 dstream<<(u32)str_out2[i]<<",";
115 assert(str_out2.size() == fromdata.getSize());
117 for(u32 i=0; i<str_out2.size(); i++)
119 assert(str_out2[i] == fromdata[i]);
126 SharedBuffer<u8> fromdata(4);
132 std::ostringstream os(std::ios_base::binary);
133 compress(fromdata, os, SER_FMT_VER_HIGHEST);
135 std::string str_out = os.str();
137 dstream<<"str_out.size()="<<str_out.size()<<std::endl;
138 dstream<<"TestCompress: 1,5,5,1 -> ";
139 for(u32 i=0; i<str_out.size(); i++)
141 dstream<<(u32)str_out[i]<<",";
145 /*assert(str_out.size() == 10);
147 assert(str_out[0] == 0);
148 assert(str_out[1] == 0);
149 assert(str_out[2] == 0);
150 assert(str_out[3] == 4);
151 assert(str_out[4] == 0);
152 assert(str_out[5] == 1);
153 assert(str_out[6] == 1);
154 assert(str_out[7] == 5);
155 assert(str_out[8] == 0);
156 assert(str_out[9] == 1);*/
158 std::istringstream is(str_out, std::ios_base::binary);
159 std::ostringstream os2(std::ios_base::binary);
161 decompress(is, os2, SER_FMT_VER_HIGHEST);
162 std::string str_out2 = os2.str();
164 dstream<<"decompress: ";
165 for(u32 i=0; i<str_out2.size(); i++)
167 dstream<<(u32)str_out2[i]<<",";
171 assert(str_out2.size() == fromdata.getSize());
173 for(u32 i=0; i<str_out2.size(); i++)
175 assert(str_out2[i] == fromdata[i]);
189 assert(n.d == CONTENT_AIR);
190 assert(n.getLight(LIGHTBANK_DAY) == 0);
191 assert(n.getLight(LIGHTBANK_NIGHT) == 0);
195 assert(n.light_propagates() == true);
197 assert(n.light_propagates() == false);
201 struct TestVoxelManipulator
209 VoxelArea a(v3s16(-1,-1,-1), v3s16(1,1,1));
210 assert(a.index(0,0,0) == 1*3*3 + 1*3 + 1);
211 assert(a.index(-1,-1,-1) == 0);
213 VoxelArea c(v3s16(-2,-2,-2), v3s16(2,2,2));
214 // An area that is 1 bigger in x+ and z-
215 VoxelArea d(v3s16(-2,-2,-3), v3s16(3,2,2));
217 core::list<VoxelArea> aa;
221 core::array<VoxelArea> results;
222 results.push_back(VoxelArea(v3s16(-2,-2,-3),v3s16(3,2,-3)));
223 results.push_back(VoxelArea(v3s16(3,-2,-2),v3s16(3,2,2)));
225 assert(aa.size() == results.size());
227 dstream<<"Result of diff:"<<std::endl;
228 for(core::list<VoxelArea>::Iterator
229 i = aa.begin(); i != aa.end(); i++)
234 s32 j = results.linear_search(*i);
248 dstream<<"*** Setting (-1,0,-1)=2 ***"<<std::endl;
250 v.setNodeNoRef(v3s16(-1,0,-1), MapNode(2));
254 assert(v.getNode(v3s16(-1,0,-1)).d == 2);
256 dstream<<"*** Reading from inexistent (0,0,-1) ***"<<std::endl;
258 EXCEPTION_CHECK(InvalidPositionException, v.getNode(v3s16(0,0,-1)));
262 dstream<<"*** Adding area ***"<<std::endl;
268 assert(v.getNode(v3s16(-1,0,-1)).d == 2);
269 EXCEPTION_CHECK(InvalidPositionException, v.getNode(v3s16(0,1,1)));
278 const char *content =
290 v3s16 size(12, 4, 2);
291 VoxelArea area(v3s16(0,0,0), size-v3s16(1,1,1));
293 const char *p = content;
294 for(s16 z=0; z<size.Z; z++)
295 for(s16 y=size.Y-1; y>=0; y--)
296 for(s16 x=0; x<size.X; x++)
299 //n.pressure = size.Y - y;
308 v.setNode(v3s16(x,y,z), n);
312 v.print(dstream, VOXELPRINT_WATERPRESSURE);
314 core::map<v3s16, u8> active_nodes;
315 v.updateAreaWaterPressure(area, active_nodes);
317 v.print(dstream, VOXELPRINT_WATERPRESSURE);
319 //s16 highest_y = -32768;
321 NOTE: These are commented out because this behaviour is changed
324 //assert(v.getWaterPressure(v3s16(7, 1, 1), highest_y, 0) == -1);
325 //assert(highest_y == 3);
326 /*assert(v.getWaterPressure(v3s16(7, 1, 1), highest_y, 0) == 3);
327 //assert(highest_y == 3);*/
329 active_nodes.clear();
330 active_nodes[v3s16(9,1,0)] = 1;
331 //v.flowWater(active_nodes, 0, true, 1000);
332 v.flowWater(active_nodes, 0, false, 1000);
334 dstream<<"Final result of flowWater:"<<std::endl;
335 v.print(dstream, VOXELPRINT_WATERPRESSURE);
344 class TC : public NodeContainer
350 core::list<v3s16> validity_exceptions;
354 position_valid = true;
357 virtual bool isValidPosition(v3s16 p)
359 //return position_valid ^ (p==position_valid_exception);
360 bool exception = false;
361 for(core::list<v3s16>::Iterator i=validity_exceptions.begin();
362 i != validity_exceptions.end(); i++)
370 return exception ? !position_valid : position_valid;
373 virtual MapNode getNode(v3s16 p)
375 if(isValidPosition(p) == false)
376 throw InvalidPositionException();
380 virtual void setNode(v3s16 p, MapNode & n)
382 if(isValidPosition(p) == false)
383 throw InvalidPositionException();
386 virtual u16 nodeContainerId() const
396 MapBlock b(&parent, v3s16(1,1,1));
397 v3s16 relpos(MAP_BLOCKSIZE, MAP_BLOCKSIZE, MAP_BLOCKSIZE);
399 assert(b.getPosRelative() == relpos);
401 assert(b.getBox().MinEdge.X == MAP_BLOCKSIZE);
402 assert(b.getBox().MaxEdge.X == MAP_BLOCKSIZE*2-1);
403 assert(b.getBox().MinEdge.Y == MAP_BLOCKSIZE);
404 assert(b.getBox().MaxEdge.Y == MAP_BLOCKSIZE*2-1);
405 assert(b.getBox().MinEdge.Z == MAP_BLOCKSIZE);
406 assert(b.getBox().MaxEdge.Z == MAP_BLOCKSIZE*2-1);
408 assert(b.isValidPosition(v3s16(0,0,0)) == true);
409 assert(b.isValidPosition(v3s16(-1,0,0)) == false);
410 assert(b.isValidPosition(v3s16(-1,-142,-2341)) == false);
411 assert(b.isValidPosition(v3s16(-124,142,2341)) == false);
412 assert(b.isValidPosition(v3s16(MAP_BLOCKSIZE-1,MAP_BLOCKSIZE-1,MAP_BLOCKSIZE-1)) == true);
413 assert(b.isValidPosition(v3s16(MAP_BLOCKSIZE-1,MAP_BLOCKSIZE,MAP_BLOCKSIZE-1)) == false);
416 TODO: this method should probably be removed
417 if the block size isn't going to be set variable
419 /*assert(b.getSizeNodes() == v3s16(MAP_BLOCKSIZE,
420 MAP_BLOCKSIZE, MAP_BLOCKSIZE));*/
422 // Changed flag should be initially set
423 assert(b.getChangedFlag() == true);
424 b.resetChangedFlag();
425 assert(b.getChangedFlag() == false);
427 // All nodes should have been set to
428 // .d=CONTENT_AIR and .getLight() = 0
429 for(u16 z=0; z<MAP_BLOCKSIZE; z++)
430 for(u16 y=0; y<MAP_BLOCKSIZE; y++)
431 for(u16 x=0; x<MAP_BLOCKSIZE; x++)
433 assert(b.getNode(v3s16(x,y,z)).d == CONTENT_AIR);
434 assert(b.getNode(v3s16(x,y,z)).getLight(LIGHTBANK_DAY) == 0);
435 assert(b.getNode(v3s16(x,y,z)).getLight(LIGHTBANK_NIGHT) == 0);
439 Parent fetch functions
441 parent.position_valid = false;
446 // Positions in the block should still be valid
447 assert(b.isValidPositionParent(v3s16(0,0,0)) == true);
448 assert(b.isValidPositionParent(v3s16(MAP_BLOCKSIZE-1,MAP_BLOCKSIZE-1,MAP_BLOCKSIZE-1)) == true);
449 n = b.getNodeParent(v3s16(0,MAP_BLOCKSIZE-1,0));
450 assert(n.d == CONTENT_AIR);
452 // ...but outside the block they should be invalid
453 assert(b.isValidPositionParent(v3s16(-121,2341,0)) == false);
454 assert(b.isValidPositionParent(v3s16(-1,0,0)) == false);
455 assert(b.isValidPositionParent(v3s16(MAP_BLOCKSIZE-1,MAP_BLOCKSIZE-1,MAP_BLOCKSIZE)) == false);
458 bool exception_thrown = false;
460 // This should throw an exception
461 MapNode n = b.getNodeParent(v3s16(0,0,-1));
463 catch(InvalidPositionException &e)
465 exception_thrown = true;
467 assert(exception_thrown);
470 parent.position_valid = true;
471 // Now the positions outside should be valid
472 assert(b.isValidPositionParent(v3s16(-121,2341,0)) == true);
473 assert(b.isValidPositionParent(v3s16(-1,0,0)) == true);
474 assert(b.isValidPositionParent(v3s16(MAP_BLOCKSIZE-1,MAP_BLOCKSIZE-1,MAP_BLOCKSIZE)) == true);
475 n = b.getNodeParent(v3s16(0,0,MAP_BLOCKSIZE));
484 assert(b.getNode(p).d == 4);
485 //TODO: Update to new system
486 /*assert(b.getNodeTile(p) == 4);
487 assert(b.getNodeTile(v3s16(-1,-1,0)) == 5);*/
492 // Set lighting of all nodes to 0
493 for(u16 z=0; z<MAP_BLOCKSIZE; z++){
494 for(u16 y=0; y<MAP_BLOCKSIZE; y++){
495 for(u16 x=0; x<MAP_BLOCKSIZE; x++){
496 MapNode n = b.getNode(v3s16(x,y,z));
497 n.setLight(LIGHTBANK_DAY, 0);
498 n.setLight(LIGHTBANK_NIGHT, 0);
499 b.setNode(v3s16(x,y,z), n);
505 Check how the block handles being a lonely sky block
507 parent.position_valid = true;
508 b.setIsUnderground(false);
509 parent.node.d = CONTENT_AIR;
510 parent.node.setLight(LIGHTBANK_DAY, LIGHT_SUN);
511 parent.node.setLight(LIGHTBANK_NIGHT, 0);
512 core::map<v3s16, bool> light_sources;
513 // The bottom block is invalid, because we have a shadowing node
514 assert(b.propagateSunlight(light_sources) == false);
515 assert(b.getNode(v3s16(1,4,0)).getLight(LIGHTBANK_DAY) == LIGHT_SUN);
516 assert(b.getNode(v3s16(1,3,0)).getLight(LIGHTBANK_DAY) == LIGHT_SUN);
517 assert(b.getNode(v3s16(1,2,0)).getLight(LIGHTBANK_DAY) == 0);
518 assert(b.getNode(v3s16(1,1,0)).getLight(LIGHTBANK_DAY) == 0);
519 assert(b.getNode(v3s16(1,0,0)).getLight(LIGHTBANK_DAY) == 0);
520 assert(b.getNode(v3s16(1,2,3)).getLight(LIGHTBANK_DAY) == LIGHT_SUN);
521 assert(b.getFaceLight(1000, p, v3s16(0,1,0)) == LIGHT_SUN);
522 assert(b.getFaceLight(1000, p, v3s16(0,-1,0)) == 0);
523 assert(b.getFaceLight(0, p, v3s16(0,-1,0)) == 0);
524 // According to MapBlock::getFaceLight,
525 // The face on the z+ side should have double-diminished light
526 //assert(b.getFaceLight(p, v3s16(0,0,1)) == diminish_light(diminish_light(LIGHT_MAX)));
527 // The face on the z+ side should have diminished light
528 assert(b.getFaceLight(1000, p, v3s16(0,0,1)) == diminish_light(LIGHT_MAX));
531 Check how the block handles being in between blocks with some non-sunlight
532 while being underground
535 // Make neighbours to exist and set some non-sunlight to them
536 parent.position_valid = true;
537 b.setIsUnderground(true);
538 parent.node.setLight(LIGHTBANK_DAY, LIGHT_MAX/2);
539 core::map<v3s16, bool> light_sources;
540 // The block below should be valid because there shouldn't be
541 // sunlight in there either
542 assert(b.propagateSunlight(light_sources, true) == true);
543 // Should not touch nodes that are not affected (that is, all of them)
544 //assert(b.getNode(v3s16(1,2,3)).getLight() == LIGHT_SUN);
545 // Should set light of non-sunlighted blocks to 0.
546 assert(b.getNode(v3s16(1,2,3)).getLight(LIGHTBANK_DAY) == 0);
549 Set up a situation where:
550 - There is only air in this block
551 - There is a valid non-sunlighted block at the bottom, and
552 - Invalid blocks elsewhere.
553 - the block is not underground.
555 This should result in bottom block invalidity
558 b.setIsUnderground(false);
560 for(u16 z=0; z<MAP_BLOCKSIZE; z++){
561 for(u16 y=0; y<MAP_BLOCKSIZE; y++){
562 for(u16 x=0; x<MAP_BLOCKSIZE; x++){
565 n.setLight(LIGHTBANK_DAY, 0);
566 b.setNode(v3s16(x,y,z), n);
570 // Make neighbours invalid
571 parent.position_valid = false;
572 // Add exceptions to the top of the bottom block
573 for(u16 x=0; x<MAP_BLOCKSIZE; x++)
574 for(u16 z=0; z<MAP_BLOCKSIZE; z++)
576 parent.validity_exceptions.push_back(v3s16(MAP_BLOCKSIZE+x, MAP_BLOCKSIZE-1, MAP_BLOCKSIZE+z));
578 // Lighting value for the valid nodes
579 parent.node.setLight(LIGHTBANK_DAY, LIGHT_MAX/2);
580 core::map<v3s16, bool> light_sources;
581 // Bottom block is not valid
582 assert(b.propagateSunlight(light_sources) == false);
589 class TC : public NodeContainer
598 position_valid = true;
601 virtual bool isValidPosition(v3s16 p)
603 return position_valid;
606 virtual MapNode getNode(v3s16 p)
608 if(position_valid == false)
609 throw InvalidPositionException();
613 virtual void setNode(v3s16 p, MapNode & n)
615 if(position_valid == false)
616 throw InvalidPositionException();
619 virtual u16 nodeContainerId() const
628 parent.position_valid = false;
630 // Create one with no heightmaps
631 ServerMapSector sector(&parent, v2s16(1,1), 0);
632 //ConstantGenerator *dummyheightmap = new ConstantGenerator();
633 //sector->setHeightmap(dummyheightmap);
635 EXCEPTION_CHECK(InvalidPositionException, sector.getBlockNoCreate(0));
636 EXCEPTION_CHECK(InvalidPositionException, sector.getBlockNoCreate(1));
638 MapBlock * bref = sector.createBlankBlock(-2);
640 EXCEPTION_CHECK(InvalidPositionException, sector.getBlockNoCreate(0));
641 assert(sector.getBlockNoCreate(-2) == bref);
643 //TODO: Check for AlreadyExistsException
645 /*bool exception_thrown = false;
649 catch(InvalidPositionException &e){
650 exception_thrown = true;
652 assert(exception_thrown);*/
659 void TestSingleFixed()
662 OneChildHeightmap hm1(BS1);
664 // Test that it is filled with < GROUNDHEIGHT_VALID_MINVALUE
665 for(s16 y=0; y<=BS1; y++){
666 for(s16 x=0; x<=BS1; x++){
668 assert(hm1.m_child.getGroundHeight(p)
669 < GROUNDHEIGHT_VALID_MINVALUE);
673 hm1.m_child.setGroundHeight(v2s16(1,0), 2.0);
674 //hm1.m_child.print();
675 assert(fabs(hm1.getGroundHeight(v2s16(1,0))-2.0)<0.001);
676 hm1.setGroundHeight(v2s16(0,1), 3.0);
677 assert(fabs(hm1.m_child.getGroundHeight(v2s16(0,1))-3.0)<0.001);
680 for(s16 y=0; y<=BS1; y++){
681 for(s16 x=0; x<=BS1; x++){
683 hm1.m_child.setGroundHeight(p, -1.0);
687 f32 corners[] = {0.0, 0.0, 1.0, 1.0};
688 hm1.m_child.generateContinued(0.0, 0.0, corners);
691 assert(fabs(hm1.m_child.getGroundHeight(v2s16(1,0))-0.2)<0.05);
692 assert(fabs(hm1.m_child.getGroundHeight(v2s16(4,3))-0.7)<0.05);
693 assert(fabs(hm1.m_child.getGroundHeight(v2s16(4,4))-1.0)<0.05);
698 //g_heightmap_debugprint = true;
700 /*UnlimitedHeightmap hm1(BS1,
701 new ConstantGenerator(0.0),
702 new ConstantGenerator(0.0),
703 new ConstantGenerator(5.0));*/
704 PointAttributeDatabase padb;
705 UnlimitedHeightmap hm1(BS1, &padb);
706 // Go through it so it generates itself
707 for(s16 y=0; y<=BS1; y++){
708 for(s16 x=0; x<=BS1; x++){
710 hm1.getGroundHeight(p);
714 dstream<<"UnlimitedHeightmap hm1:"<<std::endl;
717 dstream<<"testing UnlimitedHeightmap set/get"<<std::endl;
720 // Get first heightmap and try setGroundHeight
721 FixedHeightmap * href = hm1.getHeightmap(v2s16(0,0));
722 href->setGroundHeight(p1, v1);
723 // Read from UnlimitedHeightmap
724 assert(fabs(hm1.getGroundHeight(p1)-v1)<0.001);
729 dstream<<"Running random code (get a human to check this)"<<std::endl;
730 dstream<<"myrand() values: ";
731 for(u16 i=0; i<5; i++)
732 dstream<<(u16)myrand()<<" ";
736 /*UnlimitedHeightmap hm1(BS1,
737 new ConstantGenerator(10.0),
738 new ConstantGenerator(0.3),
739 new ConstantGenerator(0.0));*/
741 PointAttributeDatabase padb;
743 padb.getList("hm_baseheight")->addPoint(v2s16(-BS1,0), Attribute(0));
744 padb.getList("hm_randmax")->addPoint(v2s16(-BS1,0), Attribute(0));
745 padb.getList("hm_randfactor")->addPoint(v2s16(-BS1,0), Attribute(0.0));
747 padb.getList("hm_baseheight")->addPoint(v2s16(0,0), Attribute(-20));
748 padb.getList("hm_randmax")->addPoint(v2s16(0,0), Attribute(0));
749 padb.getList("hm_randfactor")->addPoint(v2s16(0,0), Attribute(0.5));
751 padb.getList("hm_baseheight")->addPoint(v2s16(BS1*2,BS1), Attribute(0));
752 padb.getList("hm_randmax")->addPoint(v2s16(BS1*2,BS1), Attribute(30));
753 padb.getList("hm_randfactor")->addPoint(v2s16(BS1*2,BS1), Attribute(0.63));
755 UnlimitedHeightmap hm1(BS1, &padb);
757 // Force hm1 to generate a some heightmap
758 hm1.getGroundHeight(v2s16(0,0));
759 hm1.getGroundHeight(v2s16(0,BS1));
760 /*hm1.getGroundHeight(v2s16(BS1,-1));
761 hm1.getGroundHeight(v2s16(BS1-1,-1));*/
764 // Get the (0,0) and (1,0) heightmaps
765 /*FixedHeightmap * hr00 = hm1.getHeightmap(v2s16(0,0));
766 FixedHeightmap * hr01 = hm1.getHeightmap(v2s16(1,0));
767 f32 corners[] = {1.0, 1.0, 1.0, 1.0};
768 hr00->generateContinued(0.0, 0.0, corners);
776 //srand(7); // Get constant random
777 srand(time(0)); // Get better random
789 const int port = 30003;
793 const char sendbuffer[] = "hello world!";
794 socket.Send(Address(127,0,0,1,port), sendbuffer, sizeof(sendbuffer));
799 memset(rcvbuffer, 0, sizeof(rcvbuffer));
803 int bytes_read = socket.Receive(sender, rcvbuffer, sizeof(rcvbuffer));
807 //FIXME: This fails on some systems
808 assert(strncmp(sendbuffer, rcvbuffer, sizeof(sendbuffer))==0);
809 assert(sender.getAddress() == Address(127,0,0,1, 0).getAddress());
813 struct TestConnection
818 Test helper functions
821 // Some constants for testing
822 u32 proto_id = 0x12345678;
825 SharedBuffer<u8> data1(1);
827 Address a(127,0,0,1, 10);
830 con::BufferedPacket p1 = con::makePacket(a, data1,
831 proto_id, peer_id, channel);
833 We should now have a packet with this data:
836 [4] u16 sender_peer_id
841 assert(readU32(&p1.data[0]) == proto_id);
842 assert(readU16(&p1.data[4]) == peer_id);
843 assert(readU8(&p1.data[6]) == channel);
844 assert(readU8(&p1.data[7]) == data1[0]);
846 //dstream<<"initial data1[0]="<<((u32)data1[0]&0xff)<<std::endl;
848 SharedBuffer<u8> p2 = con::makeReliablePacket(data1, seqnum);
850 /*dstream<<"p2.getSize()="<<p2.getSize()<<", data1.getSize()="
851 <<data1.getSize()<<std::endl;
852 dstream<<"readU8(&p2[3])="<<readU8(&p2[3])
853 <<" p2[3]="<<((u32)p2[3]&0xff)<<std::endl;
854 dstream<<"data1[0]="<<((u32)data1[0]&0xff)<<std::endl;*/
856 assert(p2.getSize() == 3 + data1.getSize());
857 assert(readU8(&p2[0]) == TYPE_RELIABLE);
858 assert(readU16(&p2[1]) == seqnum);
859 assert(readU8(&p2[3]) == data1[0]);
862 struct Handler : public con::PeerHandler
864 Handler(const char *a_name)
870 void peerAdded(con::Peer *peer)
872 dstream<<"Handler("<<name<<")::peerAdded(): "
873 "id="<<peer->id<<std::endl;
877 void deletingPeer(con::Peer *peer, bool timeout)
879 dstream<<"Handler("<<name<<")::deletingPeer(): "
881 <<", timeout="<<timeout<<std::endl;
893 DSTACK("TestConnection::Run");
898 Test some real connections
900 u32 proto_id = 0xad26846a;
902 Handler hand_server("server");
903 Handler hand_client("client");
905 dstream<<"** Creating server Connection"<<std::endl;
906 con::Connection server(proto_id, 512, 5.0, &hand_server);
909 dstream<<"** Creating client Connection"<<std::endl;
910 con::Connection client(proto_id, 512, 5.0, &hand_client);
912 assert(hand_server.count == 0);
913 assert(hand_client.count == 0);
917 Address server_address(127,0,0,1, 30001);
918 dstream<<"** running client.Connect()"<<std::endl;
919 client.Connect(server_address);
923 // Client should have added server now
924 assert(hand_client.count == 1);
925 assert(hand_client.last_id == 1);
926 // But server should not have added client
927 assert(hand_server.count == 0);
933 dstream<<"** running server.Receive()"<<std::endl;
934 u32 size = server.Receive(peer_id, data, 100);
935 dstream<<"** Server received: peer_id="<<peer_id
939 catch(con::NoIncomingDataException &e)
941 // No actual data received, but the client has
942 // probably been connected
945 // Client should be the same
946 assert(hand_client.count == 1);
947 assert(hand_client.last_id == 1);
948 // Server should have the client
949 assert(hand_server.count == 1);
950 assert(hand_server.last_id == 2);
954 while(client.Connected() == false)
960 dstream<<"** running client.Receive()"<<std::endl;
961 u32 size = client.Receive(peer_id, data, 100);
962 dstream<<"** Client received: peer_id="<<peer_id
966 catch(con::NoIncomingDataException &e)
978 dstream<<"** running server.Receive()"<<std::endl;
979 u32 size = server.Receive(peer_id, data, 100);
980 dstream<<"** Server received: peer_id="<<peer_id
984 catch(con::NoIncomingDataException &e)
989 /*u8 data[] = "Hello World!";
990 u32 datasize = sizeof(data);*/
991 SharedBuffer<u8> data = SharedBufferFromString("Hello World!");
993 dstream<<"** running client.Send()"<<std::endl;
994 client.Send(PEER_ID_SERVER, 0, data, true);
1000 dstream<<"** running server.Receive()"<<std::endl;
1001 u32 size = server.Receive(peer_id, recvdata, 100);
1002 dstream<<"** Server received: peer_id="<<peer_id
1006 assert(memcmp(*data, recvdata, data.getSize()) == 0);
1009 u16 peer_id_client = 2;
1013 Send consequent packets in different order
1015 //u8 data1[] = "hello1";
1016 //u8 data2[] = "hello2";
1017 SharedBuffer<u8> data1 = SharedBufferFromString("hello1");
1018 SharedBuffer<u8> data2 = SharedBufferFromString("Hello2");
1020 Address client_address =
1021 server.GetPeer(peer_id_client)->address;
1023 dstream<<"*** Sending packets in wrong order (2,1,2)"
1027 con::Channel *ch = &server.GetPeer(peer_id_client)->channels[chn];
1028 u16 sn = ch->next_outgoing_seqnum;
1029 ch->next_outgoing_seqnum = sn+1;
1030 server.Send(peer_id_client, chn, data2, true);
1031 ch->next_outgoing_seqnum = sn;
1032 server.Send(peer_id_client, chn, data1, true);
1033 ch->next_outgoing_seqnum = sn+1;
1034 server.Send(peer_id_client, chn, data2, true);
1038 dstream<<"*** Receiving the packets"<<std::endl;
1044 dstream<<"** running client.Receive()"<<std::endl;
1046 size = client.Receive(peer_id, recvdata, 20);
1047 dstream<<"** Client received: peer_id="<<peer_id
1049 <<", data="<<recvdata
1051 assert(size == data1.getSize());
1052 assert(memcmp(*data1, recvdata, data1.getSize()) == 0);
1053 assert(peer_id == PEER_ID_SERVER);
1055 dstream<<"** running client.Receive()"<<std::endl;
1057 size = client.Receive(peer_id, recvdata, 20);
1058 dstream<<"** Client received: peer_id="<<peer_id
1060 <<", data="<<recvdata
1062 assert(size == data2.getSize());
1063 assert(memcmp(*data2, recvdata, data2.getSize()) == 0);
1064 assert(peer_id == PEER_ID_SERVER);
1066 bool got_exception = false;
1069 dstream<<"** running client.Receive()"<<std::endl;
1071 size = client.Receive(peer_id, recvdata, 20);
1072 dstream<<"** Client received: peer_id="<<peer_id
1074 <<", data="<<recvdata
1077 catch(con::NoIncomingDataException &e)
1079 dstream<<"** No incoming data for client"<<std::endl;
1080 got_exception = true;
1082 assert(got_exception);
1086 SharedBuffer<u8> data1(1100);
1087 for(u16 i=0; i<1100; i++){
1091 dstream<<"Sending data (size="<<1100<<"):";
1092 for(int i=0; i<1100 && i<20; i++){
1093 if(i%2==0) DEBUGPRINT(" ");
1094 DEBUGPRINT("%.2X", ((int)((const char*)*data1)[i])&0xff);
1100 server.Send(peer_id_client, 0, data1, true);
1105 dstream<<"** running client.Receive()"<<std::endl;
1107 u16 size = client.Receive(peer_id, recvdata, 2000);
1108 dstream<<"** Client received: peer_id="<<peer_id
1112 dstream<<"Received data (size="<<size<<"):";
1113 for(int i=0; i<size && i<20; i++){
1114 if(i%2==0) DEBUGPRINT(" ");
1115 DEBUGPRINT("%.2X", ((int)((const char*)recvdata)[i])&0xff);
1121 assert(memcmp(*data1, recvdata, data1.getSize()) == 0);
1122 assert(peer_id == PEER_ID_SERVER);
1125 // Check peer handlers
1126 assert(hand_client.count == 1);
1127 assert(hand_client.last_id == 1);
1128 assert(hand_server.count == 1);
1129 assert(hand_server.last_id == 2);
1138 dstream<<"Running " #X <<std::endl;\
1144 DSTACK(__FUNCTION_NAME);
1145 dstream<<"run_tests() started"<<std::endl;
1146 TEST(TestUtilities);
1149 TEST(TestVoxelManipulator);
1151 TEST(TestMapSector);
1152 TEST(TestHeightmap);
1153 if(INTERNET_SIMULATOR == false){
1155 dout_con<<"=== BEGIN RUNNING UNIT TESTS FOR CONNECTION ==="<<std::endl;
1156 TEST(TestConnection);
1157 dout_con<<"=== END RUNNING UNIT TESTS FOR CONNECTION ==="<<std::endl;
1159 dstream<<"run_tests() passed"<<std::endl;