没有协议的 C# SuperSocket
C# SuperSocket without protocol
问题很简单:我已经阅读了整个 SuperSocket 文档,但我不明白是否可以在不实施协议的情况下使用它。
我不需要发送特定的命令,而只需要发送一个或数百个字节,具体取决于多种因素。我需要更新一个使用简单套接字的旧 TCP 服务器,它是我在 4 年前使用 System.Net.Sockets 库制作的,我想实现一个更强大的解决方案,使用像 SuperSocket 这样的 well note 库。
这是个好主意吗?
提前致谢。
你不必实现一个协议,你可以通过实现接口简单地创建一个ReceiveFilter
:IReceiveFilter
.
所以首先创建一个自定义 RequestInfo class,如下所示:
public class MyRequestInfo : IRequestInfo
{
public string Key { get; set; }
public string Unicode { get; set; }
// You can add more properties here
}
然后创建 ReceiveFilter - ReceiveFilter 基本上是过滤所有传入消息的 class。如果您不想实施协议,这就是您所需要的。
public class MyReceiveFilter: IReceiveFilter<MyRequestInfo>
{
// This Method (Filter) is called whenever there is a new request from a connection/session
//- This sample method will convert the incomming Byte Array to Unicode string
public MyRequestInfo Filter(byte[] readBuffer, int offset, int length, bool toBeCopied, out int rest)
{
rest = 0;
try
{
var dataUnicode = Encoding.Unicode.GetString(readBuffer, offset, length);
var deviceRequest = new MyRequestInfo { Unicode = dataUnicode };
return deviceRequest;
}
catch (Exception ex)
{
return null;
}
}
public void Reset()
{
throw new NotImplementedException();
}
public int LeftBufferSize { get; }
public IReceiveFilter<MyRequestInfo> NextReceiveFilter { get; }
public FilterState State { get; }
}
下一步是创建自定义 AppSession
。 Session就像客户端连接时服务器为其创建一个会话,当客户端断开连接或服务器关闭连接时被销毁。这适用于需要客户端连接然后服务器必须发送 ACKnowledgment 以便客户端发送下一条消息的情况。
public class MyAppSession : AppSession<MyAppSession, MyRequestInfo>
{
// Properties related to your session.
public int ClientKey { get; set; }
public string SomeProperty { get; set; }
}
最后一步是创建自定义 AppServer
// Here you will be telling the AppServer to use MyAppSession as the default AppSession class and the MyRequestInfo as the defualt RequestInfo
public class MyAppServer : AppServer<MyAppSession, MyRequestInfo>
{
// Here in constructor telling to use MyReceiveFilter and MyRequestInfo
protected MyAppServer() : base(new DefaultReceiveFilterFactory<MyReceiveFilter, MyRequestInfo>())
{
NewRequestReceived += ProcessNewMessage;
}
// This method/event will fire whenever a new message is received from the client/session
// After passing through the filter
// the requestInfo will contain the Unicode string
private void ProcessNewMessage(MyAppSession session, MyRequestInfo requestinfo)
{
session.ClientKey = SessionCount;
// Here you can access the Unicode strings that where generated in the MyReceiveFilter.Filter() Method.
Console.WriteLine(requestinfo.Unicode );
// Do whatever you want
session.Send("Hello World");
session.Close();
}
}
您还可以覆盖 AppServer class 的其他方法,例如:OnSessionClosed
或 OnNewSessionConnected
就是这样 - 然后您只需初始化并启动服务器:
var myAppServer = new MyAppServer();
if (!myAppServer.Setup(2012))
{
_logger.LogMessage(MessageType.Error, string.Format("Failed to setup server"));
return;
}
if (!myAppServer.Start())
{
_logger.LogMessage(MessageType.Error, string.Format("Failed to start server"));
return;
}
问题很简单:我已经阅读了整个 SuperSocket 文档,但我不明白是否可以在不实施协议的情况下使用它。
我不需要发送特定的命令,而只需要发送一个或数百个字节,具体取决于多种因素。我需要更新一个使用简单套接字的旧 TCP 服务器,它是我在 4 年前使用 System.Net.Sockets 库制作的,我想实现一个更强大的解决方案,使用像 SuperSocket 这样的 well note 库。
这是个好主意吗?
提前致谢。
你不必实现一个协议,你可以通过实现接口简单地创建一个ReceiveFilter
:IReceiveFilter
.
所以首先创建一个自定义 RequestInfo class,如下所示:
public class MyRequestInfo : IRequestInfo
{
public string Key { get; set; }
public string Unicode { get; set; }
// You can add more properties here
}
然后创建 ReceiveFilter - ReceiveFilter 基本上是过滤所有传入消息的 class。如果您不想实施协议,这就是您所需要的。
public class MyReceiveFilter: IReceiveFilter<MyRequestInfo>
{
// This Method (Filter) is called whenever there is a new request from a connection/session
//- This sample method will convert the incomming Byte Array to Unicode string
public MyRequestInfo Filter(byte[] readBuffer, int offset, int length, bool toBeCopied, out int rest)
{
rest = 0;
try
{
var dataUnicode = Encoding.Unicode.GetString(readBuffer, offset, length);
var deviceRequest = new MyRequestInfo { Unicode = dataUnicode };
return deviceRequest;
}
catch (Exception ex)
{
return null;
}
}
public void Reset()
{
throw new NotImplementedException();
}
public int LeftBufferSize { get; }
public IReceiveFilter<MyRequestInfo> NextReceiveFilter { get; }
public FilterState State { get; }
}
下一步是创建自定义 AppSession
。 Session就像客户端连接时服务器为其创建一个会话,当客户端断开连接或服务器关闭连接时被销毁。这适用于需要客户端连接然后服务器必须发送 ACKnowledgment 以便客户端发送下一条消息的情况。
public class MyAppSession : AppSession<MyAppSession, MyRequestInfo>
{
// Properties related to your session.
public int ClientKey { get; set; }
public string SomeProperty { get; set; }
}
最后一步是创建自定义 AppServer
// Here you will be telling the AppServer to use MyAppSession as the default AppSession class and the MyRequestInfo as the defualt RequestInfo
public class MyAppServer : AppServer<MyAppSession, MyRequestInfo>
{
// Here in constructor telling to use MyReceiveFilter and MyRequestInfo
protected MyAppServer() : base(new DefaultReceiveFilterFactory<MyReceiveFilter, MyRequestInfo>())
{
NewRequestReceived += ProcessNewMessage;
}
// This method/event will fire whenever a new message is received from the client/session
// After passing through the filter
// the requestInfo will contain the Unicode string
private void ProcessNewMessage(MyAppSession session, MyRequestInfo requestinfo)
{
session.ClientKey = SessionCount;
// Here you can access the Unicode strings that where generated in the MyReceiveFilter.Filter() Method.
Console.WriteLine(requestinfo.Unicode );
// Do whatever you want
session.Send("Hello World");
session.Close();
}
}
您还可以覆盖 AppServer class 的其他方法,例如:OnSessionClosed
或 OnNewSessionConnected
就是这样 - 然后您只需初始化并启动服务器:
var myAppServer = new MyAppServer();
if (!myAppServer.Setup(2012))
{
_logger.LogMessage(MessageType.Error, string.Format("Failed to setup server"));
return;
}
if (!myAppServer.Start())
{
_logger.LogMessage(MessageType.Error, string.Format("Failed to start server"));
return;
}