File: netobject_channel_generic.cpp

package info (click to toggle)
clanlib 0.5.4-1-6
  • links: PTS
  • area: main
  • in suites: woody
  • size: 10,320 kB
  • ctags: 10,893
  • sloc: cpp: 76,056; xml: 3,281; sh: 2,961; perl: 1,204; asm: 837; makefile: 775
file content (144 lines) | stat: -rw-r--r-- 4,014 bytes parent folder | download
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
/*
	$Id: netobject_channel_generic.cpp,v 1.5 2001/11/12 20:32:32 mbn Exp $

	------------------------------------------------------------------------
	ClanLib, the platform independent game SDK.

	This library is distributed under the GNU LIBRARY GENERAL PUBLIC LICENSE
	version 2. See COPYING for details.

	For a total list of contributers see CREDITS.

	------------------------------------------------------------------------
*/

#ifdef WIN32
#pragma warning (disable:4786)
#endif

#include "API/Core/IOData/inputsource_memory.h"
#include "API/Core/IOData/outputsource_memory.h"
#include "API/Core/System/system.h"
#include "API/Network/netsession.h"
#include "API/Network/netmessage.h"
#include "API/Network/netcomputer.h"
#include "API/Network/netgroup.h"
#include "netobject_channel_generic.h"
#include "netobject_generic.h"
#include <iostream>

CL_NetObjectChannel_Generic::CL_NetObjectChannel_Generic(
	class CL_NetSession *_netgame,
	int _channel,
	CL_NetObjectChannel *_owner)
: ref(0), id_counter(0), channel(_channel), netgame(_netgame), owner(_owner)
{
}

CL_NetObjectChannel_Generic::~CL_NetObjectChannel_Generic()
{
}

int CL_NetObjectChannel_Generic::add_ref()
{
	return ++ref;
}

int CL_NetObjectChannel_Generic::release_ref()
{
	return --ref;
}

void CL_NetObjectChannel_Generic::send(int obj_id, int msg_type, const std::string &message)
{
	CL_OutputSource_Memory output;
	output.write_int32(obj_id);
	output.write_int32(msg_type);
	output.write(message.data(), message.size());

	// std::cout << "Sending netobject message. obj_id = " << obj_id << ", msg_type = " << msg_type << std::endl;
	netgame->send(channel, netgame->get_all(), CL_NetMessage(output.get_data()));
}

void CL_NetObjectChannel_Generic::talkback(int obj_id, int talkback_type, const std::string &message)
{
	CL_OutputSource_Memory output;
	output.write_int32(obj_id);
	output.write_int32(talkback_type);
	output.write(message.data(), message.size());

	// std::cout << "Sending netobject talkback message. obj_id = " << obj_id << ", talkback_type = " << talkback_type << std::endl;
	netgame->send(channel, netgame->get_server(), CL_NetMessage(output.get_data()));
}

void CL_NetObjectChannel_Generic::begin_sync(const CL_NetGroup *group)
{
}

void CL_NetObjectChannel_Generic::end_sync(const CL_NetGroup *group)
{
	CL_OutputSource_Memory output;
	output.write_int32(-1);

	netgame->send(channel, *group, CL_NetMessage(output.get_data()));
}

bool CL_NetObjectChannel_Generic::wait_sync(int timeout)
{
	int start_time = (int)CL_System::get_time();

	while (true)
	{
		CL_System::keep_alive();
		if (!received_sync.empty())
		{
			received_sync.pop();
			return true;
		}

		if (timeout != -1) continue;
		if ((int)CL_System::get_time() -start_time - timeout >= 0) break;
	}

	return false;
}

void CL_NetObjectChannel_Generic::keep_alive()
{
	while (netgame->peek(channel))
	{
		CL_NetMessage netmsg = netgame->receive(channel, 0);
		CL_InputSource_Memory input(netmsg.data);
		int obj_id = input.read_int32();

		if (obj_id == -1 && netmsg.data.size() == sizeof(int)) // sync message
		{
			// std::cout << "Received netobject sync message" << std::endl;
			received_sync.push(true);
			continue;
		}

		int msg_type = input.read_int32();
		// std::cout << "Received netobject message. obj_id = " << obj_id << ", msg_type = " << msg_type << std::endl;

		if (netmsg.from == netgame->get_server()) // message received from server:
		{
			std::map<int, CL_NetObject_Generic*>::iterator it = objects.find(obj_id);
			if (it == objects.end()) // object not found, create a new one.
			{
				sig_create_object(CL_NetObject(obj_id, owner), msg_type, input.get_data().substr(8));
				continue;
			}

			it->second->msg_signals[msg_type](input);
		}
		else // message received from client (talkback)
		{
			std::map<int, CL_NetObject_Generic*>::iterator it = objects.find(obj_id);
			if (it == objects.end()) // object not found, ignore message
				continue;

			it->second->talkback_signals[msg_type](netmsg.from, input);
		}
	}
}