public bool HandleMessage(IMessage message, IServerPeer peer) { var clientPeer = connectionCollection.GetPeers <IClientPeer>().FirstOrDefault(p => p.PeerId == new Guid((Byte[])message.Parameters[(byte)MessageParameterCode.PeerIdParameterCode])); connectionCollection.Disconnect(clientPeer); // send notify about disconnect to the sub servers log.DebugFormat("Client disconnect. Clients - {0}", connectionCollection.GetPeers <IClientPeer>().Count); return(true); }
public void TearDown() { // disconnect all clients var clients = _clientCollection.GetPeers <IClientPeer>(); Log.DebugFormat("Disconnecting {0} peers", clients.Count); foreach (var client in clients) { client.Disconnect(); } _clientCollection.Clear(); // disconnect from all servers var servers = _serverCollection.GetPeers <IServerPeer>(); Log.DebugFormat("Disconnecting {0} servers", servers.Count); foreach (var server in servers) { server.Disconnect(); } _serverCollection.Clear(); // stop all background threads foreach (var backgroundThread in _backgroundThreads) { backgroundThread.Stop(); } }
public override bool OnHandleMessage(IMessage message, IServerPeer serverPeer) { Log.DebugFormat("Forwarding Response {0}-{1}", message.Code, message.SubCode); if (message.Parameters.ContainsKey(_serverConfiguration.PeerIdCode)) { Log.DebugFormat("Looking for Peer Id {0}", new Guid((Byte[])message.Parameters[_serverConfiguration.PeerIdCode])); PhotonClientPeer peer = _connectionCollection.GetPeers <PhotonClientPeer>().FirstOrDefault(p => p.PeerId == new Guid((Byte[])message.Parameters[_serverConfiguration.PeerIdCode])); if (peer != null) { Log.DebugFormat("Found Peer"); message.Parameters.Remove(_serverConfiguration.PeerIdCode); _clientCodeRemover.RemoveCodes(message); var response = message as Response; if (response != null) { peer.SendOperationResponse( new OperationResponse(response.Code, response.Parameters) { DebugMessage = response.DebugMessage, ReturnCode = response.ReturnCode }, new SendParameters()); } else { peer.SendOperationResponse(new OperationResponse(message.Code, message.Parameters), new SendParameters()); } } } return(true); }
public bool HandleMessage(IMessage message, IServerPeer peer) { // Got a response back from the LoginUserPass - might be successful, might not. if (message.Parameters.ContainsKey(_serverConfiguration.PeerIdCode)) { Log.DebugFormat("Looking for Peer Id {0}", new Guid((Byte[])message.Parameters[_serverConfiguration.PeerIdCode])); IClientPeer clientPeer = _connectionCollection.GetPeers <IClientPeer>().FirstOrDefault(p => p.PeerId == new Guid((Byte[])message.Parameters[_serverConfiguration.PeerIdCode])); if (clientPeer != null) { Log.DebugFormat("Found Peer"); var response = message as Response; // copy our response to a return response Response returnResponse = new Response(Code, SubCode, message.Parameters); // remove any unnecessary codes from the returning packet _clientCodeRemover.RemoveCodes(returnResponse); if (response.ReturnCode == (short)ReturnCode.OK) { // Good response, get the client data and look for the userId to set it for the future. clientPeer.ClientData <CharacterData>().UserId = (int)response.Parameters[(byte)MessageParameterCode.UserId]; } // make one call to send the message back - One "exit point" for the message. clientPeer.SendMessage(response); } } return(true); }
public void Run(object threadContext) { Stopwatch timer = new Stopwatch(); timer.Start(); isRunning = true; while (isRunning) { try { // check to see if there are any players - we need a list of the players. if we have no players sleep for 1 second and try again. if (ConnectionCollection.GetPeers <IClientPeer>().Count <= 0) { Thread.Sleep(1000); timer.Restart(); continue; } if (timer.Elapsed < TimeSpan.FromMilliseconds(5000)) // run every 5000ms - 5 seconds { if (5000 - timer.ElapsedMilliseconds > 0) { Thread.Sleep(5000 - (int)timer.ElapsedMilliseconds); } continue; } Update(timer.Elapsed); // Restart the timer so that, just in case it takes longer than 100ms itll start over as soon as the process finishes. timer.Restart(); } catch (Exception e) { Log.ErrorFormat(string.Format("Exception occurid in the TestBackgroundThread.Run - {0}", e.StackTrace)); throw; } } }
public void Run(object threadContext) { Stopwatch timer = new Stopwatch(); timer.Start(); isRunning = true; // set up all colliders from xml file in BEPU Physics // set up BEPU Physics while (isRunning) { try { if (ConnectionCollection.GetPeers <IClientPeer>().Count <= 0) { Thread.Sleep(5000); timer.Restart(); continue; } if (timer.Elapsed < TimeSpan.FromMilliseconds(500)) { if (500 - timer.ElapsedMilliseconds > 0) { Thread.Sleep(500 - (int)timer.ElapsedMilliseconds); } continue; } Update(timer.Elapsed); timer.Restart(); } catch (Exception ex) { Log.ErrorFormat("[PhysicsBackgroundThread(Run)]: Exception - {0}", ex.StackTrace); } } }
public override bool OnHandleMessage(IMessage message, IServerPeer serverPeer) { Log.DebugFormat("Forwarding Event {0}-{1}", message.Code, message.SubCode); if (message.Parameters.ContainsKey(_serverConfiguration.PeerIdCode)) { Log.DebugFormat("Looking for Peer Id {0}", new Guid((Byte[])message.Parameters[_serverConfiguration.PeerIdCode])); PhotonClientPeer peer = _connectionCollection.GetPeers <PhotonClientPeer>().FirstOrDefault(p => p.PeerId == new Guid((Byte[])message.Parameters[_serverConfiguration.PeerIdCode])); if (peer != null) { message.Parameters.Remove(_serverConfiguration.PeerIdCode); _clientCodeRemover.RemoveCodes(message); peer.SendEvent(new EventData(message.Code, message.Parameters), new SendParameters()); } } return(true); }
public bool HandleMessage(IMessage message, IServerPeer peer) { var serverPeer = peer as PhotonServerPeer; var operation = new LoginOperation(serverPeer.protocol, message); if (!operation.IsValid) { Response response = new Response(Code, SubCode, new Dictionary <byte, object>() { { (byte)MessageParameterCode.SubCodeParameterCode, SubCode }, { (byte)MessageParameterCode.PeerIdParameterCode, message.Parameters[(byte)MessageParameterCode.PeerIdParameterCode] }, }, operation.GetErrorMessage(), (int)ReturnCode.OperationInvalid); peer.SendMessage(response); return(true); } if (operation.Login.Length < 6 || operation.Password.Length < 6) { peer.SendMessage(new Response(Code, SubCode, new Dictionary <byte, object>() { { (byte)MessageParameterCode.SubCodeParameterCode, SubCode }, { (byte)MessageParameterCode.PeerIdParameterCode, message.Parameters[(byte)MessageParameterCode.PeerIdParameterCode] }, }, "Login and password can't be less than 6 symbols.", (int)ReturnCode.OperationInvalid)); return(true); } try { using (var session = NHibernateHelper.OpenSession()) { using (var transaction = session.BeginTransaction()) { var account = session.QueryOver <AccountModel>().Where(a => a.Login == operation.Login).SingleOrDefault(); if (account == null) { transaction.Commit(); peer.SendMessage(new Response(Code, SubCode, new Dictionary <byte, object>() { { (byte)MessageParameterCode.SubCodeParameterCode, SubCode }, { (byte)MessageParameterCode.PeerIdParameterCode, message.Parameters[(byte)MessageParameterCode.PeerIdParameterCode] }, }, "Login or password incorrect.", (int)ReturnCode.LoginOrPasswordIncorrect)); return(true); } string Password = BitConverter.ToString(SHA512.Create().ComputeHash( Encoding.UTF8.GetBytes(account.Salt + operation.Password))).Replace("-", ""); if (Password != account.Password) { transaction.Commit(); peer.SendMessage(new Response(Code, SubCode, new Dictionary <byte, object>() { { (byte)MessageParameterCode.SubCodeParameterCode, SubCode }, { (byte)MessageParameterCode.PeerIdParameterCode, message.Parameters[(byte)MessageParameterCode.PeerIdParameterCode] }, }, "Login or password incorrect.", (int)ReturnCode.LoginOrPasswordIncorrect)); return(true); } var clientPeer = peerFactory.CreatePeer <IClientPeer>(new PeerConfig()); clientPeer.PeerId = new Guid((byte[])message.Parameters[(byte)MessageParameterCode.PeerIdParameterCode]); connectionCollection.Connect(clientPeer); //clientPeer.ClientData<CharacterData>().UserId = account.Id; log.DebugFormat("clients - {0}", connectionCollection.GetPeers <IClientPeer>().Count); transaction.Commit(); peer.SendMessage(new Response(Code, SubCode, new Dictionary <byte, object>() { { (byte)MessageParameterCode.SubCodeParameterCode, SubCode }, { (byte)MessageParameterCode.PeerIdParameterCode, message.Parameters[(byte)MessageParameterCode.PeerIdParameterCode] }, { (byte)MessageParameterCode.UserId, account.Id } }, "", (int)ReturnCode.OK)); return(true); } } } catch (Exception ex) { log.ErrorFormat("Error login handler: {0}", ex); peer.SendMessage(new Response(Code, SubCode, new Dictionary <byte, object>() { { (byte)MessageParameterCode.SubCodeParameterCode, SubCode }, { (byte)MessageParameterCode.PeerIdParameterCode, message.Parameters[(byte)MessageParameterCode.PeerIdParameterCode] }, }, ex.ToString(), (int)ReturnCode.OperationDenied)); return(true); } }
public List <T> GetSubServerOfType <T>() where T : class, ISubServer { return(subServerCollection.GetPeers <T>().Where(s => s.GetType() == typeof(T)).ToList()); }