Main Page   Namespace List   Class Hierarchy   Compound List   File List   Compound Members   File Members  

netobject_channel_generic.cpp

Go to the documentation of this file.
00001 /*
00002         $Id: netobject_channel_generic.cpp,v 1.3 2001/02/20 03:50:46 plasmoid Exp $
00003 
00004         ------------------------------------------------------------------------
00005         ClanLib, the platform independent game SDK.
00006 
00007         This library is distributed under the GNU LIBRARY GENERAL PUBLIC LICENSE
00008         version 2. See COPYING for details.
00009 
00010         For a total list of contributers see CREDITS.
00011 
00012         ------------------------------------------------------------------------
00013 */
00014 
00015 #ifdef WIN32
00016 #pragma warning (disable:4786)
00017 #endif
00018 
00019 #include "API/Core/IOData/inputsource_memory.h"
00020 #include "API/Core/IOData/outputsource_memory.h"
00021 #include "API/Core/System/system.h"
00022 #include "API/Network/netsession.h"
00023 #include "API/Network/netmessage.h"
00024 #include "API/Network/netcomputer.h"
00025 #include "API/Network/netgroup.h"
00026 #include "netobject_channel_generic.h"
00027 #include "netobject_generic.h"
00028 
00029 CL_NetObjectChannel_Generic::CL_NetObjectChannel_Generic(
00030         class CL_NetSession *_netgame,
00031         int _channel,
00032         CL_NetObjectChannel *_owner)
00033 : ref(0), id_counter(0), channel(_channel), netgame(_netgame), owner(_owner)
00034 {
00035 }
00036 
00037 CL_NetObjectChannel_Generic::~CL_NetObjectChannel_Generic()
00038 {
00039 }
00040 
00041 int CL_NetObjectChannel_Generic::add_ref()
00042 {
00043         return ++ref;
00044 }
00045 
00046 int CL_NetObjectChannel_Generic::release_ref()
00047 {
00048         return --ref;
00049 }
00050 
00051 void CL_NetObjectChannel_Generic::send(int obj_id, int msg_type, const std::string &message)
00052 {
00053         CL_OutputSource_Memory output;
00054         output.write_int32(obj_id);
00055         output.write_int32(msg_type);
00056         output.write(message.data(), message.size());
00057 
00058         netgame->send(channel, netgame->get_all(), CL_NetMessage(output.get_data()));
00059 }
00060 
00061 void CL_NetObjectChannel_Generic::talkback(int obj_id, int talkback_type, const std::string &message)
00062 {
00063         CL_OutputSource_Memory output;
00064         output.write_int32(obj_id);
00065         output.write_int32(talkback_type);
00066         output.write(message.data(), message.size());
00067 
00068         netgame->send(channel, netgame->get_server(), CL_NetMessage(output.get_data()));
00069 }
00070 
00071 void CL_NetObjectChannel_Generic::begin_sync(const CL_NetGroup *group)
00072 {
00073 }
00074 
00075 void CL_NetObjectChannel_Generic::end_sync(const CL_NetGroup *group)
00076 {
00077         CL_OutputSource_Memory output;
00078         output.write_int32(-1);
00079 
00080         netgame->send(channel, *group, CL_NetMessage(output.get_data()));
00081 }
00082 
00083 bool CL_NetObjectChannel_Generic::wait_sync(int timeout)
00084 {
00085         int start_time = (int)CL_System::get_time();
00086 
00087         while (true)
00088         {
00089                 CL_System::keep_alive();
00090                 if (!received_sync.empty())
00091                 {
00092                         received_sync.pop();
00093                         return true;
00094                 }
00095 
00096                 if (timeout != -1) continue;
00097                 if ((int)CL_System::get_time() -start_time - timeout >= 0) break;
00098         }
00099 
00100         return false;
00101 }
00102 
00103 void CL_NetObjectChannel_Generic::keep_alive()
00104 {
00105         while (netgame->peek(channel))
00106         {
00107                 CL_NetMessage netmsg = netgame->receive(channel, 0);
00108                 CL_InputSource_Memory input(netmsg.data);
00109                 int obj_id = input.read_int32();
00110 
00111                 if (obj_id == -1 && netmsg.data.size() == sizeof(int)) // sync message
00112                 {
00113                         received_sync.push(true);
00114                         continue;
00115                 }
00116 
00117                 int msg_type = input.read_int32();
00118 
00119                 if (netmsg.from == netgame->get_server()) // message received from server:
00120                 {
00121                         std::map<int, CL_NetObject_Generic*>::iterator it = objects.find(obj_id);
00122                         if (it == objects.end()) // object not found, create a new one.
00123                         {
00124                                 sig_create_object(CL_NetObject(obj_id, owner), msg_type, input.get_data().substr(8));
00125                                 continue;
00126                         }
00127 
00128                         it->second->msg_signals[msg_type](input);
00129                 }
00130                 else // message received from client (talkback)
00131                 {
00132                         std::map<int, CL_NetObject_Generic*>::iterator it = objects.find(obj_id);
00133                         if (it == objects.end()) // object not found, ignore message
00134                                 continue;
00135 
00136                         it->second->talkback_signals[msg_type](netmsg.from, input);
00137                 }
00138         }
00139 }

Generated at Wed Apr 4 19:54:02 2001 for ClanLib by doxygen1.2.6 written by Dimitri van Heesch, © 1997-2001