多平台Client TCP通讯组件

Beetle.NetPackage是一个多平台Client Socket TCP通讯组件(Apache License 2.0),组件制统一的对象协议制定规则,可以灵活方便地通过对象来描述TCP通讯交互。现有支持平台有Flash,.NET和Android。组件为了统一编写规范制定了不同语言平台下访问TCP服务的统一处理规则;分别有协议描述规范和消息处理控制。不同语言平台下都提供了一致的对象和接口主要包括:IMessage,Packge,INetClientHandler和NetClient等。

IMessage

消息描述接口,所有通讯消息必须实现相关规则。

flash

public class Register implements IMessage
    {        
        public var Name:String;        
        public var EMail:String;        
        public var City:String;
        public var Country:String;        
        public var RegTime:Date;      
        public function Register()
        {
        }
        public function Load(reader:ByteArray):void
        {
            Name= reader.readUTF();
            EMail= reader.readUTF();
            City= reader.readUTF();
            Country = reader.readUTF();
            var dv:String= reader.readUTF();
            RegTime=DateFormatter.parseDateString(dv);
            
            
        }    
        public function Save(writer:ByteArray):void
        {
            writer.writeUTF(Name);
            writer.writeUTF(EMail);
            writer.writeUTF(City);
            writer.writeUTF(Country);
            var d:DateFormatter =new DateFormatter();
            d.formatString="YYYY-MM-DD HH:NN:SS";
            writer.writeUTF(d.format(RegTime));        
        }
    }

android

public class Register implements IMessage {   
    public String Name;   
    public String EMail;  
    public String City; 
    public String Country;  
    public Date RegTime;  
    @Override
    public void Load(DataInputStream stream) throws Exception {
        // TODO Auto-generated method stub
        Name = stream.readUTF();
        EMail = stream.readUTF();
        City = stream.readUTF();
        Country = stream.readUTF();
        String format = "yyyy-MM-dd HH:mm:ss";
        SimpleDateFormat sdf = new SimpleDateFormat(format);
        RegTime = sdf.parse(stream.readUTF());  
    }
    @Override
    public void Save(DataOutputStream stream) throws Exception {
        // TODO Auto-generated method stub
        stream.writeUTF(Name);
        stream.writeUTF(EMail);
        stream.writeUTF(City);
        stream.writeUTF(Country);
        String format = "yyyy-MM-dd HH:mm:ss";
        SimpleDateFormat sdf = new SimpleDateFormat(format);
        stream.writeUTF(sdf.format(RegTime));
    }
}

c#

 public class Register:Beetle.NetPackage.IMessage
    {
        public String Name;
        public String EMail;
        public String City;
        public String Country;
        public DateTime RegTime;
        public void Load(Beetle.NetPackage.BufferStream reader)
        {
            Name = reader.ReadUTF();
            EMail = reader.ReadUTF();
            City = reader.ReadUTF();
            Country = reader.ReadUTF();
            string date = reader.ReadUTF();
            Console.WriteLine(date);
            RegTime = DateTime.ParseExact(date, "yyyy-MM-dd HH:mm:ss", CultureInfo.InvariantCulture);
        }
        public void Save(Beetle.NetPackage.BufferStream writer)
        {
            writer.WriteUTF(Name);
            writer.WriteUTF(EMail);
            writer.WriteUTF(City);
            writer.WriteUTF(Country);
            writer.WriteUTF(RegTime.ToString("yyyy-MM-dd HH:mm:ss"));
        }
    }

Package

该对象是用于通讯协议描述,默认实现4字节描述消息大小规范。Package的主要工作是对象写入通讯数据和从通讯数据获取对象。对象只是提供一个基础的处理规则,在应用中使用者必须从此类的基础上派生出新的协议处理对象。

flash

public class NPPackage extends Package
    {
        public function NPPackage()
        {
            super();
        }
        protected override function  WriteMessageType(writer:ByteArray, message:IMessage):void
        {
            writer.writeUTF(flash.utils.getQualifiedClassName(message));    
        }        
        protected override  function GetMessage(reader:ByteArray):IMessage
        {
            var name:String = reader.readUTF();
            if(name=="Register")
                return new Register();
            return null;
        }
    }

android

public class NPPackage extends Package {

    @Override
    protected void WriteMessageType(DataOutputStream writer, IMessage message)
            throws Exception {
        // TODO Auto-generated method stub
        writer.writeUTF(message.getClass().getSimpleName());
    }

    @Override
    protected IMessage GetMessage(DataInputStream reader) throws Exception {
        // TODO Auto-generated method stub
        String name= reader.readUTF();
        if(name.equals("Register"))
            return new Register();
        return null;
    }

}

c#

 class NPPackage:Beetle.NetPackage.Package
    {

        protected override void WriteMessageType(Beetle.NetPackage.BufferStream writer, Beetle.NetPackage.IMessage message)
        {
            writer.WriteUTF(message.GetType().Name);
        }

        protected override Beetle.NetPackage.IMessage GetMessage(Beetle.NetPackage.BufferStream reader)
        {
            string Name = reader.ReadUTF();
            switch (Name)
            {
                case "Register":
                    return new Register();
            }
            return null;
        }
    }

INetClientHandler

该接口主要用于描述TCP连接处理规则,包括有数据接收事件,错误处理事件,连接创建成功和失败事件。

 public interface INetClientHandler {

    void  Receive(NetClient client, Object msg);
    void  Error(NetClient client, Exception e);
    void  Disposed(NetClient client);
    void  Connected(NetClient client);

}

NetClient

该对象主要用于创建TCP通讯和进行消息发送接收处理。

flash

mClient = new NetClient("127.0.0.1",9088,new NPPackage(),new NetClientHandler(
    ClientReceive,ClientError,ClientDisposed,ClientConnected));
mClient.SocketEndian = Endian.BIG_ENDIAN;
mClient.Connect();

  android

mClient = new NetClient("10.0.2.2", 9088, new NPPackage(), this);
mClient.Connect();

  c#

mClient = new Beetle.NetPackage.NetClient("127.0.0.1", 9088, new NPPackage(), this);
mClient.LittleEndian = false;
mClient.Connect();


 

项目站点:https://beetlenp.codeplex.com/

posted @ 2013-08-14 10:36  beetlex  阅读(2457)  评论(2编辑  收藏  举报