aboutsummaryrefslogtreecommitdiff
path: root/src/clientobject.cpp
blob: 37f693c5e1310d0b4306fa9ff720b684db1e9adf (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
/*
Minetest
Copyright (C) 2010-2013 celeron55, Perttu Ahola <celeron55@gmail.com>

This program is free software; you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation; either version 2.1 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public License along
with this program; if not, write to the Free Software Foundation, Inc.,
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/

#include "clientobject.h"
#include "debug.h"
#include "porting.h"
#include "constants.h"

/*
	ClientActiveObject
*/

ClientActiveObject::ClientActiveObject(u16 id, IGameDef *gamedef,
		ClientEnvironment *env):
	ActiveObject(id),
	m_gamedef(gamedef),
	m_env(env)
{
}

ClientActiveObject::~ClientActiveObject()
{
	removeFromScene(true);
}

ClientActiveObject* ClientActiveObject::create(u8 type, IGameDef *gamedef,
		ClientEnvironment *env)
{
	// Find factory function
	std::map<u16, Factory>::iterator n;
	n = m_types.find(type);
	if(n == m_types.end())
	{
		// If factory is not found, just return.
		dstream<<"WARNING: ClientActiveObject: No factory for type="
				<<(int)type<<std::endl;
		return NULL;
	}

	Factory f = n->second;
	ClientActiveObject *object = (*f)(gamedef, env);
	return object;
}

void ClientActiveObject::registerType(u16 type, Factory f)
{
	std::map<u16, Factory>::iterator n;
	n = m_types.find(type);
	if(n != m_types.end())
		return;
	m_types[type] = f;
}


244'>244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895
--trainlogic.lua
--controls train entities stuff about connecting/disconnecting/colliding trains and other things

--local print=function(t, ...) minetest.log("action", table.concat({t, ...}, " ")) minetest.chat_send_all(table.concat({t, ...}, " ")) end
local print=function() end

local benchmark=false
--printbm=function(str, t) print("[advtrains]"..str.." "..((os.clock()-t)*1000).."ms") end
local bm={}
local bmlt=0
local bmsteps=0
local bmstepint=200
printbm=function(action, ta)
	if not benchmark then return end
	local t=(os.clock()-ta)*1000
	if not bm[action] then
		bm[action]=t
	else
		bm[action]=bm[action]+t
	end
	bmlt=bmlt+t
end
function endstep()
	if not benchmark then return end
	bmsteps=bmsteps-1
	if bmsteps<=0 then
		bmsteps=bmstepint
		for key, value in pairs(bm) do
			minetest.chat_send_all(key.." "..(value/bmstepint).." ms avg.")
		end
		minetest.chat_send_all("Total time consumed by all advtrains actions per step: "..(bmlt/bmstepint).." ms avg.")
		bm={}
		bmlt=0
	end
end

--TODO: these values need to be integrated when i remove traintypes.
advtrains.train_accel_force=2--per second and divided by number of wagons
advtrains.train_brake_force=3--per second, not divided by number of wagons
advtrains.train_roll_force=0.5--per second, not divided by number of wagons, acceleration when rolling without brake
advtrains.train_emerg_force=10--for emergency brakes(when going off track)

advtrains.audit_interval=30

advtrains.all_traintypes={}
function advtrains.register_train_type(name, drives_on, max_speed)
	advtrains.all_traintypes[name]={}
	advtrains.all_traintypes[name].drives_on=drives_on
	advtrains.all_traintypes[name].max_speed=max_speed or 10
end


advtrains.trains={}
advtrains.wagon_save={}

--load initially
advtrains.fpath=minetest.get_worldpath().."/advtrains"
local file, err = io.open(advtrains.fpath, "r")
if not file then
	local er=err or "Unknown Error"
	print("[advtrains]Failed loading advtrains save file "..er)
else
	local tbl = minetest.deserialize(file:read("*a"))
	if type(tbl) == "table" then
		advtrains.trains=tbl
	end
	file:close()
end
advtrains.fpath_ws=minetest.get_worldpath().."/advtrains_wagon_save"
local file, err = io.open(advtrains.fpath_ws, "r")
if not file then
	local er=err or "Unknown Error"
	print("[advtrains]Failed loading advtrains save file "..er)
else
	local tbl = minetest.deserialize(file:read("*a"))
	if type(tbl) == "table" then
		advtrains.wagon_save=tbl
	end
	file:close()
end


advtrains.save = function()
	--print("[advtrains]saving")
	advtrains.invalidate_all_paths()
	local datastr = minetest.serialize(advtrains.trains)
	if not datastr then
		minetest.log("error", "[advtrains] Failed to serialize train data!")
		return
	end
	local file, err = io.open(advtrains.fpath, "w")
	if err then
		return err
	end
	file:write(datastr)
	file:close()
	
	-- update wagon saves
	for _,wagon in pairs(minetest.luaentities) do
		if wagon.is_wagon and wagon.initialized then
			wagon:get_staticdata()
		end
	end
	--cross out userdata
	for w_id, data in pairs(advtrains.wagon_save) do
		data.name=nil
		data.object=nil
		if data.driver then
			data.driver_name=data.driver:get_player_name()
			data.driver=nil
		else
			data.driver_name=nil
		end
		if data.discouple then
			data.discouple.object:remove()
			data.discouple=nil
		end
	end
	--print(dump(advtrains.wagon_save))
	datastr = minetest.serialize(advtrains.wagon_save)
	if not datastr then
		minetest.log("error", "[advtrains] Failed to serialize train data!")
		return
	end
	file, err = io.open(advtrains.fpath_ws, "w")
	if err then
		return err
	end
	file:write(datastr)
	file:close()
	
	advtrains.save_trackdb()
end
minetest.register_on_shutdown(advtrains.save)

advtrains.save_and_audit_timer=advtrains.audit_interval
minetest.register_globalstep(function(dtime)
	advtrains.save_and_audit_timer=advtrains.save_and_audit_timer-dtime
	if advtrains.save_and_audit_timer<=0 then
		local t=os.clock()
		--print("[advtrains] audit step")
		--clean up orphaned trains
		for k,v in pairs(advtrains.trains) do
			--advtrains.update_trainpart_properties(k)
			if #v.trainparts==0 then
				print("[advtrains][train "..k.."] has empty trainparts, removing.")
				advtrains.trains[k]=nil
			end
		end
		--save
		advtrains.save()
		advtrains.save_and_audit_timer=advtrains.audit_interval
		printbm("saving", t)
	end
	--regular train step
	local t=os.clock()
	for k,v in pairs(advtrains.trains) do
		advtrains.train_step(k, v, dtime)
	end
	
	--see tracks.lua
	if advtrains.detector.clean_step_before then
		advtrains.detector.finalize_restore()
	end
	
	printbm("trainsteps", t)
	endstep()
end)

function advtrains.train_step(id, train, dtime)
	
	--TODO check for all vars to be present
	if not train.velocity then
		train.velocity=0
	end
	if not train.movedir or (train.movedir~=1 and train.movedir~=-1) then
		train.movedir=1
	end
	--very unimportant thing: check if couple is here
	if train.couple_eid_front and (not minetest.luaentities[train.couple_eid_front] or not minetest.luaentities[train.couple_eid_front].is_couple) then train.couple_eid_front=nil end
	if train.couple_eid_back and (not minetest.luaentities[train.couple_eid_back] or not minetest.luaentities[train.couple_eid_back].is_couple) then train.couple_eid_back=nil end
	
	--skip certain things (esp. collision) when not moving
	local train_moves=(train.velocity~=0)
	
	--if not train.last_pos then advtrains.trains[id]=nil return end
	
	if not advtrains.pathpredict(id, train) then 
		print("pathpredict failed(returned false)")
		train.velocity=0
		train.tarvelocity=0
		return
	end
	
	local path=advtrains.get_or_create_path(id, train)
	if not path then
		train.velocity=0
		train.tarvelocity=0
		print("train has no path for whatever reason")
		return 
	end
	
	local train_end_index=advtrains.get_train_end_index(train)
	--apply off-track handling:
	local front_off_track=train.max_index_on_track and train.index>train.max_index_on_track
	local back_off_track=train.min_index_on_track and train_end_index<train.min_index_on_track
	if front_off_track and back_off_track then--allow movement in both directions
		if train.tarvelocity>1 then train.tarvelocity=1 end
	elseif front_off_track then--allow movement only backward
		if train.movedir==1 and train.tarvelocity>0 then train.tarvelocity=0 end
		if train.movedir==-1 and train.tarvelocity>1 then train.tarvelocity=1 end
	elseif back_off_track then--allow movement only forward
		if train.movedir==-1 and train.tarvelocity>0 then train.tarvelocity=0 end
		if train.movedir==1 and train.tarvelocity>1 then train.tarvelocity=1 end
	end
	
	--update advtrains.detector
	if not train.detector_old_index then
		train.detector_old_index = math.floor(train_end_index)
		train.detector_old_end_index = math.floor(train_end_index)
	end
	local ifo, ifn, ibo, ibn = train.detector_old_index, math.floor(train.index), train.detector_old_end_index, math.floor(train_end_index)
	if ifn>ifo then
		for i=ifo, ifn do
			if path[i] then
				advtrains.detector.enter_node(path[i], id)
			end
		end
	elseif ifn<ifo then
		for i=ifn, ifo do
			if path[i] then
				advtrains.detector.leave_node(path[i], id)
			end
		end
	end
	if ibn<ibo then
		for i=ibn, ibn do
			if path[i] then
				advtrains.detector.enter_node(path[i], id)
			end
		end
	elseif ibn>ibo then
		for i=ibo, ibn do
			if path[i] then
				advtrains.detector.leave_node(path[i], id)
			end
		end
	end
	train.detector_old_index = math.floor(train.index)
	train.detector_old_end_index = math.floor(train_end_index)
	
	if train_moves then
		--check for collisions by finding objects
		--front
		local search_radius=4
		
		--coupling
		local couple_outward=1
		local posfront=advtrains.get_real_index_position(path, train.index+couple_outward)
		local posback=advtrains.get_real_index_position(path, train_end_index-couple_outward)
		for _,pos in ipairs({posfront, posback}) do
			if pos then
				local objrefs=minetest.get_objects_inside_radius(pos, search_radius)
				for _,v in pairs(objrefs) do
					local le=v:get_luaentity()
					if le and le.is_wagon and le.initialized and le.train_id~=id then
						advtrains.try_connect_trains(id, le.train_id)
					end
				end
			end
		end
		--heh, new collision again.
		--this time, based on NODES and the advtrains.detector.on_node table.
		local collpos
		local coll_grace=1
		if train.movedir==1 then
			collpos=advtrains.get_real_index_position(path, train.index-coll_grace)
		else
			collpos=advtrains.get_real_index_position(path, train_end_index+coll_grace)
		end
		if collpos then
			local rcollpos=advtrains.round_vector_floor_y(collpos)
			for x=-1,1 do
				for z=-1,1 do
					local testpts=minetest.pos_to_string(vector.add(rcollpos, {x=x, y=0, z=z}))
					if advtrains.detector.on_node[testpts] and advtrains.detector.on_node[testpts]~=id then
						--collides
						train.recently_collided_with_env=true
						train.velocity=0.5*train.velocity
						train.movedir=train.movedir*-1
						train.tarvelocity=0
					end
				end
			end
		end
	end
	--check for any trainpart entities if they have been unloaded. do this only if train is near a player, to not spawn entities into unloaded areas
	train.check_trainpartload=(train.check_trainpartload or 0)-dtime
	local node_range=(math.max((minetest.setting_get("active_block_range") or 0),1)*16)
	if train.check_trainpartload<=0 then
		local ori_pos=advtrains.get_real_index_position(path, train.index) --not much to calculate
		print("[advtrains][train "..id.."] at "..minetest.pos_to_string(vector.round(ori_pos)))
		
		local should_check=false
		for _,p in ipairs(minetest.get_connected_players()) do
			should_check=should_check or ((vector.distance(ori_pos, p:getpos())<node_range))
		end
		if should_check then
			--it is better to iterate luaentites only once
			--print("check_trainpartload")
			local found_uids={}
			for _,wagon in pairs(minetest.luaentities) do
				if wagon.is_wagon and wagon.initialized and wagon.train_id==id then
					if found_uids[wagon.unique_id] then
						--duplicate found, delete it
						if wagon.object then wagon.object:remove() end
					else
						found_uids[wagon.unique_id]=true
					end
				end
			end
			--print("found_uids: "..dump(found_uids))
			--now iterate trainparts and check. then cross them out to see if there are wagons over for any reason
			for pit, w_id in ipairs(train.trainparts) do
				if found_uids[w_id] then
					--print(w_id.." still loaded")
				elseif advtrains.wagon_save[w_id] then
					--print(w_id.." not loaded, but save available")
					--spawn a new and initialize it with the properties from wagon_save
					local le=minetest.env:add_entity(ori_pos, advtrains.wagon_save[w_id].entity_name):get_luaentity()
					le:init_from_wagon_save(w_id)
				else
					print(w_id.." not loaded and no save available")
					--what the hell...
					table.remove(train.trainparts, pit)
				end
			end
		end
		train.check_trainpartload=2
	end
	
	
	--handle collided_with_env
	if train.recently_collided_with_env then
		train.tarvelocity=0
		if not train_moves then
			train.recently_collided_with_env=false--reset status when stopped
		end
	end
	if train.locomotives_in_train==0 then
		train.tarvelocity=0
	end="hl slc">//dstream<<"END SharedBuffer<u8> makeReliablePacket()"<<std::endl;
	return b;
}

/*
	ReliablePacketBuffer
*/

void ReliablePacketBuffer::print()
{
	core::list<BufferedPacket>::Iterator i;
	i = m_list.begin();
	for(; i != m_list.end(); i++)
	{
		u16 s = readU16(&(i->data[BASE_HEADER_SIZE+1]));
		dout_con<<s<<" ";
	}
}
bool ReliablePacketBuffer::empty()
{
	return m_list.empty();
}
u32 ReliablePacketBuffer::size()
{
	return m_list.getSize();
}
RPBSearchResult ReliablePacketBuffer::findPacket(u16 seqnum)
{
	core::list<BufferedPacket>::Iterator i;
	i = m_list.begin();
	for(; i != m_list.end(); i++)
	{
		u16 s = readU16(&(i->data[BASE_HEADER_SIZE+1]));
		/*dout_con<<"findPacket(): finding seqnum="<<seqnum
				<<", comparing to s="<<s<<std::endl;*/
		if(s == seqnum)
			break;
	}
	return i;
}
RPBSearchResult ReliablePacketBuffer::notFound()
{
	return m_list.end();
}
u16 ReliablePacketBuffer::getFirstSeqnum()
{
	if(empty())
		throw NotFoundException("Buffer is empty");
	BufferedPacket p = *m_list.begin();
	return readU16(&p.data[BASE_HEADER_SIZE+1]);
}
BufferedPacket ReliablePacketBuffer::popFirst()
{
	if(empty())
		throw NotFoundException("Buffer is empty");
	BufferedPacket p = *m_list.begin();
	core::list<BufferedPacket>::Iterator i = m_list.begin();
	m_list.erase(i);
	return p;
}
BufferedPacket ReliablePacketBuffer::popSeqnum(u16 seqnum)
{
	RPBSearchResult r = findPacket(seqnum);
	if(r == notFound()){
		dout_con<<"Not found"<<std::endl;
		throw NotFoundException("seqnum not found in buffer");
	}
	BufferedPacket p = *r;
	m_list.erase(r);
	return p;
}
void ReliablePacketBuffer::insert(BufferedPacket &p)
{
	assert(p.data.getSize() >= BASE_HEADER_SIZE+3);
	u8 type = readU8(&p.data[BASE_HEADER_SIZE+0]);
	assert(type == TYPE_RELIABLE);
	u16 seqnum = readU16(&p.data[BASE_HEADER_SIZE+1]);

	// Find the right place for the packet and insert it there

	// If list is empty, just add it
	if(m_list.empty())
	{
		m_list.push_back(p);
		// Done.
		return;
	}
	// Otherwise find the right place
	core::list<BufferedPacket>::Iterator i;
	i = m_list.begin();
	// Find the first packet in the list which has a higher seqnum
	for(; i != m_list.end(); i++){
		u16 s = readU16(&(i->data[BASE_HEADER_SIZE+1]));
		if(s == seqnum){
			throw AlreadyExistsException("Same seqnum in list");
		}
		if(seqnum_higher(s, seqnum)){
			break;
		}
	}
	// If we're at the end of the list, add the packet to the
	// end of the list
	if(i == m_list.end())
	{
		m_list.push_back(p);
		// Done.
		return;
	}
	// Insert before i
	m_list.insert_before(i, p);
}

void ReliablePacketBuffer::incrementTimeouts(float dtime)
{
	core::list<BufferedPacket>::Iterator i;
	i = m_list.begin();
	for(; i != m_list.end(); i++){
		i->time += dtime;
		i->totaltime += dtime;
	}
}

void ReliablePacketBuffer::resetTimedOuts(float timeout)
{
	core::list<BufferedPacket>::Iterator i;
	i = m_list.begin();
	for(; i != m_list.end(); i++){
		if(i->time >= timeout)
			i->time = 0.0;
	}
}

bool ReliablePacketBuffer::anyTotaltimeReached(float timeout)
{
	core::list<BufferedPacket>::Iterator i;
	i = m_list.begin();
	for(; i != m_list.end(); i++){
		if(i->totaltime >= timeout)
			return true;
	}
	return false;
}

core::list<BufferedPacket> ReliablePacketBuffer::getTimedOuts(float timeout)
{
	core::list<BufferedPacket> timed_outs;
	core::list<BufferedPacket>::Iterator i;
	i = m_list.begin();
	for(; i != m_list.end(); i++)
	{
		if(i->time >= timeout)
			timed_outs.push_back(*i);
	}
	return timed_outs;
}

/*
	IncomingSplitBuffer
*/

IncomingSplitBuffer::~IncomingSplitBuffer()
{
	core::map<u16, IncomingSplitPacket*>::Iterator i;
	i = m_buf.getIterator();
	for(; i.atEnd() == false; i++)
	{
		delete i.getNode()->getValue();
	}
}
/*
	This will throw a GotSplitPacketException when a full
	split packet is constructed.
*/
SharedBuffer<u8> IncomingSplitBuffer::insert(BufferedPacket &p, bool reliable)
{
	u32 headersize = BASE_HEADER_SIZE + 7;
	assert(p.data.getSize() >= headersize);
	u8 type = readU8(&p.data[BASE_HEADER_SIZE+0]);
	assert(type == TYPE_SPLIT);
	u16 seqnum = readU16(&p.data[BASE_HEADER_SIZE+1]);
	u16 chunk_count = readU16(&p.data[BASE_HEADER_SIZE+3]);
	u16 chunk_num = readU16(&p.data[BASE_HEADER_SIZE+5]);

	// Add if doesn't exist
	if(m_buf.find(seqnum) == NULL)
	{
		IncomingSplitPacket *sp = new IncomingSplitPacket();
		sp->chunk_count = chunk_count;
		sp->reliable = reliable;
		m_buf[seqnum] = sp;
	}
	
	IncomingSplitPacket *sp = m_buf[seqnum];
	
	// TODO: These errors should be thrown or something? Dunno.
	if(chunk_count != sp->chunk_count)
		derr_con<<"Connection: WARNING: chunk_count="<<chunk_count
				<<" != sp->chunk_count="<<sp->chunk_count
				<<std::endl;
	if(reliable != sp->reliable)
		derr_con<<"Connection: WARNING: reliable="<<reliable
				<<" != sp->reliable="<<sp->reliable
				<<std::endl;

	// If chunk already exists, ignore it.
	// Sometimes two identical packets may arrive when there is network
	// lag and the server re-sends stuff.
	if(sp->chunks.find(chunk_num) != NULL)
		return SharedBuffer<u8>();
	
	// Cut chunk data out of packet
	u32 chunkdatasize = p.data.getSize() - headersize;
	SharedBuffer<u8> chunkdata(chunkdatasize);
	memcpy(*chunkdata, &(p.data[headersize]), chunkdatasize);
	
	// Set chunk data in buffer
	sp->chunks[chunk_num] = chunkdata;
	
	// If not all chunks are received, return empty buffer
	if(sp->allReceived() == false)
		return SharedBuffer<u8>();

	// Calculate total size
	u32 totalsize = 0;
	core::map<u16, SharedBuffer<u8> >::Iterator i;
	i = sp->chunks.getIterator();
	for(; i.atEnd() == false; i++)
	{
		totalsize += i.getNode()->getValue().getSize();
	}
	
	SharedBuffer<u8> fulldata(totalsize);

	// Copy chunks to data buffer
	u32 start = 0;
	for(u32 chunk_i=0; chunk_i<sp->chunk_count;
			chunk_i++)
	{
		SharedBuffer<u8> buf = sp->chunks[chunk_i];
		u16 chunkdatasize = buf.getSize();
		memcpy(&fulldata[start], *buf, chunkdatasize);
		start += chunkdatasize;;
	}

	// Remove sp from buffer
	m_buf.remove(seqnum);
	delete sp;

	return fulldata;
}
void IncomingSplitBuffer::removeUnreliableTimedOuts(float dtime, float timeout)
{
	core::list<u16> remove_queue;
	core::map<u16, IncomingSplitPacket*>::Iterator i;
	i = m_buf.getIterator();
	for(; i.atEnd() == false; i++)
	{
		IncomingSplitPacket *p = i.getNode()->getValue();
		// Reliable ones are not removed by timeout
		if(p->reliable == true)
			continue;
		p->time += dtime;
		if(p->time >= timeout)
			remove_queue.push_back(i.getNode()->getKey());
	}
	core::list<u16>::Iterator j;
	j = remove_queue.begin();
	for(; j != remove_queue.end(); j++)
	{
		dout_con<<"NOTE: Removing timed out unreliable split packet"
				<<std::endl;
		delete m_buf[*j];
		m_buf.remove(*j);
	}
}

/*
	Channel
*/

Channel::Channel()
{
	next_outgoing_seqnum = SEQNUM_INITIAL;
	next_incoming_seqnum = SEQNUM_INITIAL;
	next_outgoing_split_seqnum = SEQNUM_INITIAL;
}
Channel::~Channel()
{
}

/*
	Peer
*/

Peer::Peer(u16 a_id, Address a_address):
	address(a_address),
	id(a_id),
	timeout_counter(0.0),
	ping_timer(0.0),
	resend_timeout(0.5),
	avg_rtt(-1.0),
	has_sent_with_id(false),
	m_sendtime_accu(0),
	m_max_packets_per_second(10),
	m_num_sent(0),
	m_max_num_sent(0),
	congestion_control_aim_rtt(0.2),
	congestion_control_max_rate(400),
	congestion_control_min_rate(10)
{
}
Peer::~Peer()
{
}

void Peer::reportRTT(float rtt)
{
	if(rtt >= 0.0){
		if(rtt < 0.01){
			if(m_max_packets_per_second < congestion_control_max_rate)
				m_max_packets_per_second += 10;
		} else if(rtt < congestion_control_aim_rtt){
			if(m_max_packets_per_second < congestion_control_max_rate)
				m_max_packets_per_second += 2;
		} else {
			m_max_packets_per_second *= 0.8;
			if(m_max_packets_per_second < congestion_control_min_rate)
				m_max_packets_per_second = congestion_control_min_rate;
		}
	}

	if(rtt < -0.999)
	{}
	else if(avg_rtt < 0.0)
		avg_rtt = rtt;
	else
		avg_rtt = rtt * 0.1 + avg_rtt * 0.9;
	
	// Calculate resend_timeout

	/*int reliable_count = 0;
	for(int i=0; i<CHANNEL_COUNT; i++)
	{
		reliable_count += channels[i].outgoing_reliables.size();
	}
	float timeout = avg_rtt * RESEND_TIMEOUT_FACTOR
			* ((float)reliable_count * 1);*/
	
	float timeout = avg_rtt * RESEND_TIMEOUT_FACTOR;
	if(timeout < RESEND_TIMEOUT_MIN)
		timeout = RESEND_TIMEOUT_MIN;
	if(timeout > RESEND_TIMEOUT_MAX)
		timeout = RESEND_TIMEOUT_MAX;
	resend_timeout = timeout;
}
				
/*
	Connection
*/

Connection::Connection(u32 protocol_id, u32 max_packet_size, float timeout):
	m_protocol_id(protocol_id),
	m_max_packet_size(max_packet_size),
	m_timeout(timeout),
	m_peer_id(0),
	m_bc_peerhandler(NULL),
	m_bc_receive_timeout(0),
	m_indentation(0)
{
	m_socket.setTimeoutMs(5);

	Start();
}

Connection::Connection(u32 protocol_id, u32 max_packet_size, float timeout,
		PeerHandler *peerhandler):
	m_protocol_id(protocol_id),
	m_max_packet_size(max_packet_size),
	m_timeout(timeout),
	m_peer_id(0),
	m_bc_peerhandler(peerhandler),
	m_bc_receive_timeout(0),
	m_indentation(0)
{
	m_socket.setTimeoutMs(5);

	Start();
}


Connection::~Connection()
{
	stop();
	// Delete peers
	for(core::map<u16, Peer*>::Iterator
			j = m_peers.getIterator();
			j.atEnd() == false; j++)
	{
		Peer *peer = j.getNode()->getValue();
		delete peer;
	}
}

/* Internal stuff */

void * Connection::Thread()
{
	ThreadStarted();
	log_register_thread("Connection");

	dout_con<<"Connection thread started"<<std::endl;
	
	u32 curtime = porting::getTimeMs();
	u32 lasttime = curtime;

	while(getRun())
	{
		BEGIN_DEBUG_EXCEPTION_HANDLER
		
		lasttime = curtime;
		curtime = porting::getTimeMs();
		float dtime = (float)(curtime - lasttime) / 1000.;
		if(dtime > 0.1)
			dtime = 0.1;
		if(dtime < 0.0)
			dtime = 0.0;
		
		runTimeouts(dtime);

		while(m_command_queue.size() != 0){
			ConnectionCommand c = m_command_queue.pop_front();
			processCommand(c);
		}

		send(dtime);

		receive();
		
		END_DEBUG_EXCEPTION_HANDLER(derr_con);
	}

	return NULL;
}

void Connection::putEvent(ConnectionEvent &e)
{
	assert(e.type != CONNEVENT_NONE);
	m_event_queue.push_back(e);
}

void Connection::processCommand(ConnectionCommand &c)
{
	switch(c.type){
	case CONNCMD_NONE:
		dout_con<<getDesc()<<" processing CONNCMD_NONE"<<std::endl;
		return;
	case CONNCMD_SERVE:
		dout_con<<getDesc()<<" processing CONNCMD_SERVE port="
				<<c.port<<std::endl;
		serve(c.port);
		return;
	case CONNCMD_CONNECT:
		dout_con<<getDesc()<<" processing CONNCMD_CONNECT"<<std::endl;
		connect(c.address);
		return;
	case CONNCMD_DISCONNECT:
		dout_con<<getDesc()<<" processing CONNCMD_DISCONNECT"<<std::endl;
		disconnect();
		return;
	case CONNCMD_SEND:
		dout_con<<getDesc()<<" processing CONNCMD_SEND"<<std::endl;
		send(c.peer_id, c.channelnum, c.data, c.reliable);
		return;
	case CONNCMD_SEND_TO_ALL:
		dout_con<<getDesc()<<" processing CONNCMD_SEND_TO_ALL"<<std::endl;
		sendToAll(c.channelnum, c.data, c.reliable);
		return;
	case CONNCMD_DELETE_PEER:
		dout_con<<getDesc()<<" processing CONNCMD_DELETE_PEER"<<std::endl;
		deletePeer(c.peer_id, false);
		return;
	}
}

void Connection::send(float dtime)
{
	for(core::map<u16, Peer*>::Iterator
			j = m_peers.getIterator();
			j.atEnd() == false; j++)
	{
		Peer *peer = j.getNode()->getValue();
		peer->m_sendtime_accu += dtime;
		peer->m_num_sent = 0;
		peer->m_max_num_sent = peer->m_sendtime_accu *
				peer->m_max_packets_per_second;
	}
	Queue<OutgoingPacket> postponed_packets;
	while(m_outgoing_queue.size() != 0){
		OutgoingPacket packet = m_outgoing_queue.pop_front();
		Peer *peer = getPeerNoEx(packet.peer_id);
		if(!peer)
			continue;
		if(peer->channels[packet.channelnum].outgoing_reliables.size() >= 5){
			postponed_packets.push_back(packet);
		} else if(peer->m_num_sent < peer->m_max_num_sent){
			rawSendAsPacket(packet.peer_id, packet.channelnum,
					packet.data, packet.reliable);
			peer->m_num_sent++;
		} else {
			postponed_packets.push_back(packet);
		}
	}
	while(postponed_packets.size() != 0){
		m_outgoing_queue.push_back(postponed_packets.pop_front());
	}
	for(core::map<u16, Peer*>::Iterator
			j = m_peers.getIterator();
			j.atEnd() == false; j++)
	{
		Peer *peer = j.getNode()->getValue();
		peer->m_sendtime_accu -= (float)peer->m_num_sent /
				peer->m_max_packets_per_second;
		if(peer->m_sendtime_accu > 10. / peer->m_max_packets_per_second)
			peer->m_sendtime_accu = 10. / peer->m_max_packets_per_second;
	}
}

// Receive packets from the network and buffers and create ConnectionEvents
void Connection::receive()
{
	u32 datasize = m_max_packet_size * 2;  // Double it just to be safe
	// TODO: We can not know how many layers of header there are.
	// For now, just assume there are no other than the base headers.
	u32 packet_maxsize = datasize + BASE_HEADER_SIZE;
	SharedBuffer<u8> packetdata(packet_maxsize);

	bool single_wait_done = false;
	
	for(;;)
	{
	try{
		/* Check if some buffer has relevant data */
		{
			u16 peer_id;
			SharedBuffer<u8> resultdata;
			bool got = getFromBuffers(peer_id, resultdata);
			if(got){
				ConnectionEvent e;
				e.dataReceived(peer_id, resultdata);
				putEvent(e);
				continue;
			}
		}
		
		if(single_wait_done){
			if(m_socket.WaitData(0) == false)
				break;
		}
		
		single_wait_done = true;

		Address sender;
		s32 received_size = m_socket.Receive(sender, *packetdata, packet_maxsize);

		if(received_size < 0)
			break;
		if(received_size < BASE_HEADER_SIZE)
			continue;
		if(readU32(&packetdata[0]) != m_protocol_id)
			continue;
		
		u16 peer_id = readPeerId(*packetdata);
		u8 channelnum = readChannel(*packetdata);
		if(channelnum > CHANNEL_COUNT-1){
			PrintInfo(derr_con);
			derr_con<<"Receive(): Invalid channel "<<channelnum<<std::endl;
			throw InvalidIncomingDataException("Channel doesn't exist");
		}

		if(peer_id == PEER_ID_INEXISTENT)
		{
			/*
				Somebody is trying to send stuff to us with no peer id.
				
				Check if the same address and port was added to our peer
				list before.
				Allow only entries that have has_sent_with_id==false.
			*/

			core::map<u16, Peer*>::Iterator j;
			j = m_peers.getIterator();
			for(; j.atEnd() == false; j++)
			{
				Peer *peer = j.getNode()->getValue();
				if(peer->has_sent_with_id)
					continue;
				if(peer->address == sender)
					break;
			}
			
			/*
				If no peer was found with the same address and port,
				we shall assume it is a new peer and create an entry.
			*/
			if(j.atEnd())
			{
				// Pass on to adding the peer
			}
			// Else: A peer was found.
			else
			{
				Peer *peer = j.getNode()->getValue();
				peer_id = peer->id;
				PrintInfo(derr_con);
				derr_con<<"WARNING: Assuming unknown peer to be "
						<<"peer_id="<<peer_id<<std::endl;
			}
		}
		
		/*
			The peer was not found in our lists. Add it.
		*/
		if(peer_id == PEER_ID_INEXISTENT)
		{
			// Somebody wants to make a new connection

			// Get a unique peer id (2 or higher)
			u16 peer_id_new = 2;
			/*
				Find an unused peer id
			*/
			bool out_of_ids = false;
			for(;;)
			{
				// Check if exists
				if(m_peers.find(peer_id_new) == NULL)
					break;
				// Check for overflow
				if(peer_id_new == 65535){
					out_of_ids = true;
					break;
				}
				peer_id_new++;
			}
			if(out_of_ids){
				errorstream<<getDesc()<<" ran out of peer ids"<<std::endl;
				continue;
			}

			PrintInfo();
			dout_con<<"Receive(): Got a packet with peer_id=PEER_ID_INEXISTENT,"
					" giving peer_id="<<peer_id_new<<std::endl;

			// Create a peer
			Peer *peer = new Peer(peer_id_new, sender);
			m_peers.insert(peer->id, peer);
			
			// Create peer addition event
			ConnectionEvent e;
			e.peerAdded(peer_id_new, sender);
			putEvent(e);
			
			// Create CONTROL packet to tell the peer id to the new peer.
			SharedBuffer<u8> reply(4);
			writeU8(&reply[0], TYPE_CONTROL);
			writeU8(&reply[1], CONTROLTYPE_SET_PEER_ID);
			writeU16(&reply[2], peer_id_new);
			sendAsPacket(peer_id_new, 0, reply, true);
			
			// We're now talking to a valid peer_id
			peer_id = peer_id_new;

			// Go on and process whatever it sent
		}

		core::map<u16, Peer*>::Node *node = m_peers.find(peer_id);

		if(node == NULL)
		{
			// Peer not found
			// This means that the peer id of the sender is not PEER_ID_INEXISTENT
			// and it is invalid.
			PrintInfo(derr_con);
			derr_con<<"Receive(): Peer not found"<<std::endl;
			throw InvalidIncomingDataException("Peer not found (possible timeout)");
		}

		Peer *peer = node->getValue();

		// Validate peer address
		if(peer->address != sender)
		{
			PrintInfo(derr_con);
			derr_con<<"Peer "<<peer_id<<" sending from different address."
					" Ignoring."<<std::endl;
			continue;
		}
		
		peer->timeout_counter = 0.0;

		Channel *channel = &(peer->channels[channelnum]);
		
		// Throw the received packet to channel->processPacket()

		// Make a new SharedBuffer from the data without the base headers
		SharedBuffer<u8> strippeddata(received_size - BASE_HEADER_SIZE);
		memcpy(*strippeddata, &packetdata[BASE_HEADER_SIZE],
				strippeddata.getSize());
		
		try{
			// Process it (the result is some data with no headers made by us)
			SharedBuffer<u8> resultdata = processPacket
					(channel, strippeddata, peer_id, channelnum, false);
			
			PrintInfo();
			dout_con<<"ProcessPacket returned data of size "
					<<resultdata.getSize()<<std::endl;
			
			ConnectionEvent e;
			e.dataReceived(peer_id, resultdata);
			putEvent(e);
			continue;
		}catch(ProcessedSilentlyException &e){
		}
	}catch(InvalidIncomingDataException &e){
	}
	catch(ProcessedSilentlyException &e){
	}
	} // for
}

void Connection::runTimeouts(float dtime)
{
	float congestion_control_aim_rtt
			= g_settings->getFloat("congestion_control_aim_rtt");
	float congestion_control_max_rate
			= g_settings->getFloat("congestion_control_max_rate");
	float congestion_control_min_rate
			= g_settings->getFloat("congestion_control_min_rate");

	core::list<u16> timeouted_peers;
	core::map<u16, Peer*>::Iterator j;
	j = m_peers.getIterator();
	for(; j.atEnd() == false; j++)
	{
		Peer *peer = j.getNode()->getValue();

		// Update congestion control values
		peer->congestion_control_aim_rtt = congestion_control_aim_rtt;
		peer->congestion_control_max_rate = congestion_control_max_rate;
		peer->congestion_control_min_rate = congestion_control_min_rate;
		
		/*
			Check peer timeout
		*/
		peer->timeout_counter += dtime;
		if(peer->timeout_counter > m_timeout)
		{
			PrintInfo(derr_con);
			derr_con<<"RunTimeouts(): Peer "<<peer->id
					<<" has timed out."
					<<" (source=peer->timeout_counter)"
					<<std::endl;
			// Add peer to the list
			timeouted_peers.push_back(peer->id);
			// Don't bother going through the buffers of this one
			continue;
		}

		float resend_timeout = peer->resend_timeout;
		for(u16 i=0; i<CHANNEL_COUNT; i++)
		{
			core::list<BufferedPacket> timed_outs;
			core::list<BufferedPacket>::Iterator j;
			
			Channel *channel = &peer->channels[i];

			// Remove timed out incomplete unreliable split packets
			channel->incoming_splits.removeUnreliableTimedOuts(dtime, m_timeout);
			
			// Increment reliable packet times
			channel->outgoing_reliables.incrementTimeouts(dtime);

			// Check reliable packet total times, remove peer if
			// over timeout.
			if(channel->outgoing_reliables.anyTotaltimeReached(m_timeout))
			{
				PrintInfo(derr_con);
				derr_con<<"RunTimeouts(): Peer "<<peer->id
						<<" has timed out."
						<<" (source=reliable packet totaltime)"
						<<std::endl;
				// Add peer to the to-be-removed list
				timeouted_peers.push_back(peer->id);
				goto nextpeer;
			}

			// Re-send timed out outgoing reliables
			
			timed_outs = channel->
					outgoing_reliables.getTimedOuts(resend_timeout);

			channel->outgoing_reliables.resetTimedOuts(resend_timeout);

			j = timed_outs.begin();
			for(; j != timed_outs.end(); j++)
			{
				u16 peer_id = readPeerId(*(j->data));
				u8 channel = readChannel(*(j->data));
				u16 seqnum = readU16(&(j->data[BASE_HEADER_SIZE+1]));

				PrintInfo(derr_con);
				derr_con<<"RE-SENDING timed-out RELIABLE to ";
				j->address.print(&derr_con);
				derr_con<<"(t/o="<<resend_timeout<<"): "
						<<"from_peer_id="<<peer_id
						<<", channel="<<((int)channel&0xff)
						<<", seqnum="<<seqnum
						<<std::endl;

				rawSend(*j);

				// Enlarge avg_rtt and resend_timeout:
				// The rtt will be at least the timeout.
				// NOTE: This won't affect the timeout of the next
				// checked channel because it was cached.
				peer->reportRTT(resend_timeout);
			}
		}
		
		/*
			Send pings
		*/
		peer->ping_timer += dtime;
		if(peer->ping_timer >= 5.0)
		{
			// Create and send PING packet
			SharedBuffer<u8> data(2);
			writeU8(&data[0], TYPE_CONTROL);
			writeU8(&data[1], CONTROLTYPE_PING);
			rawSendAsPacket(peer->id, 0, data, true);

			peer->ping_timer = 0.0;
		}
		
nextpeer:
		continue;
	}

	// Remove timed out peers
	core::list<u16>::Iterator i = timeouted_peers.begin();
	for(; i != timeouted_peers.end(); i++)
	{
		PrintInfo(derr_con);
		derr_con<<"RunTimeouts(): Removing peer "<<(*i)<<std::endl;
		deletePeer(*i, true);
	}
}

void Connection::serve(u16 port)
{
	dout_con<<getDesc()<<" serving at port "<<port<<std::endl;
	try{
		m_socket.Bind(port);
		m_peer_id = PEER_ID_SERVER;
	}
	catch(SocketException &e){
		// Create event
		ConnectionEvent ce;
		ce.bindFailed();
		putEvent(ce);
	}
}

void Connection::connect(Address address)
{
	dout_con<<getDesc()<<" connecting to "<<address.serializeString()
			<<":"<<address.getPort()<<std::endl;

	core::map<u16, Peer*>::Node *node = m_peers.find(PEER_ID_SERVER);
	if(node != NULL){
		throw ConnectionException("Already connected to a server");
	}

	Peer *peer = new Peer(PEER_ID_SERVER, address);
	m_peers.insert(peer->id, peer);

	// Create event
	ConnectionEvent e;
	e.peerAdded(peer->id, peer->address);
	putEvent(e);
	
	m_socket.Bind(0);
	
	// Send a dummy packet to server with peer_id = PEER_ID_INEXISTENT
	m_peer_id = PEER_ID_INEXISTENT;
	SharedBuffer<u8> data(0);
	Send(PEER_ID_SERVER, 0, data, true);
}

void Connection::disconnect()
{
	dout_con<<getDesc()<<" disconnecting"<<std::endl;

	// Create and send DISCO packet
	SharedBuffer<u8> data(2);
	writeU8(&data[0], TYPE_CONTROL);
	writeU8(&data[1], CONTROLTYPE_DISCO);
	
	// Send to all
	core::map<u16, Peer*>::Iterator j;
	j = m_peers.getIterator();
	for(; j.atEnd() == false; j++)
	{
		Peer *peer = j.getNode()->getValue();
		rawSendAsPacket(peer->id, 0, data, false);
	}
}

void Connection::sendToAll(u8 channelnum, SharedBuffer<u8> data, bool reliable)
{
	core::map<u16, Peer*>::Iterator j;
	j = m_peers.getIterator();
	for(; j.atEnd() == false; j++)
	{
		Peer *peer = j.getNode()->getValue();
		send(peer->id, channelnum, data, reliable);
	}
}

void Connection::send(u16 peer_id, u8 channelnum,
		SharedBuffer<u8> data, bool reliable)
{
	dout_con<<getDesc()<<" sending to peer_id="<<peer_id<<std::endl;

	assert(channelnum < CHANNEL_COUNT);
	
	Peer *peer = getPeerNoEx(peer_id);
	if(peer == NULL)
		return;
	Channel *channel = &(peer->channels[channelnum]);

	u32 chunksize_max = m_max_packet_size - BASE_HEADER_SIZE;
	if(reliable)
		chunksize_max -= RELIABLE_HEADER_SIZE;

	core::list<SharedBuffer<u8> > originals;
	originals = makeAutoSplitPacket(data, chunksize_max,
			channel->next_outgoing_split_seqnum);
	
	core::list<SharedBuffer<u8> >::Iterator i;
	i = originals.begin();
	for(; i != originals.end(); i++)
	{
		SharedBuffer<u8> original = *i;
		
		sendAsPacket(peer_id, channelnum, original, reliable);
	}
}

void Connection::sendAsPacket(u16 peer_id, u8 channelnum,
		SharedBuffer<u8> data, bool reliable)
{
	OutgoingPacket packet(peer_id, channelnum, data, reliable);
	m_outgoing_queue.push_back(packet);
}

void Connection::rawSendAsPacket(u16 peer_id, u8 channelnum,
		SharedBuffer<u8> data, bool reliable)
{
	Peer *peer = getPeerNoEx(peer_id);
	if(!peer)
		return;
	Channel *channel = &(peer->channels[channelnum]);

	if(reliable)
	{
		u16 seqnum = channel->next_outgoing_seqnum;
		channel->next_outgoing_seqnum++;

		SharedBuffer<u8> reliable = makeReliablePacket(data, seqnum);

		// Add base headers and make a packet
		BufferedPacket p = makePacket(peer->address, reliable,
				m_protocol_id, m_peer_id, channelnum);
		
		try{
			// Buffer the packet
			channel->outgoing_reliables.insert(p);
		}
		catch(AlreadyExistsException &e)
		{
			PrintInfo(derr_con);
			derr_con<<"WARNING: Going to send a reliable packet "
					"seqnum="<<seqnum<<" that is already "
					"in outgoing buffer"<<std::endl;
			//assert(0);
		}
		
		// Send the packet
		rawSend(p);
	}
	else
	{
		// Add base headers and make a packet
		BufferedPacket p = makePacket(peer->address, data,
				m_protocol_id, m_peer_id, channelnum);

		// Send the packet
		rawSend(p);
	}
}

void Connection::rawSend(const BufferedPacket &packet)
{
	try{
		m_socket.Send(packet.address, *packet.data, packet.data.getSize());
	} catch(SendFailedException &e){
		derr_con<<"Connection::rawSend(): SendFailedException: "
				<<packet.address.serializeString()<<std::endl;
	}
}

Peer* Connection::getPeer(u16 peer_id)
{
	core::map<u16, Peer*>::Node *node = m_peers.find(peer_id);

	if(node == NULL){
		throw PeerNotFoundException("GetPeer: Peer not found (possible timeout)");
	}

	// Error checking
	assert(node->getValue()->id == peer_id);

	return node->getValue();
}

Peer* Connection::getPeerNoEx(u16 peer_id)
{
	core::map<u16, Peer*>::Node *node = m_peers.find(peer_id);

	if(node == NULL){
		return NULL;
	}

	// Error checking
	assert(node->getValue()->id == peer_id);

	return node->getValue();
}

core::list<Peer*> Connection::getPeers()
{
	core::list<Peer*> list;
	core::map<u16, Peer*>::Iterator j;
	j = m_peers.getIterator();
	for(; j.atEnd() == false; j++)
	{
		Peer *peer = j.getNode()->getValue();
		list.push_back(peer);
	}
	return list;
}

bool Connection::getFromBuffers(u16 &peer_id, SharedBuffer<u8> &dst)
{
	core::map<u16, Peer*>::Iterator j;
	j = m_peers.getIterator();
	for(; j.atEnd() == false; j++)
	{
		Peer *peer = j.getNode()->getValue();
		for(u16 i=0; i<CHANNEL_COUNT; i++)
		{
			Channel *channel = &peer->channels[i];
			SharedBuffer<u8> resultdata;
			bool got = checkIncomingBuffers(channel, peer_id, resultdata);
			if(got){
				dst = resultdata;
				return true;
			}
		}
	}
	return false;
}

bool Connection::checkIncomingBuffers(Channel *channel, u16 &peer_id,
		SharedBuffer<u8> &dst)
{
	u16 firstseqnum = 0;
	// Clear old packets from start of buffer
	try{
	for(;;){
		firstseqnum = channel->incoming_reliables.getFirstSeqnum();
		if(seqnum_higher(channel->next_incoming_seqnum, firstseqnum))
			channel->incoming_reliables.popFirst();
		else
			break;
	}
	// This happens if all packets are old
	}catch(con::NotFoundException)
	{}
	
	if(channel->incoming_reliables.empty() == false)
	{
		if(firstseqnum == channel->next_incoming_seqnum)
		{
			BufferedPacket p = channel->incoming_reliables.popFirst();
			
			peer_id = readPeerId(*p.data);
			u8 channelnum = readChannel(*p.data);
			u16 seqnum = readU16(&p.data[BASE_HEADER_SIZE+1]);

			PrintInfo();
			dout_con<<"UNBUFFERING TYPE_RELIABLE"
					<<" seqnum="<<seqnum
					<<" peer_id="<<peer_id
					<<" channel="<<((int)channelnum&0xff)
					<<std::endl;

			channel->next_incoming_seqnum++;
			
			u32 headers_size = BASE_HEADER_SIZE + RELIABLE_HEADER_SIZE;
			// Get out the inside packet and re-process it
			SharedBuffer<u8> payload(p.data.getSize() - headers_size);
			memcpy(*payload, &p.data[headers_size], payload.getSize());

			dst = processPacket(channel, payload, peer_id, channelnum, true);
			return true;
		}
	}
	return false;
}

SharedBuffer<u8> Connection::processPacket(Channel *channel,
		SharedBuffer<u8> packetdata, u16 peer_id,
		u8 channelnum, bool reliable)
{
	IndentationRaiser iraiser(&(m_indentation));

	if(packetdata.getSize() < 1)
		throw InvalidIncomingDataException("packetdata.getSize() < 1");

	u8 type = readU8(&packetdata[0]);
	
	if(type == TYPE_CONTROL)
	{
		if(packetdata.getSize() < 2)
			throw InvalidIncomingDataException("packetdata.getSize() < 2");

		u8 controltype = readU8(&packetdata[1]);

		if(controltype == CONTROLTYPE_ACK)
		{
			if(packetdata.getSize() < 4)
				throw InvalidIncomingDataException
						("packetdata.getSize() < 4 (ACK header size)");

			u16 seqnum = readU16(&packetdata[2]);
			PrintInfo();
			dout_con<<"Got CONTROLTYPE_ACK: channelnum="
					<<((int)channelnum&0xff)<<", peer_id="<<peer_id
					<<", seqnum="<<seqnum<<std::endl;

			try{
				BufferedPacket p = channel->outgoing_reliables.popSeqnum(seqnum);
				// Get round trip time
				float rtt = p.totaltime;

				// Let peer calculate stuff according to it
				// (avg_rtt and resend_timeout)
				Peer *peer = getPeer(peer_id);
				peer->reportRTT(rtt);

				//PrintInfo(dout_con);
				//dout_con<<"RTT = "<<rtt<<std::endl;

				/*dout_con<<"OUTGOING: ";
				PrintInfo();
				channel->outgoing_reliables.print();
				dout_con<<std::endl;*/
			}
			catch(NotFoundException &e){
				PrintInfo(derr_con);
				derr_con<<"WARNING: ACKed packet not "
						"in outgoing queue"
						<<std::endl;
			}

			throw ProcessedSilentlyException("Got an ACK");
		}
		else if(controltype == CONTROLTYPE_SET_PEER_ID)
		{
			if(packetdata.getSize() < 4)
				throw InvalidIncomingDataException
						("packetdata.getSize() < 4 (SET_PEER_ID header size)");
			u16 peer_id_new = readU16(&packetdata[2]);
			PrintInfo();
			dout_con<<"Got new peer id: "<<peer_id_new<<"... "<<std::endl;

			if(GetPeerID() != PEER_ID_INEXISTENT)
			{
				PrintInfo(derr_con);
				derr_con<<"WARNING: Not changing"
						" existing peer id."<<std::endl;
			}
			else
			{
				dout_con<<"changing."<<std::endl;
				SetPeerID(peer_id_new);
			}
			throw ProcessedSilentlyException("Got a SET_PEER_ID");
		}
		else if(controltype == CONTROLTYPE_PING)
		{
			// Just ignore it, the incoming data already reset
			// the timeout counter
			PrintInfo();
			dout_con<<"PING"<<std::endl;
			throw ProcessedSilentlyException("Got a PING");
		}
		else if(controltype == CONTROLTYPE_DISCO)
		{
			// Just ignore it, the incoming data already reset
			// the timeout counter
			PrintInfo();
			dout_con<<"DISCO: Removing peer "<<(peer_id)<<std::endl;
			
			if(deletePeer(peer_id, false) == false)
			{
				PrintInfo(derr_con);
				derr_con<<"DISCO: Peer not found"<<std::endl;
			}

			throw ProcessedSilentlyException("Got a DISCO");
		}
		else{
			PrintInfo(derr_con);
			derr_con<<"INVALID TYPE_CONTROL: invalid controltype="
					<<((int)controltype&0xff)<<std::endl;
			throw InvalidIncomingDataException("Invalid control type");
		}
	}
	else if(type == TYPE_ORIGINAL)
	{
		if(packetdata.getSize() < ORIGINAL_HEADER_SIZE)
			throw InvalidIncomingDataException
					("packetdata.getSize() < ORIGINAL_HEADER_SIZE");
		PrintInfo();
		dout_con<<"RETURNING TYPE_ORIGINAL to user"
				<<std::endl;
		// Get the inside packet out and return it
		SharedBuffer<u8> payload(packetdata.getSize() - ORIGINAL_HEADER_SIZE);
		memcpy(*payload, &packetdata[ORIGINAL_HEADER_SIZE], payload.getSize());
		return payload;
	}
	else if(type == TYPE_SPLIT)
	{
		// We have to create a packet again for buffering
		// This isn't actually too bad an idea.
		BufferedPacket packet = makePacket(
				getPeer(peer_id)->address,
				packetdata,
				GetProtocolID(),
				peer_id,
				channelnum);
		// Buffer the packet
		SharedBuffer<u8> data = channel->incoming_splits.insert(packet, reliable);
		if(data.getSize() != 0)
		{
			PrintInfo();
			dout_con<<"RETURNING TYPE_SPLIT: Constructed full data, "
					<<"size="<<data.getSize()<<std::endl;
			return data;
		}
		PrintInfo();
		dout_con<<"BUFFERED TYPE_SPLIT"<<std::endl;
		throw ProcessedSilentlyException("Buffered a split packet chunk");
	}
	else if(type == TYPE_RELIABLE)
	{
		// Recursive reliable packets not allowed
		assert(reliable == false);

		if(packetdata.getSize() < RELIABLE_HEADER_SIZE)
			throw InvalidIncomingDataException
					("packetdata.getSize() < RELIABLE_HEADER_SIZE");

		u16 seqnum = readU16(&packetdata[1]);

		bool is_future_packet = seqnum_higher(seqnum, channel->next_incoming_seqnum);
		bool is_old_packet = seqnum_higher(channel->next_incoming_seqnum, seqnum);
		
		PrintInfo();
		if(is_future_packet)
			dout_con<<"BUFFERING";
		else if(is_old_packet)
			dout_con<<"OLD";
		else
			dout_con<<"RECUR";
		dout_con<<" TYPE_RELIABLE seqnum="<<seqnum
				<<" next="<<channel->next_incoming_seqnum;
		dout_con<<" [sending CONTROLTYPE_ACK"
				" to peer_id="<<peer_id<<"]";
		dout_con<<std::endl;
		
		//DEBUG
		//assert(channel->incoming_reliables.size() < 100);

		// Send a CONTROLTYPE_ACK
		SharedBuffer<u8> reply(4);
		writeU8(&reply[0], TYPE_CONTROL);
		writeU8(&reply[1], CONTROLTYPE_ACK);
		writeU16(&reply[2], seqnum);
		rawSendAsPacket(peer_id, channelnum, reply, false);

		//if(seqnum_higher(seqnum, channel->next_incoming_seqnum))
		if(is_future_packet)
		{
			/*PrintInfo();
			dout_con<<"Buffering reliable packet (seqnum="
					<<seqnum<<")"<<std::endl;*/
			
			// This one comes later, buffer it.
			// Actually we have to make a packet to buffer one.
			// Well, we have all the ingredients, so just do it.
			BufferedPacket packet = makePacket(
					getPeer(peer_id)->address,
					packetdata,
					GetProtocolID(),
					peer_id,
					channelnum);
			try{
				channel->incoming_reliables.insert(packet);
				
				/*PrintInfo();
				dout_con<<"INCOMING: ";
				channel->incoming_reliables.print();
				dout_con<<std::endl;*/
			}
			catch(AlreadyExistsException &e)
			{
			}

			throw ProcessedSilentlyException("Buffered future reliable packet");
		}
		//else if(seqnum_higher(channel->next_incoming_seqnum, seqnum))
		else if(is_old_packet)
		{
			// An old packet, dump it
			throw InvalidIncomingDataException("Got an old reliable packet");
		}

		channel->next_incoming_seqnum++;

		// Get out the inside packet and re-process it
		SharedBuffer<u8> payload(packetdata.getSize() - RELIABLE_HEADER_SIZE);
		memcpy(*payload, &packetdata[RELIABLE_HEADER_SIZE], payload.getSize());

		return processPacket(channel, payload, peer_id, channelnum, true);
	}
	else
	{
		PrintInfo(derr_con);
		derr_con<<"Got invalid type="<<((int)type&0xff)<<std::endl;
		throw InvalidIncomingDataException("Invalid packet type");
	}
	
	// We should never get here.
	// If you get here, add an exception or a return to some of the
	// above conditionals.
	assert(0);
	throw BaseException("Error in Channel::ProcessPacket()");
}

bool Connection::deletePeer(u16 peer_id, bool timeout)
{
	if(m_peers.find(peer_id) == NULL)
		return false;
	
	Peer *peer = m_peers[peer_id];

	// Create event
	ConnectionEvent e;
	e.peerRemoved(peer_id, timeout, peer->address);
	putEvent(e);

	delete m_peers[peer_id];
	m_peers.remove(peer_id);
	return true;
}

/* Interface */

ConnectionEvent Connection::getEvent()
{
	if(m_event_queue.size() == 0){
		ConnectionEvent e;
		e.type = CONNEVENT_NONE;
		return e;
	}
	return m_event_queue.pop_front();
}

ConnectionEvent Connection::waitEvent(u32 timeout_ms)
{
	try{
		return m_event_queue.pop_front(timeout_ms);
	} catch(ItemNotFoundException &ex){
		ConnectionEvent e;
		e.type = CONNEVENT_NONE;
		return e;
	}
}

void Connection::putCommand(ConnectionCommand &c)
{
	m_command_queue.push_back(c);
}

void Connection::Serve(unsigned short port)
{
	ConnectionCommand c;
	c.serve(port);
	putCommand(c);
}

void Connection::Connect(Address address)
{
	ConnectionCommand c;
	c.connect(address);
	putCommand(c);
}

bool Connection::Connected()
{
	JMutexAutoLock peerlock(m_peers_mutex);

	if(m_peers.size() != 1)
		return false;
		
	core::map<u16, Peer*>::Node *node = m_peers.find(PEER_ID_SERVER);
	if(node == NULL)
		return false;
	
	if(m_peer_id == PEER_ID_INEXISTENT)
		return false;
	
	return true;
}

void Connection::Disconnect()
{
	ConnectionCommand c;
	c.disconnect();
	putCommand(c);
}

u32 Connection::Receive(u16 &peer_id, SharedBuffer<u8> &data)
{
	for(;;){
		ConnectionEvent e = waitEvent(m_bc_receive_timeout);
		if(e.type != CONNEVENT_NONE)
			dout_con<<getDesc()<<": Receive: got event: "
					<<e.describe()<<std::endl;
		switch(e.type){
		case CONNEVENT_NONE:
			throw NoIncomingDataException("No incoming data");
		case CONNEVENT_DATA_RECEIVED:
			peer_id = e.peer_id;
			data = SharedBuffer<u8>(e.data);
			return e.data.getSize();
		case CONNEVENT_PEER_ADDED: {
			Peer tmp(e.peer_id, e.address);
			if(m_bc_peerhandler)
				m_bc_peerhandler->peerAdded(&tmp);
			continue; }
		case CONNEVENT_PEER_REMOVED: {
			Peer tmp(e.peer_id, e.address);
			if(m_bc_peerhandler)
				m_bc_peerhandler->deletingPeer(&tmp, e.timeout);
			continue; }
		case CONNEVENT_BIND_FAILED:
			throw ConnectionBindFailed("Failed to bind socket "
					"(port already in use?)");
		}
	}
	throw NoIncomingDataException("No incoming data");
}

void Connection::SendToAll(u8 channelnum, SharedBuffer<u8> data, bool reliable)
{
	assert(channelnum < CHANNEL_COUNT);

	ConnectionCommand c;
	c.sendToAll(channelnum, data, reliable);
	putCommand(c);
}

void Connection::Send(u16 peer_id, u8 channelnum,
		SharedBuffer<u8> data, bool reliable)
{
	assert(channelnum < CHANNEL_COUNT);

	ConnectionCommand c;
	c.send(peer_id, channelnum, data, reliable);
	putCommand(c);
}

void Connection::RunTimeouts(float dtime)
{
	// No-op
}

Address Connection::GetPeerAddress(u16 peer_id)
{
	JMutexAutoLock peerlock(m_peers_mutex);
	return getPeer(peer_id)->address;
}

float Connection::GetPeerAvgRTT(u16 peer_id)
{
	JMutexAutoLock peerlock(m_peers_mutex);
	return getPeer(peer_id)->avg_rtt;
}

void Connection::DeletePeer(u16 peer_id)
{
	ConnectionCommand c;
	c.deletePeer(peer_id);
	putCommand(c);
}

void Connection::PrintInfo(std::ostream &out)
{
	out<<getDesc()<<": ";
}

void Connection::PrintInfo()
{
	PrintInfo(dout_con);
}

std::string Connection::getDesc()
{
	return std::string("con(")+itos(m_socket.GetHandle())+"/"+itos(m_peer_id)+")";
}

} // namespace