public void Init() { m_bufferManager.InitBuffer(); AsyncUserToken userToken; for (int i = 0; i < m_numConnections; i++) { userToken = new AsyncUserToken(m_receiveBufferSize); userToken.ReceiveEventArgs.Completed += new EventHandler <SocketAsyncEventArgs>(IOReceive_Completed); userToken.SendEventArgs.Completed += new EventHandler <SocketAsyncEventArgs>(IOSend_Completed); m_readWritePool.Push(userToken); } }
// Assigns a buffer from the buffer pool to the // specified SocketAsyncEventArgs object // // <returns>true if the buffer was successfully set, else false</returns> public bool SetBuffer(AsyncUserToken args) { if (m_freeIndexPool.Count > 0) { args.ReceiveEventArgs.SetBuffer(m_buffer, m_freeIndexPool.Pop(), m_bufferSize); } else { if ((m_numBytes - m_bufferSize) < m_currentIndex) { return(false); } args.ReceiveEventArgs.SetBuffer(m_buffer, m_currentIndex, m_bufferSize); m_currentIndex += m_bufferSize; } return(true); }
private void ProcessSend(SocketAsyncEventArgs e) { AsyncUserToken token = (AsyncUserToken)e.UserToken; if (e.SocketError == SocketError.Success) { //bool willRaiseEvent = token.Socket.ReceiveAsync(token.ReceiveEventArgs); // if (!willRaiseEvent) // { //ProcessReceive(token.ReceiveEventArgs); // } //ProcessSend(e); } else { CloseClientSocket(e); } }
private void ProcessReceive(SocketAsyncEventArgs e) { // check if the remote host closed the connection AsyncUserToken token = (AsyncUserToken)e.UserToken; if (token.ReceiveEventArgs.BytesTransferred > 0 && token.ReceiveEventArgs.SocketError == SocketError.Success) { //如果是第一次链接的话就发送协议握手 if (token.Handshake == 0) { Console.WriteLine(WebSocketClass.AnalyticData(e.Buffer, e.Buffer.Length)); //e.Buffer:来自client的数据 var websocket_head = WebSocketClass.PackHandShakeData(e.Buffer); //这个必须用ReceiveEventArgs发送数据 token.ReceiveEventArgs.SetBuffer(websocket_head, 0, websocket_head.Length); token.Handshake++; bool willRaiseEvent = token.Socket.SendAsync(token.ReceiveEventArgs); if (!willRaiseEvent) { //ProcessSend(token.SendEventArgs); // ProcessReceive(token.ReceiveEventArgs); } } //握手完成 else if (token.Handshake == 1 && e.SocketError == SocketError.Success) { var websocket_head = WebSocketClass.PackHandShakeData(e.Buffer); //这里应该要验证Sec-WebSocket-Accep(关闭时也要) token.Handshake++; //如果是握手完成的数据就没必要触发发送事件 bool willRaiseEvent = token.Socket.ReceiveAsync(token.ReceiveEventArgs); if (!willRaiseEvent) { ProcessReceive(token.ReceiveEventArgs); } } else { var client_str = WebSocketClass.AnalyticData(e.Buffer, e.Buffer.Length); var snake_info = client_str; //判断是否是double double judge = 0; if (Double.TryParse(snake_info, out judge)) { token.snake.direc_snake(judge); } //如果是接受的是普通数据就没必要触发发送事件 bool willRaiseEvent = token.Socket.ReceiveAsync(token.ReceiveEventArgs); if (!willRaiseEvent) { ProcessReceive(token.ReceiveEventArgs); } } } else { CloseClientSocket(e); } }
private void ProcessAccept(SocketAsyncEventArgs e) { if (e.SocketError != SocketError.Success) { return; //异步处理失败,不做处理 } Interlocked.Increment(ref m_numConnectedSockets); Console.WriteLine("Client connection accepted. There are {0} clients connected to the server", m_numConnectedSockets); AsyncUserToken token = m_readWritePool.Pop(); ((AsyncUserToken)token).Socket = e.AcceptSocket; AsyncUserToken flag_usertoken = null; foreach (var m_asyncsocket in m_asyncSocketList) { if (((IPEndPoint)e.AcceptSocket.LocalEndPoint).Address.ToString() == ((IPEndPoint)m_asyncsocket.Socket.LocalEndPoint).Address.ToString()) { flag_usertoken = m_asyncsocket; } } if (flag_usertoken == null) { ((AsyncUserToken)token).snake.init_snake(); //添加到usertokenlist m_asyncSocketList.Add(token); bool willRaiseEvent = e.AcceptSocket.ReceiveAsync(token.ReceiveEventArgs); if (!willRaiseEvent) { lock (token) { ProcessReceive(token.ReceiveEventArgs); } } StartAccept(e); var dic = new Dictionary <string, string>(); dic["ip"] = ((IPEndPoint)token.Socket.LocalEndPoint).Address.ToString(); PublicSend(token.SendEventArgs, WebSocketClass.PackData(Json.funcObj2JsonStr(dic))); } else { ((AsyncUserToken)token).snake = flag_usertoken.snake; //移除掉线的 m_asyncSocketList.Remove(flag_usertoken); Interlocked.Decrement(ref m_numConnectedSockets); m_maxNumberAcceptedClients.Release(); Console.WriteLine("A client has been disconnected from the server. There are {0} clients connected to the server", m_numConnectedSockets); //添加到usertokenlist m_asyncSocketList.Add(token); bool willRaiseEvent = e.AcceptSocket.ReceiveAsync(token.ReceiveEventArgs); if (!willRaiseEvent) { lock (token) { ProcessReceive(token.ReceiveEventArgs); } } StartAccept(e); } }