内容发布更新时间 : 2024/11/8 11:31:45星期一 下面是文章的全部内容请认真阅读。
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
var requestInfo= new MyRequestInfo(Guid.NewGuid().ToString(), bodyBuffer.CloneRange(offset, length));
return requestInfo; } }
///
public class MyReceiveFilterFactory : IReceiveFilterFactory
public IReceiveFilter
return new MyReceiveFilter(); } }
///
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); }