From b7d74ffb4c50edb08b19b90180b01693d6a3c618 Mon Sep 17 00:00:00 2001 From: mcmlxxix <> Date: Mon, 1 Feb 2010 20:15:58 +0000 Subject: [PATCH] Added file synchronization functions. Fixed some bugs. Created some bugs. --- exec/load/commclient.js | 189 ++++++++++++++++++++++++++++++++++++---- 1 file changed, 173 insertions(+), 16 deletions(-) diff --git a/exec/load/commclient.js b/exec/load/commclient.js index deb0e99c60..ad090bb0df 100644 --- a/exec/load/commclient.js +++ b/exec/load/commclient.js @@ -7,40 +7,54 @@ load("funclib.js"); load("sbbsdefs.js"); load("sockdefs.js"); - -const normal_scope= "#"; -const global_scope= "!"; -const priv_scope= "%"; +//load("commsync.js"); + +const normal_scope= "#"; +const global_scope= "!"; +const hub_query= "?"; +const priv_scope= "%"; +const file_sync= "@"; +const tries= 5; +const connection_timeout= 2; function GameConnection(id) { this.session_id= (id?id:"default"); + this.notices= []; - //hub should point to the local, internal ip address or domain of the computer running gameservice.js - //port should point to the port the gaming service is set to use in gameservice.js + var comminit=new File(system.ctrl_dir + "commsync.ini"); + comminit.open('r'); + var module=comminit.iniGetObject(this.session_id); + comminit.close(); + + const working_dir=module.working_directory; + + //hub should point to the local, internal ip address or domain of the computer running commservice.js + //port should point to the port the gaming service is set to use in commservice.js const hub= "localhost"; const port= 10088; - const tries= 5; - const connection_timeout= 2; var sock=new Socket(); sock.bind(0,server.interface_ip_address); this.init=function() { - log("initializing communication service connection"); + this.notices.push("Connecting to hub... "); for(var t=0;t<tries;t++) { sock.connect(hub,port,connection_timeout); if(sock.is_connected) { sock.send("&" + this.session_id + "\r\n"); - log("connection to " + hub + " successful"); return true; } - mswait(50); } - log("connection to " + hub + " failed with error " + sock.last_error); + this.notices.push("Connection to " + hub + " failed..."); + return false; + } + this.getnotices=function() + { + if(this.notices.length) return this.notices; return false; } this.receive=function() @@ -48,12 +62,11 @@ function GameConnection(id) var data=0; if(!sock.is_connected) { - log("connecting to hub"); if(!this.init()) return -1; } if(sock.data_waiting) { - var raw_data=sock.recvline(4092,connection_timeout); + var raw_data=sock.recvline(16384,connection_timeout); data=js.eval(raw_data); } return data; @@ -62,7 +75,6 @@ function GameConnection(id) { if(!sock.is_connected) { - log("connecting to hub"); if(!this.init()) return -1; } if(!data.scope) @@ -70,9 +82,154 @@ function GameConnection(id) log("scope undefined"); return -1; } - var packet=data.scope + this.session_id + ":" + data.toSource() + "\r\n"; + var packet=data.scope + this.session_id + "" + data.toSource() + "\r\n"; sock.send(packet); } + this.recvfile=function(remote_file) + { + var filesock=new Socket(); + filesock.bind(0,server.interface_ip_address); + filesock.connect(hub,port,connection_timeout); + + if(filesock.is_connected) + { + filesock.send(file_sync + this.session_id + "#send" + file_getname(remote_file) + "\r\n"); + while(filesock.is_connected) + { + var data=filesock.recvline(1024,connection_timeout); + data=parse_query(data); + var response=data[1]; + var file=data[2]; + var date=data[3]; + + switch(response) + { + case "#askrecv": + receive_file(filesock,this.session_id,file,date); + break; + case "#abort": + filesock.close(); + return false; + case "#endquery": + filesock.close(); + return true; + default: + filesock.close(); + log("unknown response: " + response); + return false; + } + } + } + } + this.sendfile=function(local_file) + { + var filesock=new Socket(); + filesock.bind(0,server.interface_ip_address); + filesock.connect(hub,port,connection_timeout); + + var files=directory(working_dir + file_getname(local_file)); + for(var f=0;f<files.length && filesock.is_connected;f++) + { + var filename=file_getname(files[f]); + var filedate=file_date(files[f]); + + filesock.send(file_sync + this.session_id + "#askrecv" + filename + "" + filedate + "\r\n"); + var data=filesock.recvline(1024,connection_timeout); + data=parse_query(data); + var response=data[1]; + + switch(response) + { + case "#ok": + log("sending file: " + filename); + filesock.sendfile(files[f]); + filesock.send("#eof\r\n"); + log("file sent: " + filename); + break; + case "#skip": + log("skipping file"); + break; + case "#abort": + log("aborting query"); + return false; + default: + log("unknown response: " + response); + filesock.send("#abort\r\n"); + return false; + } + } + filesock.send("#endquery\r\n"); + filesock.close(); + } + this.close=function() + { + log("terminating service connection"); + sock.close(); + } + + function receive_file(socket,session_id,filename,filedate) + { + filename=working_dir+filename; + if(file_date(filename)==filedate) + { + log("skipping file"); + socket.send(file_sync + session_id + "#skip\r\n"); + return false; + } + + log("confirming transfer"); + socket.send(file_sync + session_id + "#ok\r\n"); + var file=new File(filename + ".tmp"); + file.open('w',false); + + var count=0; + while(count<50) + { + var data=false; + if(socket.data_waiting) data=socket.recvline(1024,connection_timeout); + if(data) + { + switch(data) + { + case "#abort": + log("transfer aborted"); + file.close(); + file_remove(file.name); + return false; + case "#eof": + log("file received: " + filename); + file.close(); + file_utime(file.name,time(),filedate); + file_remove(filename); + file_rename(file.name,filename); + return true; + default: + file.writeln(data); + break; + + } + } + else + { + count++; + mswait(25); + } + } + log("transfer timed out"); + file.close(); + file_remove(file.name); + return false; + } + function parse_query(query) + { + if(query) return query.split(""); + return false; + } + + if(!sock.is_connected) + { + if(!this.init()) return -1; + } } -- GitLab