package com.webgame.as3.socket
{
    import com.webgame.as3.events.*;
    import com.webgame.as3.security.*;
    import flash.events.*;
    import flash.net.*;
    import flash.utils.*;

    public class AbstractSocket extends EventDispatcher
    {
        private var port:int;
        public var sock:Socket;
        protected var length:int = -1;
        protected var cmd:int = -1;
        private var host:String;
        private var isConnected:Boolean;
        protected var FSecurity:ISecurity;

        public function AbstractSocket(param1:ISecurity = null)
        {
            if (getQualifiedClassName(this) == "com.webgame.as3.socket::AbstractSocket")
            {
                throw new ArgumentError("AbstractSocket can\'t be instantiated directly");
            }
            this.FSecurity = param1;
            this.isConnected = false;
            this.sock = new Socket();
            this.sock.endian = Endian.LITTLE_ENDIAN;
            this.initListener();
            return;
        }// end function

        protected function onIOError(event:IOErrorEvent) : void
        {
            this.connectFail(event.text);
            return;
        }// end function

        public function get connected() : Boolean
        {
            return this.isConnected;
        }// end function

        public function connect(param1:String, param2:int) : void
        {
            this.host = param1;
            this.port = param2;
            this.sock.connect(param1, param2);
            return;
        }// end function

        private function initListener() : void
        {
            this.sock.addEventListener(Event.CONNECT, this.onConnect);
            this.sock.addEventListener(Event.CLOSE, this.onServerClose);
            this.sock.addEventListener(ProgressEvent.SOCKET_DATA, this.onData);
            this.sock.addEventListener(IOErrorEvent.IO_ERROR, this.onIOError);
            this.sock.addEventListener(SecurityErrorEvent.SECURITY_ERROR, this.onSecurityError);
            return;
        }// end function

        protected function onConnect(event:Event) : void
        {
            this.isConnected = true;
            var _loc_2:* = new SocketDataEvent(SocketDataEvent.SOCKET_CONNECT_OK_EVENT);
            _loc_2.sockData = null;
            _loc_2.errtext = "";
            dispatchEvent(_loc_2);
            return;
        }// end function

        protected function onServerClose(event:Event) : void
        {
            this.isConnected = false;
            var _loc_2:* = new SocketDataEvent(SocketDataEvent.SOCKET_CONNECT_CLOSE_EVENT);
            _loc_2.sockData = null;
            _loc_2.errtext = "server close client";
            dispatchEvent(_loc_2);
            return;
        }// end function

        public function close() : void
        {
            this.sock.removeEventListener(Event.CONNECT, this.onConnect);
            this.sock.removeEventListener(Event.CLOSE, this.onServerClose);
            this.sock.removeEventListener(ProgressEvent.SOCKET_DATA, this.onData);
            this.sock.removeEventListener(IOErrorEvent.IO_ERROR, this.onIOError);
            this.sock.removeEventListener(SecurityErrorEvent.SECURITY_ERROR, this.onSecurityError);
            if (this.sock.connected)
            {
                this.sock.close();
            }
            this.isConnected = false;
            return;
        }// end function

        public function send(param1:Object) : Boolean
        {
            return false;
        }// end function

        protected function onSendFail(param1:Object) : void
        {
            var _loc_2:* = new SocketDataEvent(SocketDataEvent.SOCKET_SEND_FAIL_EVENT);
            _loc_2.sockData = param1;
            dispatchEvent(_loc_2);
            return;
        }// end function

        protected function onSecurityError(event:SecurityErrorEvent) : void
        {
            this.connectFail(event.text);
            return;
        }// end function

        protected function onData(event:ProgressEvent) : void
        {
            var event:* = event;
            try
            {
                do
                {
                    
                    if (this.length == -1)
                    {
                    }
                    if (this.sock.bytesAvailable >= 2)
                    {
                        this.length = this.sock.readShort();
                    }
                    if (this.length != -1)
                    {
                    }
                    if (this.sock.bytesAvailable >= this.length)
                    {
                        this.onDataRecv(this.length);
                        this.cmd = -1;
                        this.length = -1;
                    }
                    if (this.length == -1)
                    {
                    }
                }while (this.sock.bytesAvailable >= 2)
            }
            catch (error:Error)
            {
                trace(error.message);
            }
            return;
        }// end function

        protected function onDataRecv(param1:int) : void
        {
            return;
        }// end function

        private function connectFail(param1:String) : void
        {
            trace("connect fail", param1);
            var _loc_2:* = new SocketDataEvent(SocketDataEvent.SOCKET_CONNECT_FAIL_EVENT);
            _loc_2.sockData = null;
            _loc_2.errtext = param1;
            dispatchEvent(_loc_2);
            return;
        }// end function

        protected function handleData(param1:Object) : void
        {
            var _loc_2:* = new SocketDataEvent(SocketDataEvent.SOCKET_DATA_RECV_EVENT);
            _loc_2.sockData = param1;
            dispatchEvent(_loc_2);
            return;
        }// end function

    }
}
