PageRenderTime 1695ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 1ms

/node_modules/telehash/node_modules/telehash-cs2a/node_modules/node-forge/js/socket.js

https://github.com/ImyarekRu/Paradox
JavaScript | 344 lines | 185 code | 23 blank | 136 comment | 30 complexity | 381a101407fa37b5c4d639de5a0f49fa MD5 | raw file
Possible License(s): AGPL-1.0, Apache-2.0, BSD-3-Clause, GPL-2.0, GPL-3.0, LGPL-3.0, LGPL-2.1, MIT, 0BSD, JSON, CC-BY-SA-3.0, MPL-2.0-no-copyleft-exception
  1. /**
  2. * Socket implementation that uses flash SocketPool class as a backend.
  3. *
  4. * @author Dave Longley
  5. *
  6. * Copyright (c) 2010-2013 Digital Bazaar, Inc.
  7. */
  8. (function() {
  9. /* ########## Begin module implementation ########## */
  10. function initModule(forge) {
  11. // define net namespace
  12. var net = forge.net = forge.net || {};
  13. // map of flash ID to socket pool
  14. net.socketPools = {};
  15. /**
  16. * Creates a flash socket pool.
  17. *
  18. * @param options:
  19. * flashId: the dom ID for the flash object element.
  20. * policyPort: the default policy port for sockets, 0 to use the
  21. * flash default.
  22. * policyUrl: the default policy file URL for sockets (if provided
  23. * used instead of a policy port).
  24. * msie: true if the browser is msie, false if not.
  25. *
  26. * @return the created socket pool.
  27. */
  28. net.createSocketPool = function(options) {
  29. // set default
  30. options.msie = options.msie || false;
  31. // initialize the flash interface
  32. var spId = options.flashId;
  33. var api = document.getElementById(spId);
  34. api.init({marshallExceptions: !options.msie});
  35. // create socket pool entry
  36. var sp = {
  37. // ID of the socket pool
  38. id: spId,
  39. // flash interface
  40. flashApi: api,
  41. // map of socket ID to sockets
  42. sockets: {},
  43. // default policy port
  44. policyPort: options.policyPort || 0,
  45. // default policy URL
  46. policyUrl: options.policyUrl || null
  47. };
  48. net.socketPools[spId] = sp;
  49. // create event handler, subscribe to flash events
  50. if(options.msie === true) {
  51. sp.handler = function(e) {
  52. if(e.id in sp.sockets) {
  53. // get handler function
  54. var f;
  55. switch(e.type) {
  56. case 'connect':
  57. f = 'connected';
  58. break;
  59. case 'close':
  60. f = 'closed';
  61. break;
  62. case 'socketData':
  63. f = 'data';
  64. break;
  65. default:
  66. f = 'error';
  67. break;
  68. }
  69. /* IE calls javascript on the thread of the external object
  70. that triggered the event (in this case flash) ... which will
  71. either run concurrently with other javascript or pre-empt any
  72. running javascript in the middle of its execution (BAD!) ...
  73. calling setTimeout() will schedule the javascript to run on
  74. the javascript thread and solve this EVIL problem. */
  75. setTimeout(function(){sp.sockets[e.id][f](e);}, 0);
  76. }
  77. };
  78. }
  79. else {
  80. sp.handler = function(e) {
  81. if(e.id in sp.sockets) {
  82. // get handler function
  83. var f;
  84. switch(e.type) {
  85. case 'connect':
  86. f = 'connected';
  87. break;
  88. case 'close':
  89. f = 'closed';
  90. break;
  91. case 'socketData':
  92. f = 'data';
  93. break;
  94. default:
  95. f = 'error';
  96. break;
  97. }
  98. sp.sockets[e.id][f](e);
  99. }
  100. };
  101. }
  102. var handler = 'forge.net.socketPools[\'' + spId + '\'].handler';
  103. api.subscribe('connect', handler);
  104. api.subscribe('close', handler);
  105. api.subscribe('socketData', handler);
  106. api.subscribe('ioError', handler);
  107. api.subscribe('securityError', handler);
  108. /**
  109. * Destroys a socket pool. The socket pool still needs to be cleaned
  110. * up via net.cleanup().
  111. */
  112. sp.destroy = function() {
  113. delete net.socketPools[options.flashId];
  114. for(var id in sp.sockets) {
  115. sp.sockets[id].destroy();
  116. }
  117. sp.sockets = {};
  118. api.cleanup();
  119. };
  120. /**
  121. * Creates a new socket.
  122. *
  123. * @param options:
  124. * connected: function(event) called when the socket connects.
  125. * closed: function(event) called when the socket closes.
  126. * data: function(event) called when socket data has arrived,
  127. * it can be read from the socket using receive().
  128. * error: function(event) called when a socket error occurs.
  129. */
  130. sp.createSocket = function(options) {
  131. // default to empty options
  132. options = options || {};
  133. // create flash socket
  134. var id = api.create();
  135. // create javascript socket wrapper
  136. var socket = {
  137. id: id,
  138. // set handlers
  139. connected: options.connected || function(e){},
  140. closed: options.closed || function(e){},
  141. data: options.data || function(e){},
  142. error: options.error || function(e){}
  143. };
  144. /**
  145. * Destroys this socket.
  146. */
  147. socket.destroy = function() {
  148. api.destroy(id);
  149. delete sp.sockets[id];
  150. };
  151. /**
  152. * Connects this socket.
  153. *
  154. * @param options:
  155. * host: the host to connect to.
  156. * port: the port to connect to.
  157. * policyPort: the policy port to use (if non-default), 0 to
  158. * use the flash default.
  159. * policyUrl: the policy file URL to use (instead of port).
  160. */
  161. socket.connect = function(options) {
  162. // give precedence to policy URL over policy port
  163. // if no policy URL and passed port isn't 0, use default port,
  164. // otherwise use 0 for the port
  165. var policyUrl = options.policyUrl || null;
  166. var policyPort = 0;
  167. if(policyUrl === null && options.policyPort !== 0) {
  168. policyPort = options.policyPort || sp.policyPort;
  169. }
  170. api.connect(id, options.host, options.port, policyPort, policyUrl);
  171. };
  172. /**
  173. * Closes this socket.
  174. */
  175. socket.close = function() {
  176. api.close(id);
  177. socket.closed({
  178. id: socket.id,
  179. type: 'close',
  180. bytesAvailable: 0
  181. });
  182. };
  183. /**
  184. * Determines if the socket is connected or not.
  185. *
  186. * @return true if connected, false if not.
  187. */
  188. socket.isConnected = function() {
  189. return api.isConnected(id);
  190. };
  191. /**
  192. * Writes bytes to this socket.
  193. *
  194. * @param bytes the bytes (as a string) to write.
  195. *
  196. * @return true on success, false on failure.
  197. */
  198. socket.send = function(bytes) {
  199. return api.send(id, forge.util.encode64(bytes));
  200. };
  201. /**
  202. * Reads bytes from this socket (non-blocking). Fewer than the number
  203. * of bytes requested may be read if enough bytes are not available.
  204. *
  205. * This method should be called from the data handler if there are
  206. * enough bytes available. To see how many bytes are available, check
  207. * the 'bytesAvailable' property on the event in the data handler or
  208. * call the bytesAvailable() function on the socket. If the browser is
  209. * msie, then the bytesAvailable() function should be used to avoid
  210. * race conditions. Otherwise, using the property on the data handler's
  211. * event may be quicker.
  212. *
  213. * @param count the maximum number of bytes to read.
  214. *
  215. * @return the bytes read (as a string) or null on error.
  216. */
  217. socket.receive = function(count) {
  218. var rval = api.receive(id, count).rval;
  219. return (rval === null) ? null : forge.util.decode64(rval);
  220. };
  221. /**
  222. * Gets the number of bytes available for receiving on the socket.
  223. *
  224. * @return the number of bytes available for receiving.
  225. */
  226. socket.bytesAvailable = function() {
  227. return api.getBytesAvailable(id);
  228. };
  229. // store and return socket
  230. sp.sockets[id] = socket;
  231. return socket;
  232. };
  233. return sp;
  234. };
  235. /**
  236. * Destroys a flash socket pool.
  237. *
  238. * @param options:
  239. * flashId: the dom ID for the flash object element.
  240. */
  241. net.destroySocketPool = function(options) {
  242. if(options.flashId in net.socketPools) {
  243. var sp = net.socketPools[options.flashId];
  244. sp.destroy();
  245. }
  246. };
  247. /**
  248. * Creates a new socket.
  249. *
  250. * @param options:
  251. * flashId: the dom ID for the flash object element.
  252. * connected: function(event) called when the socket connects.
  253. * closed: function(event) called when the socket closes.
  254. * data: function(event) called when socket data has arrived, it
  255. * can be read from the socket using receive().
  256. * error: function(event) called when a socket error occurs.
  257. *
  258. * @return the created socket.
  259. */
  260. net.createSocket = function(options) {
  261. var socket = null;
  262. if(options.flashId in net.socketPools) {
  263. // get related socket pool
  264. var sp = net.socketPools[options.flashId];
  265. socket = sp.createSocket(options);
  266. }
  267. return socket;
  268. };
  269. } // end module implementation
  270. /* ########## Begin module wrapper ########## */
  271. var name = 'net';
  272. if(typeof define !== 'function') {
  273. // NodeJS -> AMD
  274. if(typeof module === 'object' && module.exports) {
  275. var nodeJS = true;
  276. define = function(ids, factory) {
  277. factory(require, module);
  278. };
  279. }
  280. // <script>
  281. else {
  282. if(typeof forge === 'undefined') {
  283. forge = {};
  284. }
  285. return initModule(forge);
  286. }
  287. }
  288. // AMD
  289. var deps;
  290. var defineFunc = function(require, module) {
  291. module.exports = function(forge) {
  292. var mods = deps.map(function(dep) {
  293. return require(dep);
  294. }).concat(initModule);
  295. // handle circular dependencies
  296. forge = forge || {};
  297. forge.defined = forge.defined || {};
  298. if(forge.defined[name]) {
  299. return forge[name];
  300. }
  301. forge.defined[name] = true;
  302. for(var i = 0; i < mods.length; ++i) {
  303. mods[i](forge);
  304. }
  305. return forge[name];
  306. };
  307. };
  308. var tmpDefine = define;
  309. define = function(ids, factory) {
  310. deps = (typeof ids === 'string') ? factory.slice(2) : ids.slice(2);
  311. if(nodeJS) {
  312. delete define;
  313. return tmpDefine.apply(null, Array.prototype.slice.call(arguments, 0));
  314. }
  315. define = tmpDefine;
  316. return define.apply(null, Array.prototype.slice.call(arguments, 0));
  317. };
  318. define(['require', 'module'], function() {
  319. defineFunc.apply(null, Array.prototype.slice.call(arguments, 0));
  320. });
  321. })();