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

    public class AMF3Socket extends AbstractSocket
    {
        private var FDebug:Boolean;

        public function AMF3Socket(param1:ISecurity = null, param2:Boolean = false)
        {
            super(param1);
            this.FDebug = param2;
            sock.endian = Endian.BIG_ENDIAN;
            return;
        }// end function

        override public function send(param1:Object) : Boolean
        {
            var _loc_2:ByteArray = null;
            if (sock.connected)
            {
                if (this.FDebug)
                {
                    trace("send pack--------->>");
                    this.printObject(param1);
                }
                _loc_2 = new ByteArray();
                _loc_2.writeObject(param1);
                this.sock.writeInt(param1.cmd);
                this.sock.writeInt(_loc_2.length);
                this.sock.writeBytes(_loc_2);
                this.sock.flush();
                return true;
            }
            else
            {
                onSendFail(param1);
                return false;
            }
        }// end function

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

        override protected function onDataRecv(param1:int) : void
        {
            var _loc_2:* = new ByteArray();
            sock.readBytes(_loc_2, 0, param1);
            var _loc_3:* = _loc_2.readObject();
            _loc_3.cmd = cmd;
            if (this.FDebug)
            {
                trace("recv pack--------->>");
                this.printObject(_loc_3);
            }
            handleData(_loc_3);
            return;
        }// end function

        public function printObject(param1:Object, param2:int = 0) : void
        {
            var _loc_5:* = undefined;
            if (param1 == null)
            {
                return;
            }
            var _loc_3:String = "";
            var _loc_4:int = 0;
            while (_loc_4 < param2)
            {
                
                _loc_3 = _loc_3 + "  ";
                _loc_4 = _loc_4 + 1;
            }
            for (_loc_5 in param1)
            {
                
                if (!(param1[_loc_5] is int))
                {
                }
                if (!(param1[_loc_5] is Number))
                {
                }
                if (param1[_loc_5] is String)
                {
                    if (String(_loc_5) == "cmd")
                    {
                        trace(_loc_3 + "\"" + _loc_5 + "\":", "0x" + (param1[_loc_5] as int).toString(16));
                    }
                    else
                    {
                        trace(_loc_3 + "\"" + _loc_5 + "\":", param1[_loc_5]);
                    }
                    continue;
                }
                if (param1[_loc_5] is Array)
                {
                    trace(_loc_3 + "\"" + _loc_5 + "\":[");
                }
                else
                {
                    trace(_loc_3 + "\"" + _loc_5 + "\":{");
                }
                this.printObject(param1[_loc_5], (param2 + 1));
                if (param1[_loc_5] is Array)
                {
                    trace(_loc_3 + "]");
                    continue;
                }
                trace(_loc_3 + "}");
            }
            return;
        }// end function

    }
}
