void ProcessPacket(ClientProxy inClientProxy, NetIncomingMessage inInputStream) { //remember we got a packet so we know not to disconnect for a bit inClientProxy.UpdateLastPacketTime(); uint32_t packetType; packetType = inInputStream.ReadUInt32(); switch ((PacketType)packetType) { case PacketType.kHelloCC: //need to resend welcome. to be extra safe we should check the name is the one we expect from this address, //otherwise something weird is going on... SendWelcomePacket(inClientProxy); break; case PacketType.kInputCC: if (inClientProxy.GetDeliveryNotificationManager().ReadAndProcessState(inInputStream)) { HandleInputPacket(inClientProxy, inInputStream); } break; case PacketType.kRPC: HandleRPCPacket(inClientProxy, inInputStream); break; default: //LOG("Unknown packet type received from %s", inClientProxy.GetSocketAddress().ToString().c_str()); break; } }
public bool Possess(int player_id) { mClient = NetworkManagerServer.sInstance.GetClientProxy(player_id); if (mClient == null) { return(false); } return(true); }
public void HandleNewClient(ClientProxy inClientProxy) { int playerId = inClientProxy.GetPlayerId(); byte worldId = inClientProxy.GetWorldId(); GameMode.sInstance.AddEntry((uint32_t)playerId, inClientProxy.GetName()); SpawnActorForPlayer(playerId, worldId); ServerMonitor.sInstance.AddUser(worldId); }
public override void HandleConnectionReset(System.Net.IPEndPoint inFromAddress) { //just dc the client right away... ClientProxy c = null; if (mAddressToClientMap.TryGetValue(inFromAddress, out c) == true) { HandleClientDisconnected(c); } }
void SendWelcomePacket(ClientProxy inClientProxy) { var welcomePacket = GetServer().CreateMessage(); welcomePacket.Write((uint32_t)PacketType.kWelcomeCC); welcomePacket.Write(inClientProxy.GetPlayerId()); Log.Information(string.Format("Server Welcoming, new client {0} as player {1}", inClientProxy.GetName(), inClientProxy.GetPlayerId())); GetServer().SendMessage(welcomePacket, inClientProxy.mConnection, NetDeliveryMethod.Unreliable); }
public ClientProxy GetClientProxy(int inPlayerId) { ClientProxy c = null; if (mPlayerIdToClientMap.TryGetValue(inPlayerId, out c) == true) { return(c); } return(null); }
void WriteLastMoveTimestampIfDirty(NetOutgoingMessage inOutputStream, ClientProxy inClientProxy) { //first, dirty? bool isTimestampDirty = inClientProxy.IsLastMoveTimestampDirty(); inOutputStream.Write(isTimestampDirty); if (isTimestampDirty) { inOutputStream.Write(inClientProxy.GetUnprocessedMoveList().GetLastMoveTimestamp()); inClientProxy.SetIsLastMoveTimestampDirty(false); } }
void HandleRPCPacket(ClientProxy inClientProxy, NetIncomingMessage inInputStream) { int networkId = inInputStream.ReadInt32(); ulong hash = inInputStream.ReadUInt64(); int senderClientId = inClientProxy.GetPlayerId(); NetGameObject obj; if (mNetworkIdToGameObjectMap.TryGetValue(networkId, out obj) == true) { obj.OnRemoteServerRPC(hash, senderClientId, inInputStream); } }
void HandleClientDisconnected(ClientProxy inClientProxy) { mPlayerIdToClientMap.Remove(inClientProxy.GetPlayerId()); mAddressToClientMap.Remove(inClientProxy.GetSocketAddress()); ((Server)(Engine.sInstance)).HandleLostClient(inClientProxy); Log.Information(string.Format("HandleClientDisconnected client {0} as player {1}, addr_map{2}, id_map{3}", inClientProxy.GetName(), inClientProxy.GetPlayerId(), mAddressToClientMap.Count, mPlayerIdToClientMap.Count)); //was that the last client? if so, bye! if (mAddressToClientMap.Count == 0) { //Engine.sInstance.SetShouldKeepRunning(false); } }
public override void ProcessPacket(NetIncomingMessage inInputStream, System.Net.IPEndPoint inFromAddress) { //try to get the client proxy for this address //pass this to the client proxy to process ClientProxy c = null; if (mAddressToClientMap.TryGetValue(inFromAddress, out c) == false) { //didn't find one? it's a new cilent..is the a HELO? if so, create a client proxy... HandlePacketFromNewClient(inInputStream, inFromAddress); } else { ProcessPacket(c, inInputStream); } }
void HandleInputPacket(ClientProxy inClientProxy, NetIncomingMessage inInputStream) { Move move = new Move(); uint32_t moveCount = inInputStream.ReadUInt32(2); for (; moveCount > 0; --moveCount) { if (move.Read(inInputStream)) { //log.InfoFormat("recv move {0}, {1}, {2}, {3}", move.GetDeltaTime(), move.GetInputState(), move.GetTimestamp(), moveCount); if (inClientProxy.GetUnprocessedMoveList().AddMoveIfNew(move)) { inClientProxy.SetIsLastMoveTimestampDirty(true); } } } }
void HandlePacketFromNewClient(NetIncomingMessage inInputStream, System.Net.IPEndPoint inFromAddress) { //read the beginning- is it a hello? uint32_t packetType = inInputStream.ReadUInt32(); if (packetType == (uint32_t)PacketType.kHelloCC) { //read the name string name = inInputStream.ReadString(); byte worldId = inInputStream.ReadByte(); ClientProxy newClientProxy = new ClientProxy(inFromAddress, name, mNewPlayerId++, worldId); newClientProxy.mConnection = inInputStream.SenderConnection; mAddressToClientMap[inFromAddress] = newClientProxy; mPlayerIdToClientMap[newClientProxy.GetPlayerId()] = newClientProxy; Log.Information(string.Format("HandlePacketFromNewClient new client {0} as player {1}, addr_map{2}, id_map{3}, world_id{4}", newClientProxy.GetName(), newClientProxy.GetPlayerId(), mAddressToClientMap.Count, mPlayerIdToClientMap.Count, newClientProxy.GetWorldId() )); //tell the server about this client, spawn a cat, etc... //if we had a generic message system, this would be a good use for it... //instead we'll just tell the server directly ((Server)Engine.sInstance).HandleNewClient(newClientProxy); //and welcome the client... SendWelcomePacket(newClientProxy); //and now init the replication manager with everything we know about! foreach (var pair in mNetworkIdToGameObjectMap) { if (pair.Value.WorldId == newClientProxy.GetWorldId()) { newClientProxy.GetReplicationManagerServer().ReplicateCreate(pair.Key, pair.Value.GetAllStateMask()); } } } else { //bad incoming packet from unknown client- we're under attack!! //LOG("Bad incoming packet from unknown client at socket %s", inFromAddress); } }
public void TakeDamage(int inDamagingPlayerId, int damage = 1) { mHealth -= damage; if (mHealth <= 0.0f) { //score one for damaging player... GameMode.sInstance.IncScore((uint)inDamagingPlayerId, 1); //and you want to die SetDoesWantToDie(true); //tell the client proxy to make you a new cat ClientProxy clientProxy = NetworkManagerServer.sInstance.GetClientProxy((int)GetPlayerId()); if (clientProxy != null) { clientProxy.HandleActorDied(); } } //tell the world our health dropped NetworkManagerServer.sInstance.SetStateDirty(GetNetworkId(), WorldId, (uint)EActorReplicationState.ECRS_Health); }
public void HandleLostClient(ClientProxy inClientProxy) { //kill client's actor //remove client from scoreboard int playerId = inClientProxy.GetPlayerId(); GameMode.sInstance.RemoveEntry((uint32_t)playerId); SActor actor = GetActorForPlayer(playerId, inClientProxy.GetWorldId()); if (actor != null) { actor.Unpossess(); actor.SetDoesWantToDie(true); } if (ServerMonitor.sInstance.DelUser(inClientProxy.GetWorldId()) == 0) { Log.Information(string.Format("Close World {0}", inClientProxy.GetWorldId())); NetworkManagerServer.sInstance.Clear(inClientProxy.GetWorldId()); World.Instance(inClientProxy.GetWorldId()).Clear(); } }
void SendStatePacketToClient(ClientProxy inClientProxy) { //build state packet var statePacket = GetServer().CreateMessage(); //it's state! statePacket.Write((uint32_t)PacketType.kStateCC); InFlightPacket ifp = inClientProxy.GetDeliveryNotificationManager().WriteState(statePacket); WriteLastMoveTimestampIfDirty(statePacket, inClientProxy); AddScoreBoardStateToPacket(statePacket); var rmtd = new ReplicationManagerTransmissionData(inClientProxy.GetReplicationManagerServer()); inClientProxy.GetReplicationManagerServer().Write(statePacket, rmtd); ifp.SetTransmissionData((int)TransmissionDataType.kReplicationManager, rmtd); var ret = GetServer().SendMessage(statePacket, inClientProxy.mConnection, NetDeliveryMethod.Unreliable); //log.InfoFormat("send {0}", ret); }
public void Unpossess() { mClient = null; }