ThreadRemotingServer.hx 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. /*
  2. * Copyright (C)2005-2012 Haxe Foundation
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a
  5. * copy of this software and associated documentation files (the "Software"),
  6. * to deal in the Software without restriction, including without limitation
  7. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  8. * and/or sell copies of the Software, and to permit persons to whom the
  9. * Software is furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice shall be included in
  12. * all copies or substantial portions of the Software.
  13. *
  14. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  19. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  20. * DEALINGS IN THE SOFTWARE.
  21. */
  22. package neko.net;
  23. class ThreadRemotingServer extends ThreadServer<haxe.remoting.SocketConnection,String> {
  24. var domains : Array<String>;
  25. var port : Int;
  26. public function new( ?domains ) {
  27. super();
  28. messageHeaderSize = 2;
  29. this.domains = domains;
  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 dynamic function makePolicyFile() {
  38. var str = "<cross-domain-policy>";
  39. for( d in domains )
  40. str += '<allow-access-from domain="'+d+'" to-ports="'+port+'"/>';
  41. str += "</cross-domain-policy>";
  42. return str;
  43. }
  44. public override function run( host, port ) {
  45. this.port = port;
  46. super.run(host,port);
  47. }
  48. public override function clientConnected( s : sys.net.Socket ) {
  49. var ctx = new haxe.remoting.Context();
  50. var cnx = haxe.remoting.SocketConnection.create(s,ctx);
  51. var me = this;
  52. cnx.setErrorHandler(function(e) {
  53. if( !Std.is(e,haxe.io.Eof) && !Std.is(e,haxe.io.Error) )
  54. me.logError(e);
  55. me.stopClient(s);
  56. });
  57. initClientApi(cnx,ctx);
  58. return cnx;
  59. }
  60. override function readClientMessage( cnx : haxe.remoting.SocketConnection, buf : haxe.io.Bytes, pos : Int, len : Int ) {
  61. var msgLen = cnx.getProtocol().messageLength(buf.get(pos),buf.get(pos+1));
  62. if( msgLen == null ) {
  63. if( buf.get(pos) != 60 )
  64. throw "Invalid remoting message '"+buf.getString(pos,len)+"'";
  65. var p = pos;
  66. while( p < len ) {
  67. if( buf.get(p) == 0 )
  68. break;
  69. p++;
  70. }
  71. if( p == len )
  72. return null;
  73. p -= pos;
  74. return {
  75. msg : buf.getString(pos,p),
  76. bytes : p + 1,
  77. };
  78. }
  79. if( len < msgLen )
  80. return null;
  81. if( buf.get(pos + msgLen-1) != 0 )
  82. throw "Truncated message";
  83. return {
  84. msg : buf.getString(pos+2,msgLen-3),
  85. bytes : msgLen,
  86. };
  87. }
  88. public override function clientMessage( cnx : haxe.remoting.SocketConnection, msg : String ) {
  89. try {
  90. if( msg.charCodeAt(0) == 60 ) {
  91. if( domains != null && msg == "<policy-file-request/>" )
  92. cnx.getProtocol().socket.write(makePolicyFile()+"\x00");
  93. else
  94. onXml(cnx,msg);
  95. } else
  96. cnx.processMessage(msg);
  97. } catch( e : Dynamic ) {
  98. if( !Std.is(e,haxe.io.Eof) && !Std.is(e,haxe.io.Error) )
  99. logError(e);
  100. stopClient(cnx.getProtocol().socket);
  101. }
  102. }
  103. }