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 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); }
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); }
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 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(); } }