private void InitClient() { //绑定当收到服务器发送的消息后的处理事件 this.HandleRecMsg = new Action <byte[], SocketClient>((bytes, theClient) => { if (bytes != null && bytes.Length > 0) { //缓冲区有剩余包 List <byte> bufferList = new List <byte>(); while (!theClient.BufferByte.IsEmpty) { byte b = 0; if (theClient.BufferByte.TryDequeue(out b)) { bufferList.Add(b); } } //包的字节数据 List <byte> bytesContent = new List <byte>(); //把剩余的包字节跟新的包合并起来 if (bufferList.Count > 0) { //把缓冲区的字节拿出来放到新收到字节的前面 bytesContent = bufferList.Concat(bytes).ToList(); } else { bytesContent = bytes.ToList(); } //此类可以共用一下. IpByteData ibda = new IpByteData(); ibda.SetBytes(bytesContent.ToArray()); //多余的包字节缓冲起来 if (ibda.RemainingBytes.Count > 0) { for (int i = 0; i < ibda.RemainingBytes.Count; i++) { theClient.BufferByte.Enqueue(ibda.RemainingBytes[i]); } } //解压完整的包 if (ibda.BytesComplete.Count > 0) { foreach (List <byte> item in ibda.BytesComplete) { string requestKey = ""; string responseContent = ""; IpByteData.GetContentAndRequetKey(item.ToArray(), out requestKey, out responseContent); //万一没加入成功,则开奖失败, theClient.ResponseContent.TryAdd(requestKey, responseContent); // theClient.ResponseContent.Add(requestKey, responseContent); } } } }); //绑定向服务器发送消息后的处理事件 this.HandleSendMsg = new Action <byte[], SocketClient>((bytes, theClient) => { // Console.WriteLine($"向服务器发送消息:{msg}"); }); }
/// <summary> /// 初始化事件 /// </summary> private void InitServer() { //处理从客户端收到的消息 this.HandleRecMsg = new Action <byte[], SocketConnection, SocketServer>((bytes, conn, theServer) => { string keyip = conn.ClientIp; if (bytes != null && bytes.Length > 0) { //此类可以共用一下. IpByteData ibda = new IpByteData(); if (!theServer.BufferByte.ContainsKey(keyip)) { ibda.SetBytes(bytes); //多余的包字节缓冲起来 if (ibda.RemainingBytes.Count > 0) { theServer.BufferByte.TryAdd(keyip, ibda.RemainingBytes); } //解压完整的包 if (ibda.BytesComplete.Count > 0) { foreach (List <byte> item in ibda.BytesComplete) { string msgAll = Encoding.UTF8.GetString(item.ToArray()); //Console.WriteLine($"收到来自【{ conn.ClientIp }】的消息:{msgAll }"); //conn.Send(msgAll); string requestKey = ""; string responseContent = ""; IpByteData.GetContentAndRequetKey(item.ToArray(), out requestKey, out responseContent); theServer.ResponseContent.Invoke(requestKey, responseContent, conn); } } } else { //如果存在缓冲。 则需要先把缓冲里面的字节拿出来。 List <byte> listBufferByte = new List <byte>(); if (theServer.BufferByte.TryRemove(keyip, out listBufferByte)) { //把缓冲区的字节拿出来放到新收到字节的前面 byte[] bufferBytes = listBufferByte.ToArray(); byte[] allBytes = bufferBytes.Concat(bytes).ToArray(); ibda.SetBytes(allBytes); //多余的包字节缓冲起来 if (ibda.RemainingBytes.Count > 0) { theServer.BufferByte.TryAdd(keyip, ibda.RemainingBytes); } //解压完整的包 if (ibda.BytesComplete.Count > 0) { foreach (List <byte> item in ibda.BytesComplete) { string msgAll = Encoding.UTF8.GetString(item.ToArray()); Console.WriteLine($"收到来自【{ conn.ClientIp }】的消息【缓冲区】:{msgAll}"); } } } else { if (theServer.BufferByte.ContainsKey(keyip)) { //把byte 继续缓冲起来。 theServer.BufferByte[keyip].AddRange(bytes); } } } } }); //处理服务器启动后事件 this.HandleServerStarted = new Action <SocketServer>(theServer => { Console.WriteLine("服务已启动************"); }); //处理新的客户端连接后的事件 this.HandleNewClientConnected = new Action <SocketServer, SocketConnection>((theServer, theCon) => { Console.WriteLine($@"一个新的客户端接入,当前连接数:{theServer.GetConnectionCount()}"); }); //处理客户端连接关闭后的事件 this.HandleClientClose = new Action <SocketConnection, SocketServer>((theCon, theServer) => { Console.WriteLine($@"一个客户端关闭,当前连接数为:{theServer.GetConnectionCount()}"); }); //处理异常 this.HandleException = new Action <Exception>(ex => { Console.WriteLine(ex.Message); }); }