FastSocket学习笔记~制定自已的传输协议~续~制定基于FastSocket的协议

时间:2022-01-15 20:39:08

FastSocket这个东西上次我已经说过,它使用简单,功能强大,扩展灵活,目前在新浪的生产环境中已经被广泛使用,所以它的性能,安全等各方面我们绝对可以信赖,今天我们来说一个话题,和上一讲有关,这次我们制作一个基于FastSocket的传输协议,它的意义重大,当fastSocket提供的协议不能满足项目要求时,我们就必须硬着头皮去自己写了,还好,fastsocket为我们铺好了路,我们只要按着这条路走下去,就可以了。

自定义的协议格式如下

aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAA0YAAAAmCAIAAABvWhyIAAAFTklEQVR4nO2WSxbiOgxEWQoLf5vsGcMevRw6tsolJQGbXM6dxDj6lMugx+vPfwAAAACwNI+vVwAAAAAAB2GkAwAAAFgeRjoAAACA5WGkAwAAAFgeRjoAAACA5WGkAwAAAFgeRjoAAACA5WGkAwAAAFgeRjoAAACA5WGkAwAAAFgeRjoAAACA5WGkAwAAAFgeRjoAAACA5WGkAwAAAFgeRjoAAACA5emPdM//P1+v71vo3i9VZhd8dxbfOpobtoywk8Ml/a7CM4AHfpXJpbuivNY//lvb5nCk66bJJltOUCf+u+h+hc+3j17sRo6SRhHMfrcXn/9+pm1Z1+nXljVD+8mqrbOkhBWKHfFSezrdaI5hapoXzmV+YcW2i3BKLaj9rQg65hIe2EU45XLVxL+V9/yCC1I7m1PBTVcPX2ekS2tqphbRtnvrZ3f+LY74QIf6pZazylyariDsy/53SQkb7RT/Op+X6zXyzITC3vxvFQ+InUcuV038W3mvkMVP6uxMBTddPXzdGulejVPfH9s7sFvRj7sgOrLuuRvEzNg+Ri92lTmleOcxKkbrIESOQomM87S8q0fYQNfZnn63F/EolBFJjwgrZDF1HkZohW0XRYQoZtvgULTuuYgX5xFWBBRS7DakpGsPqHtkkaTZoxEp8IAWod2fkjFbXuoEh0JFes7jvYJ62hjPfz/DU4661lJEsuh+t8eH6YNuOL9ikaUWxJfPtFfbdfe0utGGO4fFD1sWBZg66Gh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,如果要想扩展一个自己的协议,要对 client和server端分别进行开发,下面我们来看一下client的开发

aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAVEAAAEDCAIAAAADUqDpAAAQlUlEQVR4nO2dsW8cxxWH/XeoOMACJINkqUJtcCkDI7V6G/A/kFQpBCMgELoKbMNxYAMKLJbRtbYLwtHFqQimMBtblSnbgNSQdLF1igsWezPz3r6d3bnb3fd9GAhzw5n33p74uzdLzj6+cX39awUAbngDzQO4orjmH548MLaiYQDAhl1o/ub6trWheYDdkKP5y8tL++Q+ml9s0zVOyVSnJX2cSt6lSIZyB6DQWfPr9frw8DD5pdOLq/tPr+48ubr/9Or04mozWGt+9Wy1XC6Xy+Xq2erm+na5XNb/Knm+lkFXPTTnS/1ORrKnDRJJtneAgG6aX6/X9+7dk77J7jy5evz85Vff/fj4+cs7T7Y0vxH8zz/9cnN9+8P3L5bbtGo+49t6EHUNrvkM4z29AwR00Hwt+E7fZBvNL5fLb//9bXMzb8/zscdgJN63x6ukvXTScrC5SBrXIwxsWsZbI4wv0+IdIMCq+abglTzfbJvBpOb75Pl4t5+Mp3VQsRNrXlkl+Wr9alLhyZf6JSN46IRJ84Hgdc3fXN/Gmv/007839/YZ9/PBYGsab1Wa5bNDyau70Xy8xYhnonnoRLvmLy8vDw8PF9skZyqar2W/XC6//upr++/qFGHommntGzXfOkca7BRVcsR47WgeOjHk7+eVvX3P39VJ4/XeOznTPiHpMe4rUSWzsXQhyQ9QxaweT/JCACQ4kwPgC87eAviCZ2wAfIHmAXyB5gF8geYBfIHmAXyB5gF8UVDz6/V6QGtnZ2ePHj3anOT77LPPBrQM4IqCmj84OBhK9mdnZ/UDOcfHx4PYBPBJQc0vFoujo6NBZF9n+KbgpWwvnbTdJZyEhdFSVvOr1apV9snqOgFxkj8+Pl4ul4rroFMUFA4Toqzmq6pqlX2yuk5A82H74+PjjeDRPEAGxTVfVdXJycnR0dGrV6+yTS1TPHr0SHEdP7JWqY+jSXcErXbiVcnbiuQcnoe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nR8rzisLjVa0j9jxvicdu32IHIMls6+TYNaasah3po3lpy5Cn+di4vhDcMsM6ObX35Mtymte1KsWjRCgFqduxrwWfzLBOTiXf4lbbCVbKvfFIcqbuS5mjRFVFWTq5F7BcLJqHJNTJAfAF5+0BfIHmAXyB5gF8geYBfIHmAXyB5gF8geYBfFFc88FTMUorGgYAbNiF5m+ub1sbmgfYDTmav7y8tE/uo3nlnGxXMuwMeHyV87AwHjprfr1eHx4eJr+UrHhTa371bLVcLpfL5erZ6ub6drlc1v8qeV4/aq6wMD/9YjSSPS3bO0AJuml+vV7fu3dP+sZNVrzZaH4j+J9/+uXm+vaH718st2nVfIZUBlHa4JoH2Dv/A4Ig875ovtSEAAAAAElFTkSuQmCC" alt="" />

我们要添加的类有三个文件组成,分别是DSSBinaryProtocol,DSSBinaryResponse和一个使用这个协议的客户端入口DSSBinarySocketClient

DSSBinaryProtocol

  /// <summary>
/// 异步二进制协议
/// 协议格式
/// [Message Length(int32)][SeqID(int32)][ProjectID(int16)][Cmd Length(int16)][VersonNumber Length(int16)][Cmd + VersonNumber + Body Buffer]
/// 其中参数TableName和VersonNumber长度为40,不够自动在左侧补空格
/// </summary>
public sealed class DSSBinaryProtocol : IProtocol<DSSBinaryResponse>
{ #region IProtocol Members
/// <summary>
/// find response
/// </summary>
/// <param name="connection"></param>
/// <param name="buffer"></param>
/// <param name="readlength"></param>
/// <returns></returns>
/// <exception cref="BadProtocolException">bad async binary protocl</exception>
public DSSBinaryResponse FindResponse(IConnection connection, ArraySegment<byte> buffer, out int readlength)
{
if (buffer.Count < ) { readlength = ; return null; } //获取message length
var messageLength = NetworkBitConverter.ToInt32(buffer.Array, buffer.Offset);
if (messageLength < ) throw new BadProtocolException("bad async binary protocl"); readlength = messageLength + ;
if (buffer.Count < readlength) { readlength = ; return null; } var seqID = NetworkBitConverter.ToInt32(buffer.Array, buffer.Offset + );
var projectID = NetworkBitConverter.ToInt16(buffer.Array, buffer.Offset + );
var flagLength = NetworkBitConverter.ToInt16(buffer.Array, buffer.Offset + );
var versonLength = NetworkBitConverter.ToInt16(buffer.Array, buffer.Offset + );
var strName = Encoding.UTF8.GetString(buffer.Array, buffer.Offset + , flagLength);
var versonNumber = Encoding.UTF8.GetString(buffer.Array, buffer.Offset + + flagLength, versonLength); var dataLength = messageLength - - flagLength - versonLength;
byte[] data = null;
if (dataLength > )
{
data = new byte[dataLength];
Buffer.BlockCopy(buffer.Array, buffer.Offset + + flagLength + versonLength, data, , dataLength);
}
return new DSSBinaryResponse(seqID, projectID, strName, versonNumber, data);
}
#endregion
}

DSSBinaryResponse

    /// <summary>
/// 数据同步系统DSS使用的Socket协议,我们称为DSSBinary协议
/// [Message Length(int32)][SeqID(int32)][ProjectID(int16)][Cmd Length(int16)][VersonNumber Length(int16)][Cmd + VersonNumber + Body Buffer]
/// </summary>
public class DSSBinaryResponse : IResponse
{
/// <summary>
/// 流水ID
/// </summary>
public int SeqID { get; private set; }
/// <summary>
/// 项目类型编号
/// </summary>
public short ProjectID { get; set; }
/// <summary>
/// 本次传输的版本号,所有客户端唯一[项目名称(4字节)+guid(36字节)]
/// </summary>
public string VersonNumber { get; private set; }
/// <summary>
/// 命令名称
/// </summary>
public string Flag { get; private set; } /// <summary>
/// 要操作的表对象,以字节数组形式进行传输
/// </summary>
public readonly byte[] Buffer = null; public DSSBinaryResponse(int seqID,
short projectID,
string flag,
string versonNumber,
byte[] buffer)
{
this.SeqID = seqID;
this.ProjectID = projectID;
this.VersonNumber = versonNumber;
this.Flag = flag;
this.Buffer = buffer;
}
}

DSSBinarySocketClient

   /// <summary>
/// 异步socket客户端
/// </summary>
public class DSSBinarySocketClient : PooledSocketClient<DSSBinaryResponse>
{
#region Constructors
/// <summary>
/// new
/// </summary>
public DSSBinarySocketClient()
: base(new DSSBinaryProtocol())
{
}
/// <summary>
/// new
/// </summary>
/// <param name="socketBufferSize"></param>
/// <param name="messageBufferSize"></param>
public DSSBinarySocketClient(int socketBufferSize, int messageBufferSize)
: base(new DSSBinaryProtocol(), socketBufferSize, messageBufferSize, , )
{
}
/// <summary>
/// new
/// </summary>
/// <param name="socketBufferSize"></param>
/// <param name="messageBufferSize"></param>
/// <param name="millisecondsSendTimeout"></param>
/// <param name="millisecondsReceiveTimeout"></param>
public DSSBinarySocketClient(int socketBufferSize,
int messageBufferSize,
int millisecondsSendTimeout,
int millisecondsReceiveTimeout)
: base(new DSSBinaryProtocol(),
socketBufferSize,
messageBufferSize,
millisecondsSendTimeout,
millisecondsReceiveTimeout)
{
}
#endregion #region Public Methods public Task<TResult> Send<TResult>(string cmdName, short projectID, string versonNumber, byte[] payload,
Func<DSSBinaryResponse, TResult> funcResultFactory, object asyncState = null)
{
return this.Send(null, cmdName, projectID, versonNumber, payload, funcResultFactory, asyncState);
} public Task<TResult> Send<TResult>(byte[] consistentKey, string cmdName, short projectID, string versonNumber, byte[] payload,
Func<DSSBinaryResponse, TResult> funcResultFactory, object asyncState = null)
{
if (string.IsNullOrEmpty(cmdName)) throw new ArgumentNullException("cmdName");
if (funcResultFactory == null) throw new ArgumentNullException("funcResultFactory"); var seqID = base.NextRequestSeqID();
var cmdLength = cmdName.Length;
var versonNumberLength = versonNumber.Length;
var messageLength = (payload == null ? : payload.Length) + cmdLength + versonNumberLength + ;
var sendBuffer = new byte[messageLength + ]; //write message length
Buffer.BlockCopy(NetworkBitConverter.GetBytes(messageLength), , sendBuffer, , );
//write seqID.
Buffer.BlockCopy(NetworkBitConverter.GetBytes(seqID), , sendBuffer, , );
//write proejctID
Buffer.BlockCopy(NetworkBitConverter.GetBytes(projectID), , sendBuffer, , );
//write response flag length.
Buffer.BlockCopy(NetworkBitConverter.GetBytes((short)cmdLength), , sendBuffer, , );
//write verson length
Buffer.BlockCopy(NetworkBitConverter.GetBytes((short)versonNumberLength), , sendBuffer, , );
//write response cmd
Buffer.BlockCopy(Encoding.ASCII.GetBytes(cmdName), , sendBuffer, , cmdLength);
//write response versonNumber
Buffer.BlockCopy(Encoding.ASCII.GetBytes(versonNumber), , sendBuffer, + cmdLength, versonNumberLength);
//write body buffer
if (payload != null && payload.Length > )
Buffer.BlockCopy(payload, , sendBuffer, + cmdLength + versonNumberLength, payload.Length); var source = new TaskCompletionSource<TResult>(asyncState);
base.Send(new Request<DSSBinaryResponse>(consistentKey, seqID, cmdName, sendBuffer,
ex => source.TrySetException(ex),
response =>
{
TResult result;
try { result = funcResultFactory(response); }
catch (Exception ex) { source.TrySetException(ex); return; } source.TrySetResult(result);
}));
return source.Task;
}
#endregion
}

然后,我们再来说一下server端的开发,它有两个文件组成,分别是DSSBinaryCommandInfo,DSSBinaryProtocol

aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAUoAAAFYCAIAAAC6VSLCAAASg0lEQVR4nO3dPW8c1xXGcX8OFSwEWIC0pQq1waQMFqnV24A/hmAEC5iuUhiOAxYKIoFVzNZ2QRjaOAAbNVaTpDKVBJAaUol926RYZzi899xzz7ztzJ75/7AQhsP7tgSfvbvSzNF7D7/47xiP0NJIy+DBY8mP9yZfAQ8ePEZ6EG8ePNw+3mv7LhrAoSDegFvEG3BrxHg/On5ofIy3BmDJxo339dW74oN4AyNpF+9Xr17ZG/eJ99FtrRapDNWqS59J+8wODKVFvLfb7YMHD8RvPX95ee/Z5Z2nl/eeXT5/ebk7Wcf77Muzqqqqqjr78uz66l1VVfWfyu5d56FtMJrtc8etBuncrPPswCCs8d5ut++//37ud/TO08snL15//f0PT168vvP0Vrx32f7nP/51ffXub3/9e3VbMd4dUjFIqAaPN7B/pnjX2W71+7qLd1VV3/35u+a7cfvunc4YnUnf+qa9xGXnRo7eMoiD6yssThSdrI+jg+hksReQKse7mW1l924+difFePfZvaPshcz2WDypjJPGW+mVm6vD7OlrjeWAYENXiHeUbT3e11fv0nh/8cXvm2/OO3z2jk6KW2ixY7BlpjhRelKcS589GkecPSQvCsVeQESL96tXrx48eHB0m9hSiXed8Kqqvvn6G/s/jOV+6fVvWY6N8S62yZ00zq4PUnwdId4oGubfvZU35z3/YSx3vt7fxJ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VrgFtVaiDTcolrLTYN0KHEQ5b6u4jj2TwHcQ4b+qNYi9LU0Fs8oY1oGbDU7UES1lmx3yyYvnmm7HuVZ6HMBOqq1WKu1pG3EM+PFu/gtIEK1FlO1FrGZcia3nnQZgfotGA3VWgC3FnfNOXsglmNx8QaWg3gDbv0Pe7OPJlYP5+sAAAAASUVORK5CYII=" alt="" />

DSSBinaryCommandInfo

    /// <summary>
/// async binary command info.
/// </summary>
public class DSSBinaryCommandInfo : ICommandInfo
{
#region Constructors
/// <summary>
/// new
/// </summary>
/// <param name="cmdName"></param>
/// <param name="seqID"></param>
/// <param name="buffer"></param>
/// <exception cref="ArgumentNullException">cmdName is null or empty.</exception>
public DSSBinaryCommandInfo(int seqID, short projectID, string cmdName, string versonNumber, byte[] buffer)
{
if (string.IsNullOrEmpty(cmdName)) throw new ArgumentNullException("cmdName");
if (string.IsNullOrEmpty(versonNumber)) throw new ArgumentNullException("versonNumber"); this.VersonNumber = versonNumber;
this.CmdName = cmdName;
this.SeqID = seqID;
this.ProjectID = projectID;
this.Buffer = buffer;
}
#endregion #region Public Properties
/// <summary>
/// 版本号
/// </summary>
public string VersonNumber
{
get;
private set;
}
public short ProjectID { get; private set; }
/// <summary>
/// get the current command name.
/// </summary>
public string CmdName
{
get;
private set;
}
/// <summary>
/// seq id.
/// </summary>
public int SeqID
{
get;
private set;
}
/// <summary>
/// 主体内容
/// </summary>
public byte[] Buffer
{
get;
private set;
}
#endregion #region Public Methods
/// <summary>
/// reply
/// </summary>
/// <param name="connection"></param>
/// <param name="payload"></param>
public void Reply(IConnection connection, byte[] payload)
{
var packet = PacketBuilder.ToDSSBinary(this.SeqID, this.ProjectID, this.CmdName, this.VersonNumber, payload);
connection.BeginSend(packet);
}
#endregion }

DSSBinaryProtocol

    /// <summary>
/// 数据中心二进制协议
/// 协议格式
/// [Message Length(int32)][SeqID(int32)][Request|Response Flag Length(int16)][VersonNumber Length(int16)][Request|Response Flag + VersonNumber + Body Buffer]
/// </summary>
public sealed class DSSBinaryProtocol : IProtocol<DSSBinaryCommandInfo>
{
#region IProtocol Members
/// <summary>
/// find command
/// </summary>
/// <param name="connection"></param>
/// <param name="buffer"></param>
/// <param name="maxMessageSize"></param>
/// <param name="readlength"></param>
/// <returns></returns>
/// <exception cref="BadProtocolException">bad async binary protocl</exception>
public DSSBinaryCommandInfo FindCommandInfo(IConnection connection, ArraySegment<byte> buffer,
int maxMessageSize, out int readlength)
{
if (buffer.Count < ) { readlength = ; return null; } var payload = buffer.Array; //获取message length
var messageLength = NetworkBitConverter.ToInt32(payload, buffer.Offset);
if (messageLength < ) throw new BadProtocolException("bad async binary protocl");
if (messageLength > maxMessageSize) throw new BadProtocolException("message is too long"); readlength = messageLength + ;
if (buffer.Count < readlength)
{
readlength = ; return null;
} var seqID = NetworkBitConverter.ToInt32(payload, buffer.Offset + );
var projectID = NetworkBitConverter.ToInt16(payload, buffer.Offset + );
var cmdNameLength = NetworkBitConverter.ToInt16(payload, buffer.Offset + );
var versonNumberLength = NetworkBitConverter.ToInt16(payload, buffer.Offset + );
var strName = Encoding.UTF8.GetString(payload, buffer.Offset + , cmdNameLength);
var versonNumber = Encoding.UTF8.GetString(payload, buffer.Offset + + cmdNameLength, versonNumberLength); var dataLength = messageLength - - cmdNameLength;
byte[] data = null;
if (dataLength > )
{
data = new byte[dataLength];
Buffer.BlockCopy(payload, buffer.Offset + + cmdNameLength + versonNumberLength, data, , dataLength);
}
return new DSSBinaryCommandInfo(seqID, projectID, strName, versonNumber, data);
}
#endregion
}

除了上面两个文件外,我们还要修改服务端的管理类

    /// <summary>
/// Socket server manager.
/// </summary>
public class SocketServerManager
{
#region Private Members
static private readonly List<SocketBase.IHost> _listHosts = new List<SocketBase.IHost>();
#endregion #region Static Methods
/// <summary>
/// 初始化Socket Server
/// </summary>
static public void Init()
{
Init("socketServer");
}
/// <summary>
/// 初始化Socket Server
/// </summary>
/// <param name="sectionName"></param>
static public void Init(string sectionName)
{
if (string.IsNullOrEmpty(sectionName)) throw new ArgumentNullException("sectionName");
Init(ConfigurationManager.GetSection(sectionName) as Config.SocketServerConfig);
}
/// <summary>
/// 初始化Socket Server
/// </summary>
/// <param name="config"></param>
static public void Init(Config.SocketServerConfig config)
{
if (config == null) throw new ArgumentNullException("config");
if (config.Servers == null) return; foreach (Config.Server serverConfig in config.Servers)
{
//inti protocol
var objProtocol = GetProtocol(serverConfig.Protocol);
if (objProtocol == null) throw new InvalidOperationException("protocol"); //init custom service
var tService = Type.GetType(serverConfig.ServiceType, false);
if (tService == null) throw new InvalidOperationException("serviceType"); var serviceFace = tService.GetInterface(typeof(ISocketService<>).Name);
if (serviceFace == null) throw new InvalidOperationException("serviceType"); var objService = Activator.CreateInstance(tService);
if (objService == null) throw new InvalidOperationException("serviceType"); //init host.
var host = Activator.CreateInstance(typeof(SocketServer<>).MakeGenericType(
serviceFace.GetGenericArguments()),
objService,
objProtocol,
serverConfig.SocketBufferSize,
serverConfig.MessageBufferSize,
serverConfig.MaxMessageSize,
serverConfig.MaxConnections) as BaseSocketServer; host.AddListener(serverConfig.Name, new IPEndPoint(IPAddress.Any, serverConfig.Port)); _listHosts.Add(host);
}
}
/// <summary>
/// get protocol.
/// </summary>
/// <param name="protocol"></param>
/// <returns></returns>
static public object GetProtocol(string protocol)
{
switch (protocol)
{
case Protocol.ProtocolNames.AsyncBinary:
return new Protocol.AsyncBinaryProtocol();
case Protocol.ProtocolNames.Thrift:
return new Protocol.ThriftProtocol();
case Protocol.ProtocolNames.CommandLine:
return new Protocol.CommandLineProtocol();
case Protocol.ProtocolNames.DSSBinary:
return new Protocol.DSSBinaryProtocol();
}
return Activator.CreateInstance(Type.GetType(protocol, false));
} /// <summary>
/// 启动服务
/// </summary>
static public void Start()
{
foreach (var server in _listHosts) server.Start();
}
/// <summary>
/// 停止服务
/// </summary>
static public void Stop()
{
foreach (var server in _listHosts) server.Stop();
}
#endregion
}

从上面的代码中,我们看到了自己新加的协议DSSBinary,我们可以在配置文件中对它进行配置,方法和之前说的一样,在这里就不再重复了。

感谢各位的阅读!