2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License. You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing,
13 * software distributed under the License is distributed on an
14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 * KIND, either express or implied. See the License for the
16 * specific language governing permissions and limitations
20 package org.apache.thrift.transport
23 import flash.events.EventDispatcher;
24 import flash.events.Event;
25 import flash.events.IOErrorEvent;
26 import flash.events.ProgressEvent;
27 import flash.events.SecurityErrorEvent;
28 import flash.errors.EOFError;
29 import flash.errors.IOError;
30 import flash.net.URLLoader;
31 import flash.net.URLLoaderDataFormat;
32 import flash.net.URLRequest;
33 import flash.net.URLRequestMethod;
34 import flash.utils.IDataInput;
35 import flash.utils.IDataOutput;
36 import flash.utils.ByteArray;
37 import flash.net.Socket;
41 * Socket implementation of the TTransport interface. Used for working with a
42 * Thrift Socket Server based implementations.
45 public class TSocket extends TTransport
47 private var socket:Socket = null;
49 private var host:String;
53 private var obuffer:ByteArray = new ByteArray();
55 private var input:IDataInput;
57 private var output:IDataOutput;
59 private var ioCallback:Function = null;
61 private var eventDispatcher:EventDispatcher = new EventDispatcher();
63 public function TSocket(host:String, port:int):void
69 public override function close():void
76 public override function peek():Boolean
80 trace("Bytes remained:" + socket.bytesAvailable);
81 return socket.bytesAvailable>0;
86 public override function read(buf:ByteArray, off:int, len:int):int
88 var n1:int = 0, n2:int = 0, n3:int = 0, n4:int = 0, cidx:int = 2;
89 var chunkSize:ByteArray = new ByteArray();
93 input.readBytes(buf, off, len);
99 throw new TTransportError(TTransportError.END_OF_FILE, "No more data available.");
106 throw new TTransportError(TTransportError.UNKNOWN, "IO error while reading: " + e);
110 throw new TTransportError(TTransportError.NOT_OPEN, "Socket seem not to be opened: " + e);
116 throw new TTransportError(TTransportError.UNKNOWN, "Bad IO error: " + e);
121 public override function write(buf:ByteArray, off:int, len:int):void
123 obuffer.writeBytes(buf, off, len);
126 public function addEventListener(type:String, listener:Function, useCapture:Boolean = false, priority:int = 0, useWeakReference:Boolean = false):void
128 this.eventDispatcher.addEventListener(type, listener, useCapture, priority, useWeakReference);
131 public function removeEventListener(type:String, listener:Function, useCapture:Boolean = false):void
133 this.eventDispatcher.removeEventListener(type, listener, useCapture);
136 public override function open():void
138 this.socket = new Socket();
139 this.socket.addEventListener(Event.CONNECT, socketConnected);
140 this.socket.addEventListener(IOErrorEvent.IO_ERROR, socketError);
141 this.socket.addEventListener(SecurityErrorEvent.SECURITY_ERROR, socketSecurityError);
142 this.socket.addEventListener(ProgressEvent.SOCKET_DATA, socketDataHandler);
143 this.socket.connect(host, port);
146 public function socketConnected(event:Event):void
148 this.output = this.socket;
149 this.input = this.socket;
150 this.eventDispatcher.dispatchEvent(event);
153 public function socketError(event:IOErrorEvent):void
155 trace("Error Connecting:" + event);
157 if (ioCallback == null)
161 ioCallback(new TTransportError(TTransportError.UNKNOWN, "IOError: " + event.text));
162 this.eventDispatcher.dispatchEvent(event);
165 public function socketSecurityError(event:SecurityErrorEvent):void
167 trace("Security Error Connecting:" + event);
169 this.eventDispatcher.dispatchEvent(event);
172 public function socketDataHandler(event:ProgressEvent):void
174 if (ioCallback != null)
178 this.eventDispatcher.dispatchEvent(event);
181 public override function flush(callback:Function = null):void
183 this.ioCallback = callback;
184 this.output.writeBytes(this.obuffer);
186 this.obuffer.clear();
189 public override function isOpen():Boolean
191 return (this.socket == null ? false : this.socket.connected);