/// <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> public void ProcessSend(SocketAsyncEventArgs e) { if (e.SocketError == SocketError.Success) { // Done echoing data back to the client. //Socket s = e.UserToken as Socket; //TSessionBase TB = e.UserToken as TSessionBase; //Socket s = TB.MSocket; //ReadWritePool.Push(e); //// Read the next block of data send from the client. //Boolean willRaiseEvent = s.ReceiveAsync(e); //if (!willRaiseEvent) //{ // this.ProcessReceive(e); //} TSessionBase TB = e.UserToken as TSessionBase; TB.BCanSend = true; //e.SetBuffer(null, 0, 0); } else { this.CloseClientSocket(e); } }
/// <summary> /// Close the socket associated with the client. /// </summary> /// <param name="e">SocketAsyncEventArg associated with the completed send/receive operation.</param> public void CloseClientSocket(SocketAsyncEventArgs e) { //if (null == e.UserToken) //{ // //this.readWritePool.Push(e); // return; //} //Socket s = e.UserToken as Socket; TSessionBase TB = e.UserToken as TSessionBase; Socket s = TB.MSocket; try { s.Shutdown(SocketShutdown.Send); } catch (Exception) { // Throws if client process has already closed. } s.Close(); // Decrement the counter keeping track of the total number of clients connected to the server. this.semaphoreAcceptedClients.Release(); Interlocked.Decrement(ref this.numConnectedSockets); Console.WriteLine("A client has been disconnected from the server. There are {0} clients connected to the server", this.numConnectedSockets); //e.AcceptSocket = null; // Free the SocketAsyncEventArg so they can be reused by another client. this.readWritePool.Push(e); }
public virtual void Store(byte[] datagramBytes, TSessionBase session) { }
/// <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> public void ProcessReceive(SocketAsyncEventArgs e) { TSessionBase TB = e.UserToken as TSessionBase; Socket s = TB.MSocket; if (TB.State != TSessionState.Active) { return; } if (!s.Connected) { TB.SetInactive(); return; } try { if (e.BytesTransferred == 0) { TB.DisconnectType = TDisconnectType.Normal; TB.State = TSessionState.Inactive; } else // 正常数据包 { if (e.SocketError == SocketError.Success) { TB.LastSessionTime = DateTime.Now; // 合并报文,按报文头、尾字符标志抽取报文,将包交给数据处理器 TB.ResolveSessionBuffer(e); //TB.ReceiveDatagram(); // 继续接收 Boolean willRaiseEvent = s.ReceiveAsync(e); if (!willRaiseEvent) { this.ProcessReceive(e); } } else { this.CloseClientSocket(e); } } } catch (Exception err) // 读 socket 异常,关闭该会话,系统不认为是错误(这种错误可能太多) { if (TB.State == TSessionState.Active) { TB.DisconnectType = TDisconnectType.Exception; TB.State = TSessionState.Inactive; this.CloseClientSocket(e); TB.OnSessionReceiveException(err); } } //// Check if the remote host closed the connection. //if (e.BytesTransferred > 0) //{ // if (e.SocketError == SocketError.Success) // { // TSessionBase TB = e.UserToken as TSessionBase; // Socket s = TB.MSocket; // //Socket s = e.UserToken as Socket; // Int32 bytesTransferred = e.BytesTransferred; // // Get the message received from the listener. // String received = Encoding.ASCII.GetString(e.Buffer, e.Offset, bytesTransferred); // // Increment the count of the total bytes receive by the server. // Interlocked.Add(ref this.totalBytesRead, bytesTransferred); // Console.WriteLine("Received: \"{0}\". The server has read a total of {1} bytes.", received, this.totalBytesRead); // // Format the data to send to the client. // Byte[] sendBuffer = Encoding.ASCII.GetBytes("Returning " + received); // // Set the buffer to send back to the client. // e.SetBuffer(sendBuffer, 0, sendBuffer.Length); // Boolean willRaiseEvent = s.SendAsync(e); // if (!willRaiseEvent) // { // this.ProcessSend(e); // } // } // else // { // this.CloseClientSocket(e); // } //} }