/// <summary> /// Stop the server. /// </summary> public void Stop() { this.listenSocket.Close(); IsListening = false; var values = new AsyncUserToken[_clients.Count]; _clients.Values.CopyTo(values, 0); foreach (var item in values) { CloseClient(item); } readWritePool.Clear(); //主动回收内存,否则内存不会被马上回收 GC.Collect(); }
private void ProcessSend(SocketAsyncEventArgs e) { if (e.SocketError == SocketError.Success) { AsyncUserToken token = e.UserToken as AsyncUserToken; if (!token.Socket.ReceiveAsync(e)) { this.ProcessReceive(e); } } else { this.ProcessError(e); } }
/// <summary> /// This method is invoked when an asynchronous send operation completes. /// The method issues another receive on the socket to read any additional /// data sent from the client. /// </summary> /// <param name="e">SocketAsyncEventArg associated with the completed send operation.</param> private void ProcessSend(SocketAsyncEventArgs e) { if (e.SocketError == SocketError.Success) { // Done echoing data back to the client. AsyncUserToken token = e.UserToken as AsyncUserToken; if (!token.Socket.ReceiveAsync(e)) { // Read the next block of data send from the client. this.ProcessReceive(e); } } else { this.ProcessError(e); } }
/// <summary> /// This method is invoked when an asynchronous receive operation completes. /// If the remote host closed the connection, then the socket is closed. /// If data was received then the data is echoed back to the client. /// </summary> /// <param name="e">SocketAsyncEventArg associated with the completed receive operation.</param> private void ProcessReceive(SocketAsyncEventArgs e) { try { AsyncUserToken token = e.UserToken as AsyncUserToken; token.LastExchangeTime = Environment.TickCount; token.AsynSocketArgs = e; // Check if the remote host closed the connection. if (e.BytesTransferred > 0) { if (e.SocketError == SocketError.Success) { if (IsSplitPack) { //if (e.BytesTransferred == 16) //{ // //组装心跳 todo:暂时使用,兼容以前的客户端,以前的通讯组件心跳包没加长度 // var buff = new byte[20]; // Array.Copy(BitConverter.GetBytes(16), 0, buff, 0, 4); // Array.Copy(e.Buffer, e.Offset, buff, 4, e.BytesTransferred); // DynamicBufferManager.WriteBuffer(token.SessionId, buff, 0, buff.Length); //} //else { DynamicBufferManager.WriteBuffer(token.SessionId, e.Buffer, e.Offset, e.BytesTransferred); } var list = DynamicBufferManager.PopPackets(token.SessionId); foreach (var item in list) { if (IsUseHeartBeatCertificate && token.IsCertified == false) { token.IsCertified = ValidHeartBeats(item); if (token.IsCertified) { continue; } else { ShowLog("客户端未通过心跳验证,关闭该连接"); CloseClient(token); return; } } else if (IsUseHeartBeatCertificate && item.Length == 16) { //若客户端发送的是心跳数据则跳过 if (ValidHeartBeats(item)) { continue; } } RaiseOnReceive(new DataReceivedArgs(token.SessionId, item)); } } else { var data = new byte[e.BytesTransferred]; Array.Copy(e.Buffer, e.Offset, data, 0, e.BytesTransferred); RaiseOnReceive(new DataReceivedArgs(token.SessionId, data)); } if (token.Socket.Connected) { if (!token.Socket.ReceiveAsync(e)) { // Read the next block of data sent by client. this.ProcessReceive(e); } } } else { this.ProcessError(e); } } else { this.CloseClient(token); } } catch (Exception ex) { this.ProcessError(e); ShowLog(ex); } }