2013年8月27日星期二

Multi-Platform Client TCP communication components

 

Beetle.NetPackage is a multi-platform Client Socket TCP communication components (Apache License 2.0), components of the system unified object protocol rules, the flexibility to easily communicate through the object to describe TCP interaction. Existing support platform Flash,. NET and Android. components in order to unify the preparation of specifications developed different language platform to access TCP service uniform processing rules; were standardized protocol description and message processing control. Different language platform provides a consistent under all objects and interfaces include: IMessage, Packge, INetClientHandler and NetClient so on.

 

IMessage

 

messages describe interfaces, all communication messages must implement the relevant rules.

 

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

 

This object is used for communication protocol description, the default implementation of 4-byte message size specification description. Package's main job is to write the object communication data and communication data obtained from the object. Object is simply to provide a basis for the processing rules in the application the user must be derived on the basis of such a new protocol processing objects.

 

 

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

 

This interface is mainly used to describe the TCP connection handling rules, including receiving event data, error handling events, connecting create 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

 

This object is mainly used to create a TCP communications and for message sending and receiving treatment.

 

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


 

 

project site: https://beetlenp.codeplex.com/

没有评论:

发表评论