Debian Codegraph @

extra/haxelib_src/hx3compat/std/neko/net/ThreadRemotingServer.hx

raw
1 /*
2 * Copyright (C)2005-2018 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
24 class ThreadRemotingServer extends ThreadServer<haxe.remoting.SocketConnection,String> {
25
26 var domains : Array<String>;
27 var port : Int;
28
29 public function new( ?domains ) {
30 super();
31 messageHeaderSize = 2;
32 this.domains = domains;
33 }
34
35 public dynamic function initClientApi( cnx : haxe.remoting.SocketConnection, ctx : haxe.remoting.Context ) {
36 throw "Not implemented";
37 }
38
39 public dynamic function onXml( cnx : haxe.remoting.SocketConnection, data : String ) {
40 throw "Unhandled XML data '"+data+"'";
41 }
42
43 public dynamic function makePolicyFile() {
44 var str = "<cross-domain-policy>";
45 for( d in domains )
46 str += '<allow-access-from domain="'+d+'" to-ports="'+port+'"/>';
47 str += "</cross-domain-policy>";
48 return str;
49 }
50
51 public override function run( host, port ) {
52 this.port = port;
53 super.run(host,port);
54 }
55
56 public override function clientConnected( s : sys.net.Socket ) {
57 var ctx = new haxe.remoting.Context();
58 var cnx = haxe.remoting.SocketConnection.create(s,ctx);
59 var me = this;
60 cnx.setErrorHandler(function(e) {
61 if( !Std.is(e,haxe.io.Eof) && !Std.is(e,haxe.io.Error) )
62 me.logError(e);
63 me.stopClient(s);
64 });
65 initClientApi(cnx,ctx);
66 return cnx;
67 }
68
69 override function readClientMessage( cnx : haxe.remoting.SocketConnection, buf : haxe.io.Bytes, pos : Int, len : Int ) {
70 var msgLen = cnx.getProtocol().messageLength(buf.get(pos),buf.get(pos+1));
71 if( msgLen == null ) {
72 if( buf.get(pos) != 60 )
73 throw "Invalid remoting message '"+buf.getString(pos,len)+"'";
74 var p = pos;
75 while( p < len ) {
76 if( buf.get(p) == 0 )
77 break;
78 p++;
79 }
80 if( p == len )
81 return null;
82 p -= pos;
83 return {
84 msg : buf.getString(pos,p),
85 bytes : p + 1,
86 };
87 }
88 if( len < msgLen )
89 return null;
90 if( buf.get(pos + msgLen-1) != 0 )
91 throw "Truncated message";
92 return {
93 msg : buf.getString(pos+2,msgLen-3),
94 bytes : msgLen,
95 };
96 }
97
98 public override function clientMessage( cnx : haxe.remoting.SocketConnection, msg : String ) {
99 try {
100 if( msg.charCodeAt(0) == 60 ) {
101 if( domains != null && msg == "<policy-file-request/>" )
102 cnx.getProtocol().socket.write(makePolicyFile()+"\x00");
103 else
104 onXml(cnx,msg);
105 } else
106 cnx.processMessage(msg);
107 } catch( e : Dynamic ) {
108 if( !Std.is(e,haxe.io.Eof) && !Std.is(e,haxe.io.Error) )
109 logError(e);
110 stopClient(cnx.getProtocol().socket);
111 }
112 }
113
114 }