c#ネットワーク通信フレームワークnetworkcommsカーネル解析の9カスタム処理方法の実行メカニズム
15685 ワード
NetworkCommsネットワーク通信フレームワークの概要
本稿はnetworkcomms 2に基づく.3.1オープンソースバージョンgplv 3プロトコル
私たちが自分で書いた処理方法をカスタム処理方法と呼びます
たとえば、サーバにログインに関する処理方法を書きます.
以上のように,サーバがメッセージタイプが"ReqLogin"であるメッセージを受信すると,関連する処理方法が自動的に取得される.
背後にある運行メカニズムを見てみましょう.
まず、登録文を参照してください.
辞書:
私たちは上の一言を見ました.
PacketTypeHandlerDelegateWrapperクラスを見てみましょう.このような処理方法をパラメータとしています.
このようなものを見てみましょう.
この章はよく書けないような気がします.レベルが限られているので、自分でnetworkcomms 2を読むことができます.3.1オープンソースバージョンのソースコードは、より明確に表示されます.
www.cnblogs.com/networkcomms
www.networkcomms.cn
本稿はnetworkcomms 2に基づく.3.1オープンソースバージョンgplv 3プロトコル
私たちが自分で書いた処理方法をカスタム処理方法と呼びます
たとえば、サーバにログインに関する処理方法を書きます.
NetworkComms.AppendGlobalIncomingPacketHandler<LoginContract>("ReqLogin", IncomingLoginRequest);
private void IncomingLoginRequest(PacketHeader header, Connection connection, LoginContract loginContract)
{
try
{
string resMsg="";
// , ,
if (loginContract.UserID == "1000" && loginContract.PassWord == "123")
resMsg = " ";
else
resMsg = " ";
// ,
ResMsgContract contract = new ResMsgContract();
contract.Message = resMsg;
connection.SendObject("ResLogin", contract);
}
catch (Exception ex)
{
}
}
以上のように,サーバがメッセージタイプが"ReqLogin"であるメッセージを受信すると,関連する処理方法が自動的に取得される.
背後にある運行メカニズムを見てみましょう.
まず、登録文を参照してください.
NetworkComms.AppendGlobalIncomingPacketHandler<LoginContract>("ReqLogin", IncomingLoginRequest);
AppendGlobalIncomingPacketHandler() , ”ReqLogin" “INcomingLoginRequest" NetworkComms :
AppendGlobalIncomingPacketHander<T> , IncomingLoginRequest PacketHandlerCallBackDelegate<T> 。
PacketHandlerCallBackDelegate
public delegate void PacketHandlerCallBackDelegate<T>(PacketHeader packetHeader, Connection connection, T incomingObject);
AppendGlobalIncomingPacketHandler() :
public static void AppendGlobalIncomingPacketHandler<T>(string packetTypeStr, PacketHandlerCallBackDelegate<T> packetHandlerDelgatePointer)
{
if (packetTypeStr == null) throw new ArgumentNullException("packetTypeStr", "Provided packetType string cannot be null.");
if (packetHandlerDelgatePointer == null) throw new ArgumentNullException("packetHandlerDelgatePointer", "Provided PacketHandlerCallBackDelegate<T> cannot be null.");
if (sendReceiveOptions == null) throw new ArgumentNullException("sendReceiveOptions", "Provided SendReceiveOptions cannot be null.");
lock (globalDictAndDelegateLocker)
{
if (globalIncomingPacketUnwrappers.ContainsKey(packetTypeStr))
{
//Make sure if we already have an existing entry that it matches with the provided
if (!globalIncomingPacketUnwrappers[packetTypeStr].Options.OptionsCompatible(sendReceiveOptions))
throw new PacketHandlerException("The proivded SendReceiveOptions are not compatible with existing SendReceiveOptions already specified for this packetTypeStr.");
}
else
globalIncomingPacketUnwrappers.Add(packetTypeStr, new PacketTypeUnwrapper(packetTypeStr, sendReceiveOptions));
//Ad the handler to the list
if (globalIncomingPacketHandlers.ContainsKey(packetTypeStr))
{
//Make sure we avoid duplicates
PacketTypeHandlerDelegateWrapper<T> toCompareDelegate = new PacketTypeHandlerDelegateWrapper<T>(packetHandlerDelgatePointer);
bool delegateAlreadyExists = false;
foreach (var handler in globalIncomingPacketHandlers[packetTypeStr])
{
if (handler == toCompareDelegate)
{
delegateAlreadyExists = true;
break;
}
}
if (delegateAlreadyExists)
throw new PacketHandlerException("This specific packet handler delegate already exists for the provided packetTypeStr.");
//
globalIncomingPacketHandlers[packetTypeStr].Add(new PacketTypeHandlerDelegateWrapper<T>(packetHandlerDelgatePointer));
}
else
globalIncomingPacketHandlers.Add(packetTypeStr, new List<IPacketTypeHandlerDelegateWrapper>() { new PacketTypeHandlerDelegateWrapper<T>(packetHandlerDelgatePointer) });
if (LoggingEnabled) logger.Info("Added incoming packetHandler for '" + packetTypeStr + "' packetType.");
}
}
辞書:
static Dictionary<string, List<IPacketTypeHandlerDelegateWrapper>> globalIncomingPacketHandlers = new Dictionary<string, List<IPacketTypeHandlerDelegateWrapper>>();
私たちは上の一言を見ました.
PacketTypeHandlerDelegateWrapper<T> toCompareDelegate = new PacketTypeHandlerDelegateWrapper<T>(packetHandlerDelgatePointer);
PacketTypeHandlerDelegateWrapperクラスを見てみましょう.このような処理方法をパラメータとしています.
このようなものを見てみましょう.
class PacketTypeHandlerDelegateWrapper<T> : IPacketTypeHandlerDelegateWrapper
{
NetworkComms.PacketHandlerCallBackDelegate<T> innerDelegate;
public PacketTypeHandlerDelegateWrapper(NetworkComms.PacketHandlerCallBackDelegate<T> packetHandlerDelegate)
{
this.innerDelegate = packetHandlerDelegate;
}
public object DeSerialize(MemoryStream incomingBytes, SendReceiveOptions options)
{
if (incomingBytes == null) return null;
//if (incomingBytes == null || incomingBytes.Length == 0) return null;
else
//{
//if (options.DataSerializer == null)
// throw new ArgumentNullException("options", "The provided options.DataSerializer was null. Cannot continue with deserialise.");
return options.DataSerializer.DeserialiseDataObject<T>(incomingBytes, options.DataProcessors, options.Options);
//}
}
public void Process(PacketHeader packetHeader, Connection connection, object obj)
{
innerDelegate(packetHeader, connection, (obj == null ? default(T) : (T)obj));
}
public bool Equals(IPacketTypeHandlerDelegateWrapper other)
{
if (innerDelegate == (other as PacketTypeHandlerDelegateWrapper<T>).innerDelegate)
return true;
else
return false;
}
public bool EqualsDelegate(Delegate other)
{
return other as NetworkComms.PacketHandlerCallBackDelegate<T> == innerDelegate;
}
}
この章はよく書けないような気がします.レベルが限られているので、自分でnetworkcomms 2を読むことができます.3.1オープンソースバージョンのソースコードは、より明確に表示されます.
www.cnblogs.com/networkcomms
www.networkcomms.cn