2009年3月21日星期六

AS3中操作Telnet的类


package hongwei.net
{
    import flash.events.DataEvent;
    import flash.events.Event;
    import flash.events.EventDispatcher;
    import flash.events.IOErrorEvent;
    import flash.events.ProgressEvent;
    import flash.events.SecurityErrorEvent;
    import flash.net.Socket;
    import flash.utils.ByteArray;
    
    [Event(name="close", type="flash.events.Event")]
    [Event(name="connect", type="flash.events.Event")]
    [Event(name="data", type="flash.events.DataEvent")]
    [Event(name="ioError", type="flash.events.IOErrorEvent")]
    [Event(name="securityError", type="flash.events.SecurityErrorEvent")]
    public class TelnetSocket extends EventDispatcher
    {
        private static const NUL:int        = 0x00;
        private static const BEL:int        = 0x07;
        private static const BS:int        = 0x08;
        private static const HT:int        = 0x09;
        private static const LF:int        = 0x0A;
        private static const FF:int        = 0x0C;
        private static const CR:int        = 0x0D;
        private static const SE:int        = 0xF0;
        private static const NOP:int        = 0xF1;
        private static const DM:int        = 0xF2;
        private static const BRK:int        = 0xF3;
        private static const IP:int        = 0xF4;
        private static const AO:int        = 0xF5;
        private static const AYT:int        = 0xF6;
        private static const EC:int        = 0xF7;
        private static const EL:int        = 0xF8;
        private static const GA:int        = 0xF9;
        private static const SB:int        = 0xFA;
        private static const WILL:int    = 0xFB;
        private static const WONT:int    = 0xFC;
        private static const DO:int        = 0xFD;
        private static const DONT:int    = 0xFE;
        private static const IAC:int        = 0xFF;
        
        public var charSet:String = "utf-8";
        
        private var _socket:Socket;
        private var _state:int;
        
        public function TelnetSocket(host:String=null, port:int=0)
        {
            _socket = new Socket(host, port);
            _socket.addEventListener(Event.CLOSE, _socket_close);
            _socket.addEventListener(Event.CONNECT, _socket_connect);
            _socket.addEventListener(IOErrorEvent.IO_ERROR, _socket_ioError);
            _socket.addEventListener(ProgressEvent.SOCKET_DATA, _socket_socketData);
            _socket.addEventListener(SecurityErrorEvent.SECURITY_ERROR, _socket_securityError);
            
            _state = 0;
        }
        
        public function get connected():Boolean
        {
            return _socket.connected;
        }
        
        public function close():void
        {
            _socket.close();
        }
        
        public function connect(host:String, port:int):void
        {
            _socket.connect(host, port);
        }
        
        public function send(value:String):void
        {
            _socket.writeMultiByte(value, charSet);
            _socket.flush();
        }
        
        private function _socket_close(e:Event):void
        {
            dispatchEvent(e);
        }
        
        private function _socket_connect(e:Event):void
        {
            dispatchEvent(e);
        }
        
        private function _socket_ioError(e:IOErrorEvent):void
        {
            dispatchEvent(e);
        }
        
        private function _socket_socketData(e:ProgressEvent):void
        {
            var n:int = _socket.bytesAvailable;
            var buffer:ByteArray = new ByteArray();
            
            while (0 <= --n)
            {
                var b:int = _socket.readUnsignedByte();
                
                switch (_state)
                {
                    case 0:
                        if (IAC == b)
                        {
                            _state = 1;
                        }
                        else if (CR != b)
                        {
                            buffer.writeByte(b);
                        }
                        break;
                    case 1:
                        _state = DO == b ? 2 : 0;
                        break;
                    case 2:
                        _socket.writeByte(IAC);
                        _socket.writeByte(WONT);
                        _socket.writeByte(b);
                        _socket.flush();
                        _state = 0;
                        break;
                }
            }
            
            buffer.position = 0;
            
            dispatchEvent(new DataEvent(DataEvent.DATA, false, false, buffer.readMultiByte(buffer.length, charSet)));
        }
        
        private function _socket_securityError(e:SecurityErrorEvent):void
        {
            dispatchEvent(e);
        }
    }
}

没有评论: