/// <summary> /// 服务器启动,构造时需指定最大连接数 /// </summary> /// <param name="max">最大连接数</param> public ServerStart(int max) { //把消息编码方法置入编码委托 this.encode = new Encode(MessageEncoding.Encode); //把消息解码方法置入解码委托 this.decode = new Decode(MessageEncoding.Decode); //把定长解码置入定长解码委托中 this.LD = new LengthDecode(LengthEncoding.Decode); //把定长编码置入定长编码委托中 this.LE = new LengthEncode(LengthEncoding.Encode); //TCP ipv4连接,定义socket this.ServerIPV4 = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); //TCP ipv6连接,定义socket this.ServerIPV6 = new Socket(AddressFamily.InterNetworkV6, SocketType.Stream, ProtocolType.Tcp); this.maxClient = max; //线程池 userTokenPool = new UserTokenPool(max); for (int i = 0; i < max; i++) { UserToken token = new UserToken(); //设置各种接收完成后的调用--读写完成 token.receiveSAEA.Completed += new EventHandler <SocketAsyncEventArgs>(this.IO_Comleted); token.sendSAEA.Completed += new EventHandler <SocketAsyncEventArgs>(this.IO_Comleted); //设置解码/编码 token.LD = this.LD; token.LE = this.LE; token.encode = this.encode; token.decode = this.decode; //设置发送委托和关闭委托 token.sendProcess = new UserToken.SendProcess(this.ProcessSend); token.closeProcess = new UserToken.CloseProcess(this.ClientClose); token.center = this.center; userTokenPool.EnterQuene(token); } }
public void Init() { lengthEncode = LengthEncoding.Encode; lengthDecode = LengthEncoding.Decode; messageEncode = MessageEncoding.Encode; messageDecode = MessageEncoding.Decode; recvPool = new NetPool(); sendPool = new NetPool(); }
public ABSHandlerCenter center; //定义一个消息控制中心类 /// <summary> /// 构造函数,函数最大上线人数 /// </summary> /// <param name="maxNum"></param> public ServerStart(int maxNum) { server = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); //实例化服务器 this.maxNum = maxNum; //最大上线人数 semaphore = new Semaphore(maxNum, maxNum); //初始化信号量 tool = new UserTokenTool(maxNum); //初始化用户管理工具类 LE = LengthCoding.EnCode; //将粘包处理操作给委托 LD = LengthCoding.DeCode; //将分包处理操作给委托 encode = MessageCoding.encode; //将消息体序列化的方法给委托 decode = MessageCoding.decode; //将消息体反序列化的方法给委托 }