PageRenderTime 149ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/ref/Tobba/Socket.js

https://gitlab.com/MaddoScientisto/OpenBYOND
JavaScript | 81 lines | 69 code | 10 blank | 2 comment | 15 complexity | 57bf411fb933c5f11fb5bfeac43c163a MD5 | raw file
  1. DM.Socket = function(host, port) {
  2. this.name = "socket" + Math.floor(Math.random() * 1000000);
  3. var elem = document.createElement('span');
  4. elem.id = this.name;
  5. document.body.appendChild(elem); // TODO: need to put these in some off-screen "socket holder" object
  6. swfobject.embedSWF("DM/socket_bridge.swf", elem, "1", "1", "9.0.0","expressInstall.swf", {'scope' : this.name}, {}, {'allowscriptaccess' : 'always'});
  7. // FIXME: this hex conversion stuff is going to be SLOW
  8. window[this.name] = this;
  9. this.loaded = function() {
  10. this.swf = document.getElementById(this.name);
  11. this.swf.connect(host, String(port));
  12. }
  13. this.connected = function() {
  14. this.onconnect();
  15. }
  16. this.header = new Uint8Array(4);
  17. this.headerLength = 0;
  18. this.activePacket;
  19. this.packetPosition = 0;
  20. this.receive = function(msg) {
  21. var bytes = new Uint8Array(msg.length/2);
  22. for (var i = 0; i < msg.length; i += 2)
  23. bytes[i/2] = parseInt(msg.charAt(i)+msg.charAt(i+1), 16);
  24. var p = 0;
  25. while (p < bytes.length)
  26. {
  27. if (this.headerLength < 4)
  28. {
  29. for (; this.headerLength < 4 && bytes.length-p > 0; this.headerLength++)
  30. this.header[this.headerLength] = bytes[p++];
  31. }
  32. if (this.headerLength >= 4)
  33. {
  34. if (!this.activePacket)
  35. this.activePacket = new DM.Packet(this.header[0] * 0x100 + this.header[1], this.header[2] * 0x100 + this.header[3]);
  36. for (; this.packetPosition < this.activePacket.data.length && bytes.length-p > 0; this.packetPosition++)
  37. this.activePacket.data[this.packetPosition] = bytes[p++];
  38. if (this.packetPosition >= this.activePacket.data.length)
  39. {
  40. var packet = this.activePacket;
  41. this.activePacket = null;
  42. this.headerLength = 0;
  43. this.packetPosition = 0;
  44. this.onreceive(packet); // TODO: wrap handler in try/catch to prevent full breakage
  45. }
  46. }
  47. }
  48. }
  49. // TODO: handle all callbacks
  50. var lookup = {}
  51. for (var i = 0; i < 256; i++)
  52. lookup[i] = ('00'+i.toString(16)).substr(-2);
  53. this.SetSequence = function(seq) {
  54. this.sequence = seq;
  55. }
  56. this.Send = function(packet) {
  57. var msg = ('0000'+packet.id.toString(16)).substr(-4) + ('0000'+packet.data.length.toString(16)).substr(-4);
  58. if (this.sequence)
  59. {
  60. msg = ('0000'+this.sequence.toString(16)).substr(-4) + msg;
  61. this.sequence = (17364 * this.sequence) % 0xFFF1;
  62. if (this.sequence == 0)
  63. this.sequence = 1;
  64. }
  65. for (var i = 0; i < packet.data.length; i++)
  66. msg += lookup[packet.data[i]];
  67. this.swf.write(msg);
  68. }
  69. this.onconnect = function() { }
  70. this.onreceive = function(packet) { }
  71. }