C# 常用协议实现模版及FixedSizeReceiveFilter示例(SuperSocket入门)
简介
SuperSocket是一款开源的基于.NET平台的高性能Socket服务器框架,支持多种协议(如TCP、UDP、WebSocket等),并提供了基于协议的使用模板,便于快速开发网络应用程序。
本文将介绍使用SuperSocket实现通信协议的方法,以及其中的模板和Fixed Size Receive Filter的示例。
常用协议实现模板
SuperSocket提供了许多常用协议的实现模板,可直接使用并进行相关的配置即可。
以下是常用协议实现模板的示例代码:
基于TCP的Echo协议
using SuperSocket.ProtoBase;
using SuperSocket.SocketBase;
using SuperSocket.SocketBase.Config;
using SuperSocket.SocketEngine;
using System;
using System.Collections.Generic;
namespace SuperSocketEchoServer
{
public class EchoReceiveFilter : TerminatorReceiveFilter<EchoRequest>
{
protected override EchoRequest ProcessMatchedRequest(byte[] buffer, int offset, int length, object context)
{
return new EchoRequest { Content = Encoding.ASCII.GetString(buffer, offset, length) };
}
}
public class EchoRequest : IRequest
{
public string Content { get; set; }
}
public class EchoServer : AppServer<EchoSession, EchoRequest>
{
protected override void OnSessionClosed(EchoSession session, CloseReason reason)
{
Console.WriteLine("Connection closed: {0}\n", session.RemoteEndPoint.Address);
base.OnSessionClosed(session, reason);
}
protected override void OnNewSessionConnected(EchoSession session)
{
Console.WriteLine("New connection established: {0}\n", session.RemoteEndPoint.Address);
base.OnNewSessionConnected(session);
}
}
public class EchoSession : AppSession<EchoSession, EchoRequest>
{
}
class Program
{
static void Main(string[] args)
{
var appServer = new EchoServer();
if(!appServer.Setup(2016))
{
Console.WriteLine("Failed to setup!");
Console.ReadKey();
return;
}
if (!appServer.Start())
{
Console.WriteLine("Failed to start!");
Console.ReadKey();
return;
}
Console.WriteLine("Server started!\n");
Console.ReadKey();
}
}
}
基于WebSocket的Echo协议
using SuperSocket.WebSocket;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace SuperWebSocketEchoServer
{
public class WebSocketEchoServer : WebSocketServer<WebSocketEchoSession>
{
protected override void OnSessionClosed(WebSocketEchoSession session, CloseReason reason)
{
Console.WriteLine("Connection closed: {0}\n", session.RemoteEndPoint.Address);
base.OnSessionClosed(session, reason);
}
protected override void OnNewSessionConnected(WebSocketEchoSession session)
{
Console.WriteLine("New connection established: {0}\n", session.RemoteEndPoint.Address);
base.OnNewSessionConnected(session);
}
}
public class WebSocketEchoSession : WebSocketSession<WebSocketEchoSession>
{
protected override void OnMessageReceived(string message)
{
Send(message);
}
}
class Program
{
static void Main(string[] args)
{
var appServer = new WebSocketEchoServer();
if (!appServer.Setup(2016))
{
Console.WriteLine("Failed to setup!");
Console.ReadKey();
return;
}
if (!appServer.Start())
{
Console.WriteLine("Failed to start!");
Console.ReadKey();
return;
}
Console.WriteLine("Server started!\n");
Console.ReadKey();
}
}
}
Fixed Size Receive Filter示例
使用Fixed Size Receive Filter,可以对接收到的数据进行指定长度的分组,便于后续的数据处理。
以下是使用Fixed Size Receive Filter的示例代码:
using SuperSocket.ProtoBase;
using SuperSocket.SocketBase;
using SuperSocket.SocketBase.Protocol;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace SuperSocketFixedSizeServer
{
public class FixedSizeRequest : IRequestInfo
{
public string Key { get; set; }
public byte[] Body { get; set; }
}
public class FixedSizeReceiveFilter : FixedSizeReceiveFilter<FixedSizeRequest>
{
public FixedSizeReceiveFilter() : base(5)
{
}
protected override FixedSizeRequest ProcessMatchedRequest(byte[] buffer)
{
return new FixedSizeRequest
{
Key = Encoding.ASCII.GetString(buffer, 0, 2),
Body = buffer.Skip(2).Take(3).ToArray()
};
}
}
public class FixedSizeServer : AppServer<FixedSizeSession, FixedSizeRequest>
{
protected override void OnSessionClosed(FixedSizeSession session, CloseReason reason)
{
Console.WriteLine("Connection closed: {0}\n", session.RemoteEndPoint.Address);
base.OnSessionClosed(session, reason);
}
protected override void OnNewSessionConnected(FixedSizeSession session)
{
Console.WriteLine("New connection established: {0}\n", session.RemoteEndPoint.Address);
base.OnNewSessionConnected(session);
}
}
public class FixedSizeSession : AppSession<FixedSizeSession, FixedSizeRequest>
{
}
class Program
{
static void Main(string[] args)
{
var appServer = new FixedSizeServer();
appServer.NewSessionConnected += new SessionHandler<FixedSizeSession>(appServer_NewSessionConnected);
appServer.NewRequestReceived += new RequestHandler<FixedSizeSession, FixedSizeRequest>(appServer_NewRequestReceived);
if (!appServer.Setup(2016))
{
Console.WriteLine("Failed to setup!");
Console.ReadKey();
return;
}
if (!appServer.Start())
{
Console.WriteLine("Failed to start!");
Console.ReadKey();
return;
}
Console.WriteLine("Server started!\n");
Console.ReadKey();
}
static void appServer_NewSessionConnected(FixedSizeSession session)
{
Console.WriteLine("New session connected: {0}\n", session.RemoteEndPoint.Address);
}
static void appServer_NewRequestReceived(FixedSizeSession session, FixedSizeRequest requestInfo)
{
Console.WriteLine("Request received: {0},{1}\n", requestInfo.Key, Encoding.ASCII.GetString(requestInfo.Body));
session.Send("Received");
}
}
}
在以上示例代码中,指定了数据分组的长度为5,即2个字符为Key,后面3个字符为Body,可根据实际需求修改分组的长度。
总结
本文介绍了使用SuperSocket实现通信协议的方法,以及其中的模板和Fixed Size Receive Filter的示例,由于篇幅所限,只给出了Echo协议和Fixed Size协议的示例代码,实际应用中还可以根据业务需求来选择合适的协议类型,进行相关开发和配置。
本站文章如无特殊说明,均为本站原创,如若转载,请注明出处:C# 常用协议实现模版及FixedSizeReceiveFilter示例(SuperSocket入门) - Python技术站