ThreadRemotingServer.hx 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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 dynamic function initClientApi( cnx : haxe.remoting.SocketConnection, ctx : haxe.remoting.Context ) {
  32. throw "Not implemented";
  33. }
  34. public dynamic function onXml( cnx : haxe.remoting.SocketConnection, data : String ) {
  35. throw "Unhandled XML data '"+data+"'";
  36. }
  37. public override function clientConnected( s : neko.net.Socket ) {
  38. var ctx = new haxe.remoting.Context();
  39. var cnx = haxe.remoting.SocketConnection.create(s,ctx);
  40. var me = this;
  41. cnx.setErrorHandler(function(e) {
  42. if( !Std.is(e,haxe.io.Eof) && !Std.is(e,haxe.io.Error) )
  43. me.logError(e);
  44. me.stopClient(s);
  45. });
  46. initClientApi(cnx,ctx);
  47. return cnx;
  48. }
  49. override function readClientMessage( cnx : haxe.remoting.SocketConnection, buf : haxe.io.Bytes, pos : Int, len : Int ) {
  50. var msgLen = cnx.getProtocol().messageLength(buf.get(pos),buf.get(pos+1));
  51. if( msgLen == null ) {
  52. if( buf.get(pos) != 60 )
  53. throw "Invalid remoting message '"+buf.readString(pos,len)+"'";
  54. var p = pos;
  55. while( p < len ) {
  56. if( buf.get(p) == 0 )
  57. break;
  58. p++;
  59. }
  60. if( p == len )
  61. return null;
  62. p -= pos;
  63. return {
  64. msg : buf.readString(pos,p),
  65. bytes : p + 1,
  66. };
  67. }
  68. if( len < msgLen )
  69. return null;
  70. if( buf.get(pos + msgLen-1) != 0 )
  71. throw "Truncated message";
  72. return {
  73. msg : buf.readString(pos+2,msgLen-3),
  74. bytes : msgLen,
  75. };
  76. }
  77. public override function clientMessage( cnx : haxe.remoting.SocketConnection, msg : String ) {
  78. if( msg.charCodeAt(0) == 60 ) {
  79. onXml(cnx,msg);
  80. return;
  81. }
  82. try {
  83. cnx.processMessage(msg);
  84. } catch( e : Dynamic ) {
  85. if( !Std.is(e,haxe.io.Eof) && !Std.is(e,haxe.io.Error) )
  86. logError(e);
  87. stopClient(cnx.getProtocol().socket);
  88. }
  89. }
  90. }