예제 #1
0
        public override void OnHandlerMessage(Photon.SocketServer.OperationRequest request, OperationResponse response, ClientPeer peer, SendParameters sendParameters)
        {
            Dictionary <byte, object> parameters = request.Parameters;
            object jsonObject = null;

            parameters.TryGetValue((byte)ParameterCode.User, out jsonObject);
            User user = JsonMapper.ToObject <User>(jsonObject.ToString());

            if (user.Username == "zw" && user.Password == "123")
            {
                response.ReturnCode = (short)ReturnCode.Success;
            }
            else
            {
                response.ReturnCode   = (short)ReturnCode.Fail;
                response.DebugMessage = "用户名或密码错误!";
            }
            //User userDB = manager.GetUserByUsername(user.Username);
            //if (userDB != null && userDB.Password == MD5Tool.GetMD5(user.Password))
            //{
            //    //用户名和密码正确 登录成功
            //    response.ReturnCode = (short)ReturnCode.Success;
            //    peer.LoginUser = userDB;
            //}
            //else
            //{
            //    response.ReturnCode = (short)ReturnCode.Fail;
            //    response.DebugMessage = "用户名或密码错误!";

            //}
        }
예제 #2
0
        public override void OnHandlerMessage(Photon.SocketServer.OperationRequest request, OperationResponse response, ClientPeer peer, SendParameters sendParameters)
        {
            SubCode subCode = ParameterTool.GetParameter <SubCode>(request.Parameters, ParameterCode.SubCode, false);

            response.Parameters.Add((byte)ParameterCode.SubCode, subCode);
            switch (subCode)
            {
            case SubCode.AddTaskDB:
                TaskDB taskDB = ParameterTool.GetParameter <TaskDB>(request.Parameters, ParameterCode.TaskDB);
                taskDB.Role = peer.LoginRole;
                taskDBManager.AddTaskDB(taskDB);
                taskDB.Role = null;
                ParameterTool.AddParameter(response.Parameters, ParameterCode.TaskDB, taskDB);
                response.ReturnCode = (short)ReturnCode.Success;
                break;

            case SubCode.GetTaskDB:
                List <TaskDB> list = taskDBManager.GetTaskDBList(peer.LoginRole);
                foreach (var taskDb in list)
                {
                    taskDb.Role = null;
                }
                ParameterTool.AddParameter(response.Parameters, ParameterCode.TaskDBList, list);
                response.ReturnCode = (short)ReturnCode.Success;

                break;

            case SubCode.UpdateTaskDB:
                TaskDB taskDB2 = ParameterTool.GetParameter <TaskDB>(request.Parameters, ParameterCode.TaskDB);
                taskDB2.Role = peer.LoginRole;
                taskDBManager.UpdateTaskDB(taskDB2);
                response.ReturnCode = (short)ReturnCode.Success;
                break;
            }
        }
예제 #3
0
        /// <summary>
        /// The get leave request.
        /// </summary>
        /// <returns>
        /// a leave request
        /// </returns>
        private static OperationRequest GetLeaveRequest()
        {
            var request = new OperationRequest {
                OperationCode = (byte)OperationCode.Leave
            };

            return(request);
        }
예제 #4
0
        /// <summary>
        /// The get join lobby request.
        /// </summary>
        /// <returns>
        /// a join request
        /// </returns>
        private static OperationRequest GetJoinLobbyRequest()
        {
            var request = new OperationRequest {
                OperationCode = (byte)OperationCode.Join, Parameters = new Dictionary <byte, object>()
            };

            request.Parameters.Add((byte)ParameterKey.GameId, "testlobby");
            return(request);
        }
예제 #5
0
        public override void OnHandlerMessage(Photon.SocketServer.OperationRequest request, OperationResponse response, ClientPeer peer, SendParameters sendParameters)
        {
            List <ServerProperty> list = manager.GetServerList();
            string json = JsonMapper.ToJson(list);
            Dictionary <byte, object> parameters = response.Parameters;

            parameters.Add((byte)ParameterCode.ServerList, json);

            response.OperationCode = request.OperationCode;
        }
예제 #6
0
        //当客户端成功连接服务器时调用

        protected override void OnOperationRequest(Photon.SocketServer.OperationRequest operationRequest, Photon.SocketServer.SendParameters sendParameters)
        {
            //通过opcode获得处理的handler,如果找不到,则使用默认的Handler
            BaseHandler baseHandler = DictTool.GetValue <ResponseCode, BaseHandler>(Main.Instance.HandlerDict, (ResponseCode)operationRequest.OperationCode);

            if (baseHandler != null)
            {
                baseHandler.OnOperationRequest(operationRequest, sendParameters, this);
            }
            else
            {
                DictTool.GetValue <ResponseCode, BaseHandler>(Main.Instance.HandlerDict, (ResponseCode)ResponseCode.Default)
                .OnOperationRequest(operationRequest, sendParameters, this);
            }
        }
예제 #7
0
        public override void OnHandlerMessage(Photon.SocketServer.OperationRequest request, OperationResponse response, ClientPeer peer, SendParameters sendParameters)
        {
            //先得到子操作代码,根据子操作代码,分别进行不同的处理
            SubCode subcode = ParameterTool.GetParameter <SubCode>(request.Parameters, ParameterCode.SubCode, false);


            Dictionary <byte, object> parameters = response.Parameters;

            parameters.Add((byte)ParameterCode.SubCode, subcode);
            response.OperationCode = request.OperationCode;
            switch (subcode)
            {
            case SubCode.AddRole:
                Role role = ParameterTool.GetParameter <Role>(request.Parameters, ParameterCode.Role);
                role.User = peer.LoginUser;
                roleManager.AddRole(role);
                role.User = null;
                ParameterTool.AddParameter(response.Parameters, ParameterCode.Role, role);
                break;

            case SubCode.GetRole:
                List <Role> roleList = roleManager.GetRoleListByUser(peer.LoginUser);
                foreach (var role1 in roleList)
                {
                    role1.User = null;
                }
                ParameterTool.AddParameter(parameters, ParameterCode.RoleList, roleList);
                break;

            case SubCode.SelectRole:
                peer.LoginRole = ParameterTool.GetParameter <Role>(request.Parameters, ParameterCode.Role);
                break;

            case SubCode.UpdateRole:
                Role role2 = ParameterTool.GetParameter <Role>(request.Parameters, ParameterCode.Role);
                role2.User = peer.LoginUser;
                roleManager.UpdateRole(role2);
                role2.User          = null;
                response.ReturnCode = (short)ReturnCode.Success;
                break;
            }
        }
예제 #8
0
        public override void OnHandlerMessage(Photon.SocketServer.OperationRequest request, OperationResponse response, ClientPeer peer, SendParameters sendParameters)
        {
            //List<ServerProperty> list = manager.GetServerList();
            List <ServerProperty> list = new List <ServerProperty>();

            list.Add(new ServerProperty {
                ID = 1, Name = "1区", Count = 100
            });
            list.Add(new ServerProperty {
                ID = 2, Name = "2区", Count = 200
            });

            string json = JsonMapper.ToJson(list);
            Dictionary <byte, object> parameters = response.Parameters;

            parameters.Add((byte)ParameterCode.ServerList, json);

            response.OperationCode = request.OperationCode;
            response.ReturnCode    = (short)ReturnCode.Success;
        }
예제 #9
0
 /// <summary>
 ///   The get leave request.
 /// </summary>
 /// <returns>
 ///   a leave request
 /// </returns>
 private static OperationRequest GetLeaveRequest()
 {
     var request = new OperationRequest { OperationCode = LiteOpCode.Leave };
     return request;
 }
예제 #10
0
        /// <summary>
        ///   The get join request.
        /// </summary>
        /// <returns>
        ///   a join request
        /// </returns>
        private static OperationRequest GetJoinRequest(Dictionary<byte, object> customParameter)
        {
            var request = new OperationRequest { OperationCode = LiteOpCode.Join, Parameters = new Dictionary<byte, object>() };
            request.Parameters.Add((byte)ParameterKey.GameId, "testGame");

            if (customParameter != null)
            {
                foreach (var keyValue in customParameter)
                {
                    request.Parameters.Add(keyValue.Key, keyValue.Value);
                }
            }

            return request;
        }
예제 #11
0
        public void Ping()
        {
            var requests = new OperationRequest[LoopCount];
            var @params  = new Dictionary <byte, object> {
                { 10, new byte[4096] }
            };
            var operation = new OperationRequest {
                OperationCode = (byte)OperationCode.Ping, Parameters = @params
            };

            for (int l = 0; l < LoopCount; l++)
            {
                requests[l] = operation;
            }

            var clients = new TcpClient[ClientCount];

            for (int i = 0; i < ClientCount; i++)
            {
                var endPoint = new IPEndPoint(IPAddress.Parse(Settings.ServerAddress), 4530);
                clients[i] = new TcpClient();
                clients[i].ConnectCompleted  += this.TcpClient_OnConnectCompleted;
                clients[i].ConnectError      += TcpClient_OnConnectError;
                clients[i].Disconnected      += this.TcpClient_OnDisconnected;
                clients[i].Event             += TcpClient_OnEvent;
                clients[i].OperationResponse += this.TcpClient_OnOperationResponse;
                clients[i].Connect(endPoint, "Lite");
            }

            Assert.IsTrue(this.autoReset.WaitOne(1000), "Connect timeout");
            Assert.AreEqual(0, this.disconnect);
            Interlocked.Exchange(ref this.counter, 0);

            Stopwatch stopWatch = Stopwatch.StartNew();

            // join all clients to a game (ping only works if peer has joined a game)
            for (int i = 0; i < clients.Length; i++)
            {
                var request = new OperationRequest {
                    OperationCode = (byte)OperationCode.Join, Parameters = new Dictionary <byte, object>()
                };
                request.Parameters.Add((byte)ParameterKey.GameId, "TestGame" + i);
                Assert.AreEqual(SendResult.Ok, clients[i].SendOperationRequest(request, new SendParameters()));
            }

            Assert.IsTrue(this.autoReset.WaitOne(10000), "wait for join timeout");
            Assert.AreEqual(0, this.disconnect);
            Interlocked.Exchange(ref this.counter, 0);

            Console.WriteLine("sending...");
            foreach (OperationRequest operationRequest in requests)
            {
                foreach (TcpClient t in clients)
                {
                    t.SendOperationRequest(operationRequest, new SendParameters());
                }
            }

            Console.WriteLine("send completed.");

            Assert.IsTrue(this.autoReset.WaitOne(10000), "wait for response timeout");
            Assert.AreEqual(0, this.disconnect);
            LogElapsedTime(log, "Receive took: ", stopWatch.Elapsed, ClientCount * LoopCount);

            foreach (TcpClient t in clients)
            {
                t.Disconnect();
            }

            Assert.IsTrue(this.autoReset.WaitOne(10000), "wait for disconnect timeout");
            Assert.AreEqual(ClientCount, this.disconnect);
        }
        public void Ping()
        {
            var requests = new OperationRequest[LoopCount];
            var @params = new Dictionary<byte, object> { { 10, new byte[4096] } };
            var operation = new OperationRequest { OperationCode = (byte)OperationCode.Ping, Parameters = @params };

            for (int l = 0; l < LoopCount; l++)
            {
                requests[l] = operation;
            }

            var clients = new TcpClient[ClientCount];
            for (int i = 0; i < ClientCount; i++)
            {
                var endPoint = new IPEndPoint(IPAddress.Parse(Settings.ServerAddress), 4530);
                clients[i] = new TcpClient();
                clients[i].ConnectCompleted += this.TcpClient_OnConnectCompleted;
                clients[i].ConnectError += TcpClient_OnConnectError;
                clients[i].Disconnected += this.TcpClient_OnDisconnected;
                clients[i].Event += TcpClient_OnEvent;
                clients[i].OperationResponse += this.TcpClient_OnOperationResponse;
                clients[i].Connect(endPoint, "Lite");
            }

            Assert.IsTrue(this.autoReset.WaitOne(1000), "Connect timeout");
            Assert.AreEqual(0, this.disconnect);
            Interlocked.Exchange(ref this.counter, 0);

            Stopwatch stopWatch = Stopwatch.StartNew();

            // join all clients to a game (ping only works if peer has joined a game)
            for (int i = 0; i < clients.Length; i++)
            {
                var request = new OperationRequest { OperationCode = (byte)OperationCode.Join, Parameters = new Dictionary<byte, object>() };
                request.Parameters.Add((byte)ParameterKey.GameId, "TestGame" + i);
                Assert.AreEqual(SendResult.Ok, clients[i].SendOperationRequest(request, new SendParameters()));
            }

            Assert.IsTrue(this.autoReset.WaitOne(10000), "wait for join timeout");
            Assert.AreEqual(0, this.disconnect);
            Interlocked.Exchange(ref this.counter, 0);

            Console.WriteLine("sending...");
            foreach (OperationRequest operationRequest in requests)
            {
                foreach (TcpClient t in clients)
                {
                    t.SendOperationRequest(operationRequest, new SendParameters());
                }
            }

            Console.WriteLine("send completed.");

            Assert.IsTrue(this.autoReset.WaitOne(10000), "wait for response timeout");
            Assert.AreEqual(0, this.disconnect);
            LogElapsedTime(log, "Receive took: ", stopWatch.Elapsed, ClientCount * LoopCount);

            foreach (TcpClient t in clients)
            {
                t.Disconnect();
            }

            Assert.IsTrue(this.autoReset.WaitOne(10000), "wait for disconnect timeout");
            Assert.AreEqual(ClientCount, this.disconnect);
        }
예제 #13
0
        public void JoinAndJoin()
        {
            var peerOne = new DummyPeer {
                SessionId = "#1"
            };
            var peerTwo = new DummyPeer {
                SessionId = "#2"
            };
            var peerLobby = new DummyPeer {
                SessionId = "#L"
            };
            var litePeerOne   = new LiteLobbyPeer(peerOne.Protocol, peerOne);
            var litePeerTwo   = new LiteLobbyPeer(peerTwo.Protocol, peerTwo);
            var litePeerLobby = new LiteLobbyPeer(peerLobby.Protocol, peerLobby);

            try
            {
                // peer lobby: join
                OperationRequest request = GetJoinLobbyRequest();
                PeerHelper.InvokeOnOperationRequest(litePeerLobby, request, new SendParameters());
                Assert.IsTrue(peerLobby.WaitForNextResponse(WaitTimeout));
                List <OperationResponse> responseList = peerLobby.GetResponseList();
                Assert.AreEqual(1, responseList.Count);

                // peer lobby: receive lobby games
                Assert.IsTrue(peerLobby.WaitForNextEvent(WaitTimeout));
                List <EventData> eventList = peerLobby.GetEventList();
                Assert.AreEqual(1, eventList.Count);
                EventData eventData = eventList[0];
                Assert.AreEqual((byte)LiteLobbyEventCode.GameList, eventData.Code);

                // peer 1: join
                request = GetJoinRequest();
                PeerHelper.InvokeOnOperationRequest(litePeerOne, request, new SendParameters());
                Assert.IsTrue(peerOne.WaitForNextResponse(WaitTimeout));

                responseList = peerOne.GetResponseList();
                Assert.AreEqual(1, responseList.Count);
                OperationResponse response = responseList[0];
                Assert.AreEqual(1, response.Parameters[(byte)ParameterKey.ActorNr]);

                // peer 1: receive own join event
                Assert.IsTrue(peerOne.WaitForNextEvent(WaitTimeout));
                eventList = peerOne.GetEventList();
                Assert.AreEqual(1, eventList.Count);
                eventData = eventList[0];
                Assert.AreEqual(LiteOpCode.Join, eventData.Code);
                Assert.AreEqual(1, eventData.Parameters[(byte)ParameterKey.ActorNr]);

                // peer lobby: wait for lobby update (all 2 sec)
                Assert.IsTrue(peerLobby.WaitForNextEvent(2000 + WaitTimeout));
                eventList = peerLobby.GetEventList();
                Assert.AreEqual(1, eventList.Count);
                eventData = eventList[0];
                var games = (Hashtable)eventData.Parameters[(byte)ParameterKey.Data];
                Assert.AreEqual(1, games.Count);
                Assert.AreEqual("1", games["testGame"]);

                // peer 2: join
                request = GetJoinRequest();
                PeerHelper.InvokeOnOperationRequest(litePeerTwo, request, new SendParameters());
                Assert.IsTrue(peerTwo.WaitForNextResponse(WaitTimeout));

                responseList = peerTwo.GetResponseList();
                Assert.AreEqual(1, responseList.Count);
                response = responseList[0];
                Assert.AreEqual(2, response.Parameters[(byte)ParameterKey.ActorNr]);

                // peer 2: receive own join event
                Assert.IsTrue(peerTwo.WaitForNextEvent(WaitTimeout));
                eventList = peerTwo.GetEventList();
                Assert.AreEqual(1, eventList.Count);
                eventData = eventList[0];
                Assert.AreEqual(LiteOpCode.Join, eventData.Code);
                Assert.AreEqual(2, eventData.Parameters[(byte)ParameterKey.ActorNr]);

                // peer 1: receive join event of peer 2
                Assert.IsTrue(peerOne.WaitForNextEvent(WaitTimeout));
                eventList = peerOne.GetEventList();
                Assert.AreEqual(1, eventList.Count);
                eventData = eventList[0];
                Assert.AreEqual(LiteOpCode.Join, eventData.Code);
                Assert.AreEqual(2, eventData.Parameters[(byte)ParameterKey.ActorNr]);

                // peer lobby: wait for lobby update (all 2 sec)
                Assert.IsTrue(peerLobby.WaitForNextEvent(2000 + WaitTimeout));
                eventList = peerLobby.GetEventList();
                Assert.AreEqual(1, eventList.Count);
                eventData = eventList[0];
                games     = (Hashtable)eventData.Parameters[(byte)ParameterKey.Data];
                Assert.AreEqual(1, games.Count);
                Assert.AreEqual("2", games["testGame"]);

                // peer 2: join again
                request = GetJoinRequest();
                PeerHelper.InvokeOnOperationRequest(litePeerTwo, request, new SendParameters());
                Assert.IsTrue(peerTwo.WaitForNextResponse(WaitTimeout));
                responseList = peerTwo.GetResponseList();
                Assert.AreEqual(1, responseList.Count);

                // peer 2: do not receive own leave event, receive own join event
                Assert.IsTrue(peerTwo.WaitForNextEvent(WaitTimeout));
                eventList = peerTwo.GetEventList();
                Assert.AreEqual(1, eventList.Count);
                eventData = eventList[0];
                Assert.AreEqual(LiteOpCode.Join, eventData.Code);
                Assert.AreEqual(3, eventData.Parameters[(byte)ParameterKey.ActorNr]);

                // peer 1: receive leave and event of peer 2
                Assert.IsTrue(peerOne.WaitForNextEvent(WaitTimeout));
                eventList = peerOne.GetEventList();
                Assert.GreaterOrEqual(eventList.Count, 1);
                eventData = eventList[0];
                Assert.AreEqual(LiteOpCode.Leave, eventData.Code);
                Assert.AreEqual(2, eventData.Parameters[(byte)ParameterKey.ActorNr]);

                if (eventList.Count == 1)
                {
                    // waiting for join event
                    Assert.IsTrue(peerOne.WaitForNextEvent(WaitTimeout));
                    eventList = peerOne.GetEventList();
                    Assert.AreEqual(1, eventList.Count);
                    eventData = eventList[0];
                }
                else
                {
                    eventData = eventList[1];
                }

                Assert.AreEqual(LiteOpCode.Join, eventData.Code);
                Assert.AreEqual(3, eventData.Parameters[(byte)ParameterKey.ActorNr]);

                // peer lobby: wait for lobby update (all 2 sec)
                Assert.IsTrue(peerLobby.WaitForNextEvent(2000 + WaitTimeout));
                eventList = peerLobby.GetEventList();
                Assert.AreEqual(1, eventList.Count);
                eventData = eventList[0];
                games     = (Hashtable)eventData.Parameters[(byte)ParameterKey.Data];
                Assert.AreEqual(1, games.Count);
                Assert.AreEqual("2", games["testGame"]);
            }
            finally
            {
                PeerHelper.SimulateDisconnect(litePeerOne);
                PeerHelper.SimulateDisconnect(litePeerTwo);
                PeerHelper.SimulateDisconnect(litePeerLobby);
            }
        }
예제 #14
0
 /// <summary>
 /// The get join request.
 /// </summary>
 /// <returns>
 /// a join request
 /// </returns>
 private static OperationRequest GetJoinRequest()
 {
     var request = new OperationRequest { OperationCode = (byte)OperationCode.Join, Parameters = new Dictionary<byte, object>() };
     request.Parameters.Add((byte)ParameterKey.GameId, "testGame");
     request.Parameters.Add((byte)LobbyParameterKeys.LobbyId, "testlobby");
     return request;
 }