/* ----------------------------------------------------------------------------- This source file is part of OpenSpace3D For the latest info, see http://www.openspace3d.com Copyright (c) 2018 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 PlugTCPsrv = [ PTS_inst : PInstance, PTS_srv : TCPSRV, PTS_iPort : I, PTS_bEnable : I, PTS_bBroadcast : I, PTS_lCon : [TCPCON r1], PTS_lKeywords : [S r1] ]mkPlugTCPsrv;; fun cbSendMessage(inst, from, action, param, rep, obstr, keyword)= let obstr.PTS_lCon -> l in let strcatn keyword::" "::param::"\n"::nil -> mess in while(l != nil) do ( TCPsend (hd l) mess; set l = tl l; ); 0;; fun cbTcpRequest(con, obstr, req)= if (req == nil) || (!strcmp req "") then nil else ( if (!obstr.PTS_bBroadcast) then nil else ( let obstr.PTS_lCon -> l in while(l != nil) do ( let (hd l) -> ncon in if (ncon == con) then nil else TCPsend ncon req; set l = tl l; ); ); let strlen req -> echolen in let obstr.PTS_lKeywords -> l in let 0 -> found in while (l != nil && !found) do ( let hd l -> key in let strlen key -> keylenght in let substr req keylenght 1 -> sp in if ((!strcmp key (substr req 0 keylenght)) && ((!strcmp sp "") || (!strcmp sp " "))) then ( set found = 1; let substr req (keylenght + 1) (echolen - keylenght) -> np in SendPluginEvent obstr.PTS_inst (strcat key " message") np nil; ) else nil; set l = tl l; ); ); 0;; fun cbTCPconClosed(con, obstr)= set obstr.PTS_lCon = remove_from_list obstr.PTS_lCon con; SendPluginEvent obstr.PTS_inst "Client disconnected" (getTCPclientIP con) nil; SendPluginEvent obstr.PTS_inst "Clients count" itoa (sizelist obstr.PTS_lCon) nil; 0;; fun cbTcpAccept(con, obstr)= set obstr.PTS_lCon = con::obstr.PTS_lCon; rflTCPclose con @cbTCPconClosed obstr; SendPluginEvent obstr.PTS_inst "Client connected" (getTCPclientIP con) nil; SendPluginEvent obstr.PTS_inst "Clients count" itoa (sizelist obstr.PTS_lCon) nil; 0;; fun cbEnable(inst, from, action, param, rep, obstr)= if (obstr.PTS_bEnable) then nil else ( set obstr.PTS_bEnable = 1; set obstr.PTS_srv = startTCPserver _channel obstr.PTS_iPort @cbTcpRequest obstr; rflTCPaccept obstr.PTS_srv @cbTcpAccept obstr; ); 0;; fun cbDisable(inst, from, action, param, rep, obstr)= if (!obstr.PTS_bEnable) then nil else ( set obstr.PTS_bEnable = 0; if (obstr.PTS_srv == nil) then nil else ( while(obstr.PTS_lCon != nil) do ( closeTCPcon (hd obstr.PTS_lCon); set obstr.PTS_lCon = tl obstr.PTS_lCon; ); closeTCPserver obstr.PTS_srv; set obstr.PTS_srv = nil; //SendPluginEvent obstr.PTS_inst "Disconnected" nil nil; ); set obstr.PTS_lCon = nil; ); 0;; fun deleteOb(inst, obstr)= cbDisable inst nil nil nil nil obstr; 0;; fun newOb(inst)= let atoi (getPluginInstanceParam inst "port") -> port in let atoi (getPluginInstanceParam inst "broadcast") -> broadcast in let atoi (getPluginInstanceParam inst "init") -> init in let getPluginInstanceUserEvents inst -> levent in let mkPlugTCPsrv [inst nil port 0 broadcast nil nil] -> obstr in ( while (levent != nil) do ( let hd levent -> evt in let strToWordList evt -> line in let strcatnSep (revertlist tl (revertlist line)) " " -> keyword in ( PluginRegisterAction inst (strcat "Send " keyword) mkfun6 mkfun7 @cbSendMessage keyword obstr; set obstr.PTS_lKeywords = keyword::obstr.PTS_lKeywords; ); set levent = tl levent; ); if !init then nil else cbEnable inst nil nil nil nil obstr; PluginRegisterAction inst "Enable" mkfun6 @cbEnable obstr; PluginRegisterAction inst "Disable" mkfun6 @cbDisable obstr; setPluginInstanceCbDel inst mkfun2 @deleteOb obstr; ); 0;; fun IniPlug(file)= PlugRegister @newOb nil; setPluginEditor @dynamicedit; 0;;