public void Remove(AsyncSocketUserToken userToken) { lock (m_list) { m_list.Remove(userToken); } }
public void Add(AsyncSocketUserToken userToken) { lock (m_list) { m_list.Add(userToken); } }
public void CopyList(ref AsyncSocketUserToken[] array) { lock (m_list) { array = new AsyncSocketUserToken[m_list.Count]; m_list.CopyTo(array); } }
// Add a SocketAsyncEventArg instance to the pool // //The "item" parameter is the SocketAsyncEventArgs instance // to add to the pool public void Push(AsyncSocketUserToken item) { if (item == null) { throw new ArgumentNullException("Items added to a SocketAsyncEventArgsPool cannot be null"); } lock (m_pool) { m_pool.Push(item); } }
public void Init() { bufferManager.InitBuffer(); AsyncSocketUserToken userToken; for (int i = 0; i < numConnections; i++) { userToken = new AsyncSocketUserToken(receiveBufferSize); userToken.RecevicveEventArgs.Completed += new EventHandler <SocketAsyncEventArgs>(IO_Completed); userToken.SendEventArgs.Completed += new EventHandler <SocketAsyncEventArgs>(IO_Completed); asyncSocketAsyncEventArgsPool.Push(userToken); } }
public AsyncSocketInvokeElement(AsyncSocketServer asyncSocketServer, AsyncSocketUserToken asyncSocketUserToken) { this.asyncSocketServer = asyncSocketServer; this.asyncSocketUserToken = asyncSocketUserToken; netByteOrder = false; incomingDataParser = new IncomingDataParser(); outgoingDataAssembler = new OutgoingDataAssembler(); sendAsync = false; connectDT = DateTime.UtcNow; activeDT = DateTime.UtcNow; }
private void ProcessReceive(SocketAsyncEventArgs AsyncEventArgs) { AsyncSocketUserToken userToken = AsyncEventArgs.UserToken as AsyncSocketUserToken; if (userToken.Socket == null) { return; } if (userToken.RecevicveEventArgs.BytesTransferred > 0 && userToken.RecevicveEventArgs.SocketError == SocketError.Success) { int offset = userToken.RecevicveEventArgs.Offset; int count = userToken.RecevicveEventArgs.BytesTransferred; if ((userToken.AsyncSocketInvokeElement == null) & (userToken.Socket != null)) { BuildingSocketInvokeElement(userToken); offset += 1; count -= 1; } if (userToken.AsyncSocketInvokeElement == null)//没有协议对象 { CloseClientSocket(userToken); } else { if (count > 0) { if (!userToken.AsyncSocketInvokeElement.ProcessReceive(userToken.RecevicveEventArgs.Buffer, offset, count)) { CloseClientSocket(userToken); } } else { bool willRaiseEvent = userToken.Socket.ReceiveAsync(userToken.RecevicveEventArgs); if (!willRaiseEvent) { ProcessReceive(userToken.RecevicveEventArgs); } } } } else { CloseClientSocket(userToken); } }
//异步发送操作完成时,将调用此方法。 //方法问题另一个接收套接字读取任何额外的 //从客户端发送的数据 // // <param name="e"></param> private bool ProcessSend(SocketAsyncEventArgs e) { AsyncSocketUserToken userToken = e.UserToken as AsyncSocketUserToken; if (userToken.AsyncSocketInvokeElement == null) { return(false); } if (e.SocketError == SocketError.Success) { return(userToken.AsyncSocketInvokeElement.SendCompleted()); } else { CloseClientSocket((AsyncSocketUserToken)e.UserToken); return(false); } }
private void CloseClientSocket(AsyncSocketUserToken userToken) { Console.Write("关闭客户端连接,本地地址:{0},远程地址:{1}", userToken.Socket.LocalEndPoint, userToken.Socket.RemoteEndPoint); if (userToken.Socket == null) { return; } try { userToken.Socket.Shutdown(SocketShutdown.Both); } catch (Exception) { } userToken.Socket.Close(); userToken.Socket = null; maxNumberAcceptedClients.Release(); asyncSocketAsyncEventArgsPool.Push(userToken); asyncSocketUserTokenList.Remove(userToken); }
private void ProcessAccept(SocketAsyncEventArgs acceptEventArgs) { Console.WriteLine("客户端已连接,本地地址:{0},远程地址{1}", acceptEventArgs.AcceptSocket.LocalEndPoint, acceptEventArgs.AcceptSocket.RemoteEndPoint); AsyncSocketUserToken userToken = asyncSocketAsyncEventArgsPool.Pop(); asyncSocketUserTokenList.Add(userToken);//添加到正在连接列表 userToken.Socket = acceptEventArgs.AcceptSocket; userToken.ConnectDateTime = DateTime.Now; // As soon as the client is connected, post a receive to the connection bool willRaiseEvent = userToken.Socket.ReceiveAsync(userToken.RecevicveEventArgs); if (!willRaiseEvent) { lock (userToken) { ProcessReceive(userToken.RecevicveEventArgs); } } StartAccept(acceptEventArgs); }
// This method is called whenever a receive or send operation is completed on a socket // // <param name="e">SocketAsyncEventArg associated with the completed receive operation</param> void IO_Completed(object sender, SocketAsyncEventArgs asyncEventArgs) { AsyncSocketUserToken userToken = asyncEventArgs.UserToken as AsyncSocketUserToken; // determine which type of operation just completed and call the associated handler lock (userToken) { switch (asyncEventArgs.LastOperation) { case SocketAsyncOperation.Receive: ProcessReceive(asyncEventArgs); break; case SocketAsyncOperation.Send: ProcessSend(asyncEventArgs); break; default: throw new ArgumentException("The last operation completed on the socket was not a receive or send"); } } }
private void BuildingSocketInvokeElement(AsyncSocketUserToken userToken) { userToken.AsyncSocketInvokeElement = new AsyncSocketInvokeElement(this, userToken); }