1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
|
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.apache.thrift.transport;
#if (cs || neko || cpp || java || macro || lua || php || python || hl)
import sys.net.Socket;
#elseif flash
import flash.net.Socket;
#elseif js
import js.html.WebSocket;
#end
import haxe.io.Bytes;
import haxe.io.BytesBuffer;
import haxe.io.BytesInput;
import haxe.io.BytesOutput;
import haxe.io.Input;
import haxe.io.Output;
import haxe.io.Eof;
import org.apache.thrift.TConfiguration;
#if ! (flash || js)
import sys.net.Host;
#end
/**
* Socket implementation of the TTransport interface. Used for working with a
* Thrift Socket Server based implementations.
*/
class TSocket extends TEndpointTransport {
#if (flash || js)
private var host : String;
#else
private var host : Host;
#end
private var port : Int;
#if js
private var socket : WebSocket = null;
#else
private var socket : Socket = null;
#end
#if js
private var input : Dynamic = null;
private var output : WebSocket = null;
#elseif flash
private var input : Socket = null;
private var output : Socket = null;
#else
private var input : Input = null;
private var output : Output = null;
#end
private var timeout : Float = 30;
private var obuffer : BytesOutput = new BytesOutput();
private var ioCallback : TException->Void = null;
private var readCount : Int = 0;
public function new(host : String, port : Int, config : TConfiguration = null) : Void {
super(config);
#if (flash || js)
this.host = host;
#else
this.host = new Host(host);
#end
this.port = port;
}
#if ! (flash || js)
// used by TSocketServer
public static function fromSocket( socket : Socket) : TSocket {
var socketHost = socket.host();
var result = new TSocket(socketHost.host.toString(), socketHost.port);
result.assignSocket(socket);
return result;
}
#end
public override function close() : Void {
input = null;
output = null;
socket.close();
}
public override function peek() : Bool {
if( (input == null) || (socket == null)) {
return false;
} else {
#if flash
return (input.bytesAvailable > 0);
#elseif js
return true;
#else
var ready = Socket.select( [socket], null, null, 0);
return (ready.read.length > 0);
#end
}
}
// Reads up to len bytes into buffer buf, starting att offset off.
// May return less bytes than len required
public override function read( buf : BytesBuffer, off : Int, len : Int) : Int {
try
{
#if flash
var remaining = len;
while( remaining > 0) {
buf.addByte( input.readByte());
--remaining;
}
CountConsumedMessageBytes(len);
return len;
#elseif js
if( input == null) {
throw new TTransportException(TTransportException.UNKNOWN, "Still no data "); // don't block
}
var nr = len;
while( nr < len) {
buf.addByte( input.get(off+nr));
++nr;
}
CountConsumedMessageBytes(len);
return len;
#else
//socket.waitForRead(); - no, this ignores timeout and blocks infinitely
if(readCount < off) {
input.read(off-readCount);
readCount = off;
}
var data = Bytes.alloc(len);
var got = input.readBytes(data, 0, len);
buf.addBytes( data, 0, got);
readCount += got;
CountConsumedMessageBytes(got);
return got;
#end
}
catch (e : Eof)
{
trace('Eof $e');
throw new TTransportException(TTransportException.END_OF_FILE, "No more data available.");
}
catch (e : TException)
{
trace('TException $e');
throw e;
}
catch (e : Dynamic)
{
trace('Error $e');
throw new TTransportException(TTransportException.UNKNOWN, 'Bad IO error : $e');
}
}
public override function write(buf : Bytes, off : Int, len : Int) : Void
{
obuffer.writeBytes(buf, off, len);
}
public override function flush(callback : Dynamic->Void = null) : Void
{
if( ! isOpen())
{
throw new TTransportException(TTransportException.NOT_OPEN, "Transport not open");
}
#if flash
var bytes = new flash.utils.ByteArray();
var data = obuffer.getBytes();
var len = 0;
while( len < data.length) {
bytes.writeByte(data.get(len));
++len;
}
#elseif js
var data = obuffer.getBytes();
var outbuf = new js.html.Int8Array(data.length);
var len = 0;
while( len < data.length) {
outbuf.set( [data.get(len)], len);
++len;
}
var bytes = outbuf.buffer;
#else
var bytes = obuffer.getBytes();
var len = bytes.length;
#end
obuffer = new BytesOutput();
ResetConsumedMessageSize();
ioCallback = callback;
try {
readCount = 0;
#if js
output.send( bytes);
#else
output.writeBytes( bytes, 0, bytes.length);
#end
if(ioCallback != null) {
ioCallback(null); // success call
}
}
catch (e : TException)
{
trace('TException $e, message : ${e.errorMsg}');
if(ioCallback != null) {
ioCallback(e);
}
}
catch (e : Dynamic) {
trace(e);
if(ioCallback != null) {
ioCallback(new TTransportException(TTransportException.UNKNOWN, 'Bad IO error : $e'));
}
}
}
public override function isOpen() : Bool
{
return (socket != null);
}
public override function open() : Void
{
#if js
var socket = new WebSocket(host);
socket.onmessage = function( event : js.html.MessageEvent) {
this.input = event.data;
}
#elseif flash
var socket = new Socket();
socket.connect(host, port);
#elseif php
var socket = new Socket();
socket.connect(host, port);
socket.setBlocking(true);
socket.setTimeout(timeout);
#else
var socket = new Socket();
socket.setBlocking(true);
socket.setFastSend(true);
socket.setTimeout(timeout);
socket.connect(host, port);
#end
assignSocket( socket);
ResetConsumedMessageSize();
}
#if js
private function assignSocket( socket : WebSocket) : Void
#else
private function assignSocket( socket : Socket) : Void
#end
{
this.socket = socket;
#if (flash || js)
output = socket;
input = socket;
#else
output = socket.output;
input = socket.input;
#end
}
#if (flash)
public function setTimeout( timeout : UInt) : Void {
if(isOpen()) {
socket.timeout = timeout;
}
this.timeout = timeout;
}
#else
public function setTimeout( timeout : Float ) : Void {
if(isOpen()) {
#if ! (js)
socket.setTimeout(timeout);
#end
}
this.timeout = timeout;
}
#end
}
|