/* ----------------------------------------------------------------------------- This source file is part of OpenSpace3D For the latest info, see http://www.openspace3d.com Copyright (c) 2012 I-maginer 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 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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA, or go to http://www.gnu.org/copyleft/lesser.txt ----------------------------------------------------------------------------- */ struct PlugConnect = [ CON_instance : PInstance, CON_netcomm : NetComm, CON_host : S, CON_port : I, CON_room : S, CON_bInitReco : I, CON_bReco : I, CON_iWait : I, CON_trmRetry : Timer, CON_state : I ]mkPlugConnect;; proto connect = fun [PlugConnect] I;; var bDebugNetwork = 0;; fun deleteOb(inst, constr)= if (constr.CON_trmRetry == nil) then nil else ( _deltimer constr.CON_trmRetry; set constr.CON_trmRetry = nil; ); netDisconnect constr.CON_netcomm; set constr.CON_state = 0; 0;; fun cbSConnected(inst, netstr, constr)= SendPluginEvent inst "Connected" nil nil; set constr.CON_state = 1; netChangeRoom constr.CON_netcomm constr.CON_room; 0;; fun cbReconnect(trm, constr)= _deltimer constr.CON_trmRetry; set constr.CON_trmRetry = nil; connect constr; 0;; fun connect(constr)= addLogMessage strcatn (strcat (loc "OS3DCONNECT_0011") " ")::constr.CON_host::nil; if (constr.CON_trmRetry == nil) then nil else ( _deltimer constr.CON_trmRetry; set constr.CON_trmRetry = nil; ); //disable auto reconnection set constr.CON_bReco = 0; netSetServer constr.CON_netcomm constr.CON_host constr.CON_port; //no more disconnect callback, apply the default value set constr.CON_bReco = constr.CON_bInitReco; if (set constr.CON_state = (netConnect constr.CON_netcomm)) then nil else if (constr.CON_bReco) then ( addLogMessage strcatn (strcat (loc "OS3DCONNECT_0009") " ")::(itoa constr.CON_iWait)::(loc "OS3DCONNECT_0001")::nil; set constr.CON_trmRetry = _rfltimer _starttimer _channel (constr.CON_iWait * 1000) @cbReconnect constr; 0; ) else ( addLogMessage (loc "OS3DCONNECT_0008"); _DLGMessageBox _channel nil (loc "OS3DCONNECT_0007") (loc "OS3DCONNECT_0005") 0; 0; ); 0;; fun cbClosed(inst, netstr, constr)= set constr.CON_state = 0; SendPluginEvent inst "Disconnected" nil nil; if (!constr.CON_bReco) then nil else ( if (constr.CON_trmRetry == nil) then nil else ( _deltimer constr.CON_trmRetry; set constr.CON_trmRetry = nil; ); addLogMessage strcatn (strcat (loc "OS3DCONNECT_0010") " ")::(itoa constr.CON_iWait)::(loc "OS3DCONNECT_0001")::nil; set constr.CON_trmRetry = _rfltimer _starttimer _channel (constr.CON_iWait * 1000) @cbReconnect constr; ); 0;; fun cbNewUser(inst, netstr, userstr, constr)= if (!bDebugNetwork) then nil else addLogMessage strcatn "New user: "::(netUserGetLogin userstr)::nil; SendPluginEvent inst "New user" (netUserGetLogin userstr) nil; 0;; fun cbDelUser(inst, netstr, userstr, constr)= if (!bDebugNetwork) then nil else addLogMessage strcatn "Del user: "::(netUserGetLogin userstr)::nil; SendPluginEvent inst "Del user" (netUserGetLogin userstr) nil; 0;; fun cbNbUsers(inst, netstr, nb, constr)= SendPluginEvent inst "Nb users" (itoa nb) nil; 0;; fun cbConnect(inst, from, action, param, reply, constr)= if (param == nil) then nil else ( let strfind ":" param 0 -> pos in if (pos == nil) then nil else let substr param 0 pos -> host in let atoi (substr param pos + 1 1024) -> port in if (port <= 1) then nil else ( set constr.CON_host = host; set constr.CON_port = port; if (!bDebugNetwork) then nil else addLogMessage strcatn constr.CON_host::" : "::(itoa constr.CON_port)::nil; ); ); connect constr; 0;; fun cbDisconnect(inst, from, action, param, reply, constr)= if (constr.CON_trmRetry == nil) then nil else ( _deltimer constr.CON_trmRetry; set constr.CON_trmRetry = nil; ); //disable auto reconnection set constr.CON_bReco = 0; netDisconnect constr.CON_netcomm; set constr.CON_state = 0; 0;; fun cbChangeRoom(inst, from, action, param, reply, constr)= set constr.CON_room = param; netChangeRoom constr.CON_netcomm constr.CON_room; 0;; fun cbSetServer(inst, from, action, param, reply, constr)= if (param == nil) then nil else ( let strfind ":" param 0 -> pos in if (pos == nil) then nil else let substr param 0 pos -> host in let atoi (substr param pos + 1 1024) -> port in if (port <= 1) then nil else ( set constr.CON_host = host; set constr.CON_port = port; if (!bDebugNetwork) then nil else addLogMessage strcatn constr.CON_host::" : "::(itoa constr.CON_port)::nil; ); if (!constr.CON_state) then nil else connect constr; ); 0;; fun newOb(inst)= let getPluginInstanceParam inst "host" -> host in let atoi (getPluginInstanceParam inst "port") -> port in let getPluginInstanceParam inst "room" -> room in let atoi (getPluginInstanceParam inst "generateid") -> generateid in let if generateid == nil then 1 else generateid -> generateid in let (getPluginInstanceParam inst "appid") -> appid in let atoi (getPluginInstanceParam inst "autoreco") -> autoreco in let if (autoreco == nil) then 0 else autoreco -> autoreco in let atoi (getPluginInstanceParam inst "retry") -> retry in let if (retry == nil) then 30 else retry -> retry in let atoi (getPluginInstanceParam inst "autostart") -> autostart in let mkPlugConnect [inst netcomOS3D host port room autoreco 0 retry nil 0] -> constr in ( if (!generateid) && ((appid != nil) && ((strlen (strtrim appid)) > 1)) then ( set sAppId = _MD5value appid; if (!bDebugNetwork) then nil else ( addLogMessage strcatn "App ID: "::appid::" > "::sAppId::nil; ); 0; ) else ( getAppId sCurrentScenePath; 0; ); netSetServer constr.CON_netcomm constr.CON_host constr.CON_port; setPluginInstanceCbNetSConnected inst mkfun3 @cbSConnected constr; setPluginInstanceCbNetClosed inst mkfun3 @cbClosed constr; setPluginInstanceCbNetNewUser inst mkfun4 @cbNewUser constr; setPluginInstanceCbNetDelUser inst mkfun4 @cbDelUser constr; setPluginInstanceCbNetNbUsers inst mkfun4 @cbNbUsers constr; PluginRegisterAction inst "Connect" mkfun6 @cbConnect constr; PluginRegisterAction inst "Disconnect" mkfun6 @cbDisconnect constr; PluginRegisterAction inst "Change room" mkfun6 @cbChangeRoom constr; PluginRegisterAction inst "Set server url" mkfun6 @cbSetServer constr; setPluginInstanceCbDel inst mkfun2 @deleteOb constr; if (!autostart) then nil else connect constr; ); 0;; fun IniPlug(file)= PlugRegister @newOb nil; setPluginEditor @dynamicedit; 0;;