寫自己的Socket框架(三),socket框架
在通信寫完了以後,應用層接收到Socket拋上來的byte[],這個時候對於實際的寫邏輯的開發者來說,這樣的數據並不友好,我們就需要在應用層統一一個包的規則(應用層協議),處理完以後,然後再傳給實際的邏輯層去處理。
以下是一個常用的Command模式。既接收到傳遞過來的包以後,根據Command(命令)來執行對應的Command(邏輯)。
我們假定我們的包(以下所有的包都指的是應用層的包,而非Socket層的包)分為 命令頭/數據 兩塊。
public class InterUnit
{
public string Command;
public JToken Body;
}
因為采用Command模式,我們定義了一個接口ICommand
public interface ICommand
{
InterUnit Execute(InterUnit unit);
}
命令的Command,如何跟實際邏輯對應起來,常用的有Ioc,但是你也可以硬編碼,我采用的是Attribute的方式,來對應起來。
[AttributeUsage(AttributeTargets.Class, AllowMultiple = false)]
public class CommandAttribute : Attribute
{
public CommandAttribute(string command)
{
this.Command = command;
}
public string Command;
}
對應起來以後,那就需要在接到包的地方,去根據Command找到對應的Class來執行邏輯。

![]()
public class CommandFactory
{
private Dictionary<string, ICommand> _commandMap;
public CommandFactory()
{
if (_commandMap == null)
{
_commandMap = new Dictionary<string, ICommand>();
}
}
/// <summary>
/// 通過反射將標注了CommandAttribute的實例,放入字典。
/// 不需要等到需要調用時,才去動態的注入。
/// </summary>
/// <param name="assembly"></param>
public void Init(params string[] assembly)
{
if (assembly != null)
{
foreach (string s in assembly)
{
var ass = Assembly.Load(s);
if (ass != null)
{
var types = ass.GetTypes();
foreach (var type in types)
{
CommandAttribute attr = type.GetCustomAttribute(typeof(CommandAttribute), false) as CommandAttribute;
if (attr != null)
{
if (attr.Command == null || attr.Command.Length == 0)
{
_commandMap[type.Name] = Activator.CreateInstance(type) as ICommand;
}
else
{
_commandMap[attr.Command] = Activator.CreateInstance(type) as ICommand;
}
}
}
}
}
}
}
public void ExecuteCommand(SocketSession session, InterUnit unit)
{
if(_commandMap.ContainsKey(unit.Command))
{
ICommand command = _commandMap[unit.Command];
var rtv = command.Execute(unit);
if (rtv != null)
{
session.Send(BsonHelper.ToBson<InterUnit>(unit));
}
}
}
}
View Code
我在這裡采用的是Bson的格式,作為數據來傳遞。
有一個地方需要注意的就是,在Send的時候,實際上我們並沒有定義Socket的包的格式,因為在協議的地方已經處理了這個事情,會將你發送過去的數據,自動加上包頭。

![]()
public interface IProtocol
{
byte[] OnDataReceivedCallBack(byte[] data, ref int offset);
byte[] OnDataSendBefore(byte[] data);
}
public class DefaultProtocol : IProtocol
{
public byte[] OnDataReceivedCallBack(byte[] data, ref int offset)
{
int length = BitConverter.ToInt32(data, offset);
int package_head = 4;
int package_length = length + package_head;
byte[] buffer = null;
if (length > 0)
{
if (offset + package_length <= data.Length)
{
buffer = new byte[length];
Array.Copy(data, offset + package_head, buffer, 0, length);
offset += package_length;
}
}
else
{
offset = -1;
}
return buffer;
}
public byte[] OnDataSendBefore(byte[] data)
{
int length = data.Length;
var head = BitConverter.GetBytes(length);
byte[] buffer = new byte[data.Length + head.Length];
Buffer.BlockCopy(head, 0, buffer, 0, head.Length);
Buffer.BlockCopy(data, 0, buffer, head.Length, data.Length);
return buffer;
}
}
View Code
利用socket進行面向連接的網絡編程程序框架給我寫一個吧
這個。。。。。你要源代碼?
JAVA 的三個框架是哪個框架
Java的框架不止三個,很多很多,大公司都有自己寫的框架,小公司都使用現在網絡上流暢的比較流行的三大框架SSH(struts2+spring+hibernate3),祝樓主早日學業有成!~~~