ThreadRemotingServer.hx 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. /*
  2. * Copyright (c) 2005, The haXe Project Contributors
  3. * All rights reserved.
  4. * Redistribution and use in source and binary forms, with or without
  5. * modification, are permitted provided that the following conditions are met:
  6. *
  7. * - Redistributions of source code must retain the above copyright
  8. * notice, this list of conditions and the following disclaimer.
  9. * - Redistributions in binary form must reproduce the above copyright
  10. * notice, this list of conditions and the following disclaimer in the
  11. * documentation and/or other materials provided with the distribution.
  12. *
  13. * THIS SOFTWARE IS PROVIDED BY THE HAXE PROJECT CONTRIBUTORS "AS IS" AND ANY
  14. * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  15. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  16. * DISCLAIMED. IN NO EVENT SHALL THE HAXE PROJECT CONTRIBUTORS BE LIABLE FOR
  17. * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  18. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  19. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  20. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  21. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  22. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
  23. * DAMAGE.
  24. */
  25. package neko.net;
  26. class ThreadRemotingServer extends ThreadServer<haxe.remoting.SocketConnection,String> {
  27. public function new() {
  28. super();
  29. messageHeaderSize = 2;
  30. }
  31. public function newClientApi() {
  32. throw "Not implemented";
  33. return null;
  34. }
  35. function decodeChar(c) {
  36. // A...Z
  37. if( c >= 65 && c <= 90 )
  38. return c - 65;
  39. // a...z
  40. if( c >= 97 && c <= 122 )
  41. return c - 97 + 26;
  42. // 0...9
  43. if( c >= 48 && c <= 57 )
  44. return c - 48 + 52;
  45. // +
  46. if( c == 43 )
  47. return 62;
  48. // /
  49. if( c == 47 )
  50. return 63;
  51. return null;
  52. }
  53. public override function clientConnected( s : neko.net.Socket ) {
  54. return haxe.remoting.SocketConnection.socketConnect(s,newClientApi());
  55. }
  56. public override function readClientMessage( cnx, buf : String, pos : Int, len : Int ) {
  57. var c1 = decodeChar(buf.charCodeAt(pos));
  58. var c2 = decodeChar(buf.charCodeAt(pos+1));
  59. var msgLen = (c1 << 6) | c2;
  60. if( len < msgLen )
  61. return null;
  62. if( buf.charCodeAt(msgLen-1) != 0 )
  63. throw "Truncated message";
  64. return {
  65. msg : buf.substr(pos+2,msgLen-3),
  66. bytes : msgLen,
  67. };
  68. }
  69. public override function clientMessage( cnx, msg : String ) {
  70. var r = haxe.remoting.SocketConnection.processMessage(cnx,msg);
  71. if( r != null )
  72. neko.Lib.rethrow(r.exc);
  73. }
  74. }