PageRenderTime 104ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 0ms

/extensions/xultris/content/xultris-netplay.js

https://bitbucket.org/activestate/komodo-3rdparty
JavaScript | 321 lines | 167 code | 40 blank | 114 comment | 22 complexity | a6b9b22a7aee091aedbae07bbe1efe98 MD5 | raw file
Possible License(s): LGPL-2.1, MPL-2.0-no-copyleft-exception
  1. function NetPlayer()
  2. {
  3. this.netPort = 2778;
  4. this.connected = false;
  5. this.waiting = false;
  6. this.serverSocket = null;
  7. this.win = window;
  8. this.lastHost = "";
  9. this.instream = null;
  10. this.outstream = null;
  11. this.dataListener = {
  12. onStartRequest: function(request, context) {},
  13. onStopRequest: function(request, context, status)
  14. {
  15. if (!netPlayer) return;
  16. netPlayer.disconnect();
  17. },
  18. onDataAvailable: function(request, context, inputStream, offset, count)
  19. {
  20. if (!netPlayer) return;
  21. var data = netPlayer.instream.read(count);
  22. var command = data.substr(0,4);
  23. var args = data.substr(5);
  24. if (command == 'HELO')
  25. {
  26. // TODO version control here
  27. netPlayer.doneHandshake();
  28. }
  29. else if (command == 'LINE')
  30. {
  31. addJunk(args);
  32. }
  33. else if (command == 'UWIN')
  34. {
  35. gameOverMan(true);
  36. }
  37. else if (command == 'PAUS')
  38. {
  39. pause(false, true);
  40. }
  41. else if (command == 'UNPS')
  42. {
  43. pause(true, true);
  44. }
  45. }
  46. };
  47. this.listener =
  48. {
  49. onSocketAccepted : function(socket, transport)
  50. {
  51. var stream;
  52. try
  53. {
  54. netPlayer.outstream = transport.openOutputStream(0,0,0);
  55. stream = transport.openInputStream(0,0,0);
  56. netPlayer.instream = Components.classes["@mozilla.org/scriptableinputstream;1"].createInstance(Components.interfaces.nsIScriptableInputStream)
  57. netPlayer.instream.init(stream);
  58. netPlayer.serverSocket.close();
  59. netPlayer.startHandshake();
  60. }
  61. catch(e)
  62. {
  63. netPlayer.win.alert(e);
  64. }
  65. var pump = Components. classes["@mozilla.org/network/input-stream-pump;1"].createInstance(Components.interfaces.nsIInputStreamPump);
  66. pump.init(stream, -1, -1, 0, 0, false);
  67. pump.asyncRead(netPlayer.dataListener,netPlayer);
  68. },
  69. onStopListening : function(socket, status) {}
  70. };
  71. this.init = function()
  72. {
  73. this.connected = false;
  74. this.waiting = false;
  75. this.lastHost = "";
  76. }
  77. this.isConnected = function()
  78. {
  79. return this.connected;
  80. }
  81. this.isWaiting = function()
  82. {
  83. return this.waiting;
  84. }
  85. this.startClient = function(host)
  86. {
  87. this.lastHost = host;
  88. try
  89. {
  90. var transportService = Components.classes["@mozilla.org/network/socket-transport-service;1"].getService(Components.interfaces.nsISocketTransportService);
  91. var transport = transportService.createTransport(null,0,host,this.netPort,null);
  92. this.outstream = transport.openOutputStream(0,0,0);
  93. this.startHandshake();
  94. var stream = transport.openInputStream(0,0,0);
  95. this.instream = Components.classes["@mozilla.org/scriptableinputstream;1"].createInstance(Components.interfaces.nsIScriptableInputStream);
  96. this.instream.init(stream);
  97. var pump = Components.classes["@mozilla.org/network/input-stream-pump;1"].createInstance(Components.interfaces.nsIInputStreamPump);
  98. pump.init(stream, -1, -1, 0, 0, false);
  99. pump.asyncRead(this.dataListener,netPlayer);
  100. return true;
  101. }
  102. catch (e)
  103. {
  104. return false;
  105. }
  106. }
  107. this.startServer = function()
  108. {
  109. this.waiting = true;
  110. this.connected = false;
  111. try
  112. {
  113. this.serverSocket = Components.classes["@mozilla.org/network/server-socket;1"].createInstance(Components.interfaces.nsIServerSocket);
  114. this.serverSocket.init(this.netPort,false,-1);
  115. this.serverSocket.asyncListen(this.listener);
  116. return true;
  117. }
  118. catch (e)
  119. {
  120. this.waiting = false;
  121. return false;
  122. }
  123. }
  124. this.writeMessage = function(message)
  125. {
  126. if (!this.outstream) return;
  127. this.outstream.write(message+"\n", message.length+1);
  128. }
  129. this.startHandshake = function()
  130. {
  131. this.writeMessage("HELO " + version);
  132. }
  133. this.doneHandshake = function()
  134. {
  135. this.connected = true;
  136. this.waiting = false;
  137. this.win.close();
  138. }
  139. this.disconnect = function()
  140. {
  141. if (this.outstream) this.outstream.close();
  142. if (this.instream) this.instream.close();
  143. if (this.serverSocket) this.serverSocket.close();
  144. this.waiting = false;
  145. this.connected = false;
  146. if (this.win.document && this.win.document.getElementById("connected"))
  147. {
  148. this.win.document.getElementById("connected").setAttribute("style","display: hidden;");
  149. }
  150. }
  151. this.pushPause = function()
  152. {
  153. this.writeMessage("PAUS");
  154. }
  155. this.pushUnpause = function()
  156. {
  157. this.writeMessage("UNPS");
  158. }
  159. this.pushWin = function()
  160. {
  161. this.writeMessage("UWIN");
  162. this.disconnect();
  163. }
  164. this.pushLines = function(numLines)
  165. {
  166. this.writeMessage("LINE " + numLines);
  167. }
  168. }
  169. /*
  170. function clientStart() {
  171. mDump("Starting client");
  172. p2ip=window.prompt("Please enter Player 2's IP address:");
  173. if (p2ip==null) return;
  174. netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
  175. gameOverMan();
  176. setInfoText("Connecting...");
  177. theSocket = new Socket();
  178. theSocket.open(p2ip,netPort); // doesn't mean it worked!
  179. mDump("Connection with "+p2ip+":"+netPort+" opened");
  180. //try {
  181. // theSocket.async(window.process);
  182. //theSocket.write("<xultris:xultris protocol='xultris-1.0'>\n");
  183. netplayWrite("<xultris:xultris protocol='xultris-2.2'>");
  184. var hs1=theSocket.read(netTimeOut);
  185. mDump("in: "+hs1);
  186. if (hs1=="") {
  187. alert("Couldn't open connection with "+p2ip+":"+netPort);
  188. xultrisSocketCancelAll();
  189. return;
  190. }
  191. setInfoText("Connected!!!");
  192. if (hs1.indexOf("<xultris:xultris")!=0) {
  193. alert("Unknown protocol! ("+hs1+")");
  194. xultrisSocketCancelAll();
  195. return;
  196. }
  197. hs1=hs1.substring(27,38);
  198. if (hs1!="xultris-1.0") {
  199. alert("Incompatible protocol version ("+hs1+")");
  200. xultrisSocketCancelAll();
  201. return;
  202. }
  203. //} catch (ex) {
  204. // dump("An unknown socket error occured\n");
  205. //}
  206. document.getElementById("connected").setAttribute("style","display: block;");
  207. is2player=true;
  208. newGame();
  209. }
  210. function xultrisSocketError() {
  211. alert("An error occured while connecting to "+p2ip+":"+netPort);
  212. }
  213. function xultrisSocketCancelAll() {
  214. clearInfoText();
  215. }
  216. function netplayTransferLines(num) {
  217. //dump("sending netplay lines: "+num+"\n");
  218. var theHole=Math.floor(Math.random()*widthofgrid)
  219. //theSocket.write("<xultris:lines quantity='"+num+"' hole='"+theHole+"'/>\n");
  220. netplayWrite("<xultris:lines quantity='"+num+"' hole='"+theHole+"'/>");
  221. }
  222. function netplayReadLine() {
  223. var thisLine;
  224. var tl;
  225. if (((tl=theSocket.read(netTimeOut))!=null)&&(tl!="")) {
  226. thisLine = tl;
  227. }
  228. else
  229. {
  230. }
  231. else
  232. {
  233. return;
  234. }
  235. mDump("in: "+thisLine);
  236. if (thisLine.indexOf("<xultris:lines")==0) {
  237. netplayAddLines(thisLine.substring(25,26),thisLine.substring(34,35));
  238. } else if (thisLine.indexOf("<xultris:pause/>")==0) {
  239. pause();
  240. } else if (thisLine.indexOf("<xultris:unpause/>")==0) {
  241. pause(true);
  242. } else if (thisLine.indexOf("<xultris:youwin/>")==0) {
  243. gameOverMan(true);
  244. } else if (thisLine.indexOf("</xultris:xultris>")==0) {
  245. theSocket.close();
  246. }
  247. }
  248. function netplayHandshake() {
  249. netplayWrite("<xultris:xultris protocol='xultris-2.2'>");
  250. }
  251. function netplayDisconnect() {
  252. //theSocket.write("</xultris:xultris>\n");
  253. netplayWrite("</xultris:xultris>");
  254. document.getElementById("connected").setAttribute("style","");
  255. theSocket.close();
  256. }
  257. function netplayWrite(str) {
  258. //if (!is2player) return;
  259. if (theSocket)
  260. {
  261. theSocket.write(str+"\n");
  262. }
  263. else if (outstream)
  264. {
  265. outstream.write(str+"\n", str.length+1);
  266. }
  267. if (debug) mDump ("out: "+str);
  268. }
  269. */