예제 #1
0
        public void doDealMsg(byte[] buffer, NetworkStream streamToClient, TcpClient client, List <TcpClient> clients)
        {
            byte[] temp;//临时变量
            Console.WriteLine(getRequest(buffer));
            switch (getRequest(buffer))
            {
            case CommandParam.LOGIN_REQUEST:
                //登录验证
                UserInfo userInfo   = new UserInfo();
                bool     isExitUser = ConectMySqldb.queryUserInfoByUserName(getUsername(buffer), userInfo);
                if (isExitUser)
                {
                    Console.Write("\nusername: {0} ,password: {1} ", userInfo.username, userInfo.password);
                    temp = userInfo.WriteAsBytes(CommandParam.LOGIN_SUCCESS_REPLY);    //返回登陆成功回应
                    try
                    {
                        Name2Client.Add(client, userInfo.username);    //绑定用户名和客户端
                        streamToClient.Write(temp, 0, temp.Length);
                        Console.WriteLine("ClientsCount:{0},Client:{1},username:{2}", Name2Client.Count, Name2Client.ElementAt(0).Key.Client.RemoteEndPoint, Name2Client.ElementAt(0).Value);
                    }
                    catch (ArgumentException ex)
                    {
                        sendOnlyReply(CommandParam.LOGIN_FAIL_REPLY, streamToClient);
                        Console.WriteLine("此用户已经存在:" + ex.Message);
                    }
                }
                else
                {
                    sendOnlyReply(CommandParam.LOGIN_FAIL_REPLY, streamToClient);    //返回失败成功回应
                }
                break;

            case CommandParam.LOADING_REQUEST:
                //载入请求
                ReviewInfo reviewInfo = new ReviewInfo();
                ConectMySqldb.queryReviewInfoByUserName(getUsername(buffer), reviewInfo);
                temp = reviewInfo.WriteAsBytes(CommandParam.LOADING_SUCCESS_REPLY);    //返回载入成功回应
                streamToClient.Write(temp, 0, temp.Length);
                Console.WriteLine("\nbattleNum:{0},victoryNum:{1},escapeNum:{2},singlehighestScore:{3},totalScore:{4}", reviewInfo.battleNum, reviewInfo.victoryNum, reviewInfo.escapeNum, reviewInfo.singlehighestScore, reviewInfo.totalScore);

                break;

            case CommandParam.UPDATE_REQUEST:
                //更新请求
                ReviewInfo new_reviewInfo = new ReviewInfo();
                new_reviewInfo.ReadByBytes(buffer);
                bool isUpdate = ConectMySqldb.updateReviewInfo(new_reviewInfo);
                if (isUpdate)
                {
                    Console.WriteLine("更新成功");
                }
                else
                {
                    Console.WriteLine("更新失败"); sendOnlyReply(CommandParam.UPDATE_FAIL_REPLY, streamToClient);
                }
                break;

            case CommandParam.MATCH_REQUEST:
                //匹配请求
                Console.WriteLine("yes");
                bool isready = getIsReady(buffer, GameState);
                if (clients.Count < 2 || !isready)
                {
                    sendOnlyReply(CommandParam.MATCH_FAIL_REPLY, streamToClient);    //返回匹配失败回应
                }
                else if (clients.Count == 2 && isready)
                {
                    sendOnlyReply(CommandParam.MATCH_SUCCESS_REPLY, streamToClient);    //返回匹配成功回应
                }
                else
                {
                    sendOnlyReply(CommandParam.MATCH_FAIL_REPLY, streamToClient);    //返回匹配失败回应
                                                                                     //return;
                }
                break;

            case CommandParam.BATTLE_REQUEST:
                //发送指令
                if (clients.ElementAt(0) == client)
                {
                    sendToTargetClient(clients.ElementAt(1), buffer);
                    Console.WriteLine("send to {0}", clients.ElementAt(1).Client.RemoteEndPoint);
                }
                else if (clients.ElementAt(1) == client)
                {
                    sendToTargetClient(clients.ElementAt(0), buffer);
                    Console.WriteLine("send to {0}", clients.ElementAt(0).Client.RemoteEndPoint);
                }
                break;

            default:
                sendOnlyReply(CommandParam.IS_ONLINE, streamToClient);    //心跳机制
                break;
            }
        }
예제 #2
0
        /// <summary>
        /// 消息接受,回调函数,执行以下功能
        /// 登陆验证
        /// 载入请求
        /// 匹配请求
        /// 发送指令
        /// </summary>
        /// <param name="ar">异步回调的结果</param>
        private void ReadComplete(IAsyncResult ar)
        {
            int byteRead = 0;

            byte[] temp;//临时变量
            try
            {
                byteRead = streamToClient.EndRead(ar);//接受消息长度

                if (byteRead == 0)
                {
                    Console.WriteLine("Client offine");
                    return;
                }
                Console.WriteLine(getRequest(buffer));
                switch (getRequest(buffer))
                {
                case CommandParam.LOGIN_REQUEST:
                    //登录验证
                    UserInfo userInfo   = new UserInfo();
                    bool     isExitUser = ConectMySqldb.queryUserInfoByUserName(getUsername(buffer), userInfo);
                    if (isExitUser)
                    {
                        Console.Write("\nusername: {0} ,password: {1} ", userInfo.username, userInfo.password);
                        temp = userInfo.WriteAsBytes(CommandParam.LOGIN_SUCCESS_REPLY);    //返回登陆成功回应
                        try
                        {
                            Name2Client.Add(client, userInfo.username);    //绑定用户名和客户端
                            streamToClient.Write(temp, 0, temp.Length);
                            Console.WriteLine("ClientsCount:{0},Client:{1},username:{2}", Name2Client.Count, Name2Client.ElementAt(0).Key.Client.RemoteEndPoint, Name2Client.ElementAt(0).Value);
                        }
                        catch (ArgumentException ex)
                        {
                            sendOnlyReply(CommandParam.LOGIN_FAIL_REPLY, streamToClient);
                            Console.WriteLine("此用户已经存在:" + ex.Message);
                        }
                    }
                    else
                    {
                        sendOnlyReply(CommandParam.LOGIN_FAIL_REPLY, streamToClient);    //返回失败成功回应
                    }
                    break;

                case CommandParam.LOADING_REQUEST:
                    //载入请求
                    ReviewInfo reviewInfo = new ReviewInfo();
                    ConectMySqldb.queryReviewInfoByUserName(getUsername(buffer), reviewInfo);
                    temp = reviewInfo.WriteAsBytes(CommandParam.LOADING_SUCCESS_REPLY);    //返回载入成功回应
                    streamToClient.Write(temp, 0, temp.Length);
                    Console.WriteLine("\nbattleNum:{0},victoryNum:{1},escapeNum:{2},singlehighestScore:{3},totalScore:{4}", reviewInfo.battleNum, reviewInfo.victoryNum, reviewInfo.escapeNum, reviewInfo.singlehighestScore, reviewInfo.totalScore);

                    break;

                case CommandParam.UPDATE_REQUEST:
                    //更新请求
                    ReviewInfo new_reviewInfo = new ReviewInfo();
                    new_reviewInfo.ReadByBytes(buffer);
                    bool isUpdate = ConectMySqldb.updateReviewInfo(new_reviewInfo);
                    if (isUpdate)
                    {
                        Console.WriteLine("更新成功");
                    }
                    else
                    {
                        Console.WriteLine("更新失败"); sendOnlyReply(CommandParam.UPDATE_FAIL_REPLY, streamToClient);
                    }
                    break;

                case CommandParam.MATCH_REQUEST:
                    //匹配请求
                    Console.WriteLine("yes");
                    bool isready = getIsReady(buffer, GameState);
                    if (clients.Count < 2 || !isready)
                    {
                        sendOnlyReply(CommandParam.MATCH_FAIL_REPLY, streamToClient);   //返回匹配失败回应
                    }
                    else if (clients.Count == 2 && isready)
                    {
                        sendOnlyReply(CommandParam.MATCH_SUCCESS_REPLY, streamToClient);    //返回匹配成功回应
                    }
                    else
                    {
                        sendOnlyReply(CommandParam.MATCH_FAIL_REPLY, streamToClient);    //返回匹配失败回应
                        //return;
                    }
                    break;

                case CommandParam.BATTLE_REQUEST:
                    //发送指令
                    if (clients.ElementAt(0) == this.client)
                    {
                        sendToTargetClient(clients.ElementAt(1), buffer);
                        Console.WriteLine("send to {0}", clients.ElementAt(1).Client.RemoteEndPoint);
                    }
                    else if (clients.ElementAt(1) == this.client)
                    {
                        sendToTargetClient(clients.ElementAt(0), buffer);
                        Console.WriteLine("send to {0}", clients.ElementAt(0).Client.RemoteEndPoint);
                    }
                    break;

                default:
                    sendOnlyReply(CommandParam.IS_ONLINE, streamToClient);    //心跳机制
                    break;
                }

                AsyncCallback callback = new AsyncCallback(ReadComplete);
                streamToClient.BeginRead(buffer, 0, BufferSize, callback, null);
            }
            catch (Exception ex) {
                if (streamToClient != null)
                {
                    streamToClient.Dispose();
                    clients.Remove(client);
                }
                client.Close();
                Console.Write(ex.Message);
            }
        }