アクションScriptは簡単にSocket Tcpアプリケーションプロトコルアナライザを実現します.

17884 ワード

  。。smark http://www.cnblogs.com/smark/archive/2012/05/15/2501507.html





ActionScript    Socket Tcp       



      Flash           AMF,    AS            ,      。              Tcp  ,          AMF  ;                。  AS ByteArray     write read  ,             。    AS                   。



             ,                 。



package Beetle.AS

{

    import flash.utils.ByteArray;

    public interface IMessage

    {

        function Load(data:Reader):void;

        function Save(data:Writer):void;

    }

}

                      ,  Reader Writer    ByteArray  ;        ByteArray ?                    .



package Beetle.AS

{

    import flash.net.getClassByAlias;

    import flash.utils.ByteArray;

    import flash.utils.getDefinitionByName;

    import mx.controls.Image;

    public class Reader extends ByteArray

    {

        public function Reader()

        {

            super();

        }

        public function ReadMessages(className:String):Vector.<IMessage>

        {

            var results:Vector.<IMessage> = new Vector.<IMessage>();

            for(var i:int=0;i<readInt();i++)

            {

                var msg:IMessage = IMessage(getDefinitionByName(className));

                results.push(msg);

            }

            return results;

        }

    }

}

package Beetle.AS

{

    import flash.utils.ByteArray;

    public class Writer extends ByteArray

    {

        public function Writer()

        {

            super();

        }

        public function WriteMessages(items:Vector.<IMessage>):void

        {

            writeInt(items.length);

            for each(var item:IMessage in items)

            {

                item.Save(this);

            }

        }

    }

}

           ,            。



package Beetle.AS

{

    import flash.net.Socket;

    import flash.utils.ByteArray;

    import flash.utils.Endian;

    import mx.graphics.shaderClasses.ExclusionShader;

    public  class HeadSizeOfPackage

    {

        public function HeadSizeOfPackage()

        {

            mWriter.endian = Endian.LITTLE_ENDIAN;

            mReader.endian = Endian.LITTLE_ENDIAN;

        }

        private var mMessageReceive:Function;

        //        

        public function get MessageReceive():Function

        {

            return mMessageReceive;

        }

        public function set MessageReceive(value:Function):void

        {

            mMessageReceive = value;

        }

        //        

        protected  function WriteMessageTag(message:IMessage,data:Writer):void

        {

            throw new Error("WriteMessageTag not implement!");

        }

        //      

        protected  function GetMessageByTag(data:Reader):IMessage

        {

            throw new Error("GetMessageByTag not implement!");

        }

        private var mReader:Reader = new Reader();

        private var mSize:int=0;

        //    Socket     

        public function Import(socket:Socket):void

        {

            socket.endian = Endian.LITTLE_ENDIAN;

            while(socket.bytesAvailable>0)

            {

                if(mSize==0)

                {

                    mSize= socket.readInt()-4;

                    mReader.clear();

                }

                if(socket.bytesAvailable>= mSize)

                {

                   socket.readBytes(mReader,mReader.length,mSize);

                   var msg:IMessage = GetMessageByTag(mReader);

                   msg.Load(mReader);

                   if(MessageReceive!=null)

                       MessageReceive(msg);

                   mSize=0;

                }

                else{

                    mSize= mSize-socket.bytesAvailable;

                    socket.readBytes(mReader,mReader.length,socket.bytesAvailable);

                }

            }

        }

        private var mWriter:Writer = new Writer();

        //         

        public function Send(message:IMessage,socket:Socket):void

        {

             socket.endian = Endian.LITTLE_ENDIAN;

             mWriter.clear();

             WriteMessageTag(message,mWriter);

             message.Save(mWriter);

             socket.writeInt(mWriter.length+4);

             socket.writeBytes(mWriter,0,mWriter.length);

             socket.flush();

        }

    }

}

              ,         ,                       .MessageReceive          ,                C#   。                 WriteMessageTag GetMessageByTag,                                。



     Send           ,                  Socket      。



public function Send(message:IMessage,socket:Socket):void

{

     socket.endian = Endian.LITTLE_ENDIAN;

     mWriter.clear();

     WriteMessageTag(message,mWriter);

     message.Save(mWriter);

     socket.writeInt(mWriter.length+4);

     socket.writeBytes(mWriter,0,mWriter.length);

     socket.flush();

}

              Save            ,                          string int   ,         ;               socket        。



    Import           ,     Socket           。



public function Import(socket:Socket):void

{

    socket.endian = Endian.LITTLE_ENDIAN;

    while(socket.bytesAvailable>0)

    {

        if(mSize==0)

        {

            mSize= socket.readInt()-4;

            mReader.clear();

        }

        if(socket.bytesAvailable>= mSize)

        {

           socket.readBytes(mReader,mReader.length,mSize);

           var msg:IMessage = GetMessageByTag(mReader);

           msg.Load(mReader);

           if(MessageReceive!=null)

               MessageReceive(msg);

           mSize=0;

        }

        else{

            mSize= mSize-socket.bytesAvailable;

            socket.readBytes(mReader,mReader.length,socket.bytesAvailable);

        }

    }

}

                    ,           ;               ,   Socket         ,                        ,                     .



            4             ,             。          ,                  .



public class HeadSizePackage extends HeadSizeOfPackage

{

    public function HeadSizePackage()

    {

        super();

    }

    override protected function GetMessageByTag(data:Reader):IMessage 

    {

        var name:String = data.readUTF();

        switch(name)

        {

            case "Register":

                return new Register();

            case "User":

                return new User();

            case "GetUser":

                return new GetUser();

            default :

                return null;

        }

    }

    override protected function WriteMessageTag(message:IMessage, data:Writer):void

    {

         if(message is Register)

         {

             data.writeUTF("Register");

         }

         else if(message is User)

         {

             data.writeUTF("User");

         }

         else if(message is GetUser){

             data.writeUTF("GetUser");

         }

         else

         {

             data.writeUTF("NULL");

         }

         

    }

}

               ,    IMessage    



public class Register implements IMessage

{

    public function Register()

    {

    }

    public var UserName:String;

    public var EMail:String

    public function Load(data:Reader):void

    {

        UserName= data.readUTF();

        EMail = data.readUTF();

    }

    public function Save(data:Writer):void

    {

        data.writeUTF(UserName);

        data.writeUTF(EMail);

    }

}

             



var reg:Register= new Register();

reg.UserName = txtUserName.text;

reg.EMail = txtEmail.text;

mPackage.Send(reg,mSocket);

     AS Socket        ,       socket  ,   ByteArray              。