supersocket_SocketEngin_BootstrapFactory.cs详解 下载本文

内容发布更新时间 : 2024/6/24 3:06:30星期一 下面是文章的全部内容请认真阅读。

upersocket/SocketEngin/BootstrapFactory.cs详解

using System;

using System.Collections.Generic; using System.Linq; using System.Text;

using SuperSocket.Common; using SuperSocket.Facility.Protocol; using SuperSocket.SocketBase;

using SuperSocket.SocketBase.Protocol; using SuperSocket.SocketEngine; namespace OilServer {

class Program {

static void Main(string[] args) {

#region 从配置文件启动

var bootstrap = BootstrapFactory.CreateBootstrap(); if (!bootstrap.Initialize()) {

Console.WriteLine(\Console.ReadKey(); return; }

var result = bootstrap.Start();

Console.WriteLine(\if (result == StartResult.Failed) {

Console.WriteLine(\Console.ReadKey(); return; }

Console.WriteLine(\while (Console.ReadKey().KeyChar != 'q') {

Console.WriteLine(); continue; }

Console.WriteLine(); //Stop the appServer bootstrap.Stop();

Console.WriteLine(\Console.ReadKey(); #endregion #region 编程启动

//MyServer oilServer=new MyServer(); //oilServer.Setup(\//oilServer.Start();

//Console.WriteLine(\输入任意键结束...\//Console.ReadLine(); //oilServer.Stop(); #endregion } }

///

/// 某个完整请求 ///

public class MyRequestInfo: RequestInfo {

public MyRequestInfo(string key, byte[] body) : base(key, body) { } }

///

/// 请求解析 ///

public class MyReceiveFilter : FixedHeaderReceiveFilter {

public MyReceiveFilter(): base(4)//头部为4个字节 { }

protected override int GetBodyLengthFromHeader(byte[] header, int offset, int length) { int len =

BitConverter.ToInt32(

new byte[] {header[offset + 3],header[offset+2], header[offset + 1], header[offset] }, 0); return len; }

protected override MyRequestInfo ResolveRequestInfo(ArraySegment header, byte[] bodyBuffer, int offset, int length) {

var requestInfo= new MyRequestInfo(Guid.NewGuid().ToString(), bodyBuffer.CloneRange(offset, length));

return requestInfo; } }

///

/// 请求解析工厂 ///

public class MyReceiveFilterFactory : IReceiveFilterFactory {

public IReceiveFilter CreateFilter(IAppServer appServer, IAppSession appSession, System.Net.IPEndPoint remoteEndPoint) {

return new MyReceiveFilter(); } }

///

/// 某个socket会话 ///

public class MyOilSession : AppSession {

public MyOilSession() {

this.Charset = System.Text.UTF8Encoding.UTF8; }

public MyOilClient Client { get; private set; } protected override void OnSessionStarted() {

//this.Client.SessionRegiste(this); base.OnSessionStarted(); }

protected override void OnSessionClosed(CloseReason reason) {

//add your business operations //this.Client.SessionUnRegiste(this); }

protected override void HandleUnknownRequest(MyRequestInfo requestInfo) {

base.HandleUnknownRequest(requestInfo); }

protected override void HandleException(Exception e) {

base.HandleException(e); }