示例#1
0
 //发送信息给蛇
 private static void wsSend_snakes(string str)
 {
     foreach (var socket in sever.m_asyncSocketList)
     {
         var snakeEntity = socket.snake;
         if (snakeEntity != null)
         {
             if (snakeEntity.State == 1)
             {
                 if (socket.SendEventArgs.SocketError == SocketError.Success)
                 {
                     sever.PublicSend(socket.SendEventArgs, WebSocketClass.PackData(str));
                 }
             }
         }
     }
 }
示例#2
0
        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);
            }
        }
示例#3
0
        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);
            }
        }