Beetle.netpackage is a multi-platform socket TCP communication component, provides a unified object describing the Protocol rules, objects can be used to describe TCP communication interaction. Support for Flash,. NET and the Android platform.

Logs

  • 2013-09-03
    Support message controller android and wp8
    samples:
    	public void OnGetCustomerResponse(NetClient client, GetCustomerResponse e) {
    		mCustomers = e.getItemsList();
    	}
    
    	public void OnGetEmployeeResponse(NetClient client, GetEmployeeResponse e) {
    		mEmployees = e.getItemsList();
    	}
    
    	public void OnOrderSearchRespnose(NetClient client, OrderSearchResponse e) {
    		final List<Order> orders = e.getItemsList();
    		mPages = e.getPages();
    		mHandler.post(new Runnable() {
    
    			@Override
    			public void run() {
    				// TODO Auto-generated method stub
    				mOrderViewAdapter.clear();
    				for (Order item : orders) {
    					mOrderViewAdapter.add(item);
    				}
    				mOrderViewAdapter.notifyDataSetChanged();
    			}
    		});
    	}
    	@Override
    	public void Receive(NetClient client, Object msg) {
    		Controller.Invoke(this, mClient, msg);
    	}
    

  • 2013-08-25
    Support protobuf with andorid and wp8
    samples:
    		mClient =new NetClient("10.0.2.2", 9088, new ProtoPackage(), this);
    		mClient.Connect();
    
    		Register.Builder builder = Register.newBuilder();
    		builder.setEMail("henryfan@msn.com");
    		builder.setUserName("henryfan");
    		mClient.Send(builder.build());
    
     
  • 2013-08-23
    Write error on empty fixed UTF characters (Android and wp8)
    Add an IMessage, IMessage list write and read functions (. NET Android)
    • android
      	<T extends IMessage> T ReadMessage(Class<T> type) throws Exception;
      	<T extends IMessage> AbstractCollection<T> ReadMessages(Class<T> type) throws Exception;
      
      	void Write(IMessage msg)throws Exception;
      	<T extends IMessage> void Write(AbstractCollection<T> messages) throws Exception;
      
    • .net  
              T Reads<T>() where T : IMessage, new();
              IList<T> ReadMessages<T>() where T : IMessage, new();
      
              void Write(IMessage message);
              void Write(IList<IMessage> messages);
      
  • 2013-08-15
    Modify the message save and read to depends on the IDataWriter and IDataReader 

IMessage

Message definition rules

public interface IMessage {
    void Load(IDataReader stream) throws Exception;
    void Save(IDataWriter stream) throws Exception;
}

Package

TCP protocol Analyzer, the default implementation describes the message size of 4 bytes. Subclasses implement a specific message and protocol conversion process definition.

  • 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

Describes the TCP connection rules: including data received event, error handling event, connection success and failure events.

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 communication, message sending and receiving.

  • 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();
    

Last edited Sep 3, 2013 at 2:05 PM by smarkhenry, version 22