public void AddPlayer(NetConnection connection, PlayerInformation playerInfo) { String denyReason; Byte slot = AllocateSlot(playerInfo, out denyReason); // could not allocate the player if AllocateSlot returns DummySlot if (slot == ProtocolInformation.DummySlot) { Log.InfoFormat("Player \"{0}\" from {1} tried to join, but was rejected ({2}).", playerInfo.Callsign, connection, denyReason); connection.Deny(denyReason); return; } // we can now approve the player if we get here connection.Approve(); // add player to our list players[slot] = new Player(this, slot, connection, playerInfo); // and tell everyone else about this awesome new player Log.DebugFormat("Sending MsgAddPlayer to everyone else about player #{0}", slot); NetOutgoingMessage packet = Server.CreateMessage(); MsgAddPlayerPacket message = new MsgAddPlayerPacket(players[slot].PlayerInfo, false); packet.Write((Byte)message.MsgType); message.Write(packet); Log.DebugFormat("MsgAddPlayer Compiled ({0} bytes) and being sent to {1} recipients", packet.LengthBytes, players.Count - 1); // send to everyone except our new player, we let Player itself decide when to send the state to the new guy Server.SendToAll(packet, connection, NetDeliveryMethod.ReliableOrdered, 0); }
public void Start() { NetIncomingMessage packetin; while (true) { while ((packetin = _netserver.ReadMessage()) != null) { _msgSender = packetin.SenderConnection; switch (packetin.MessageType) { case NetIncomingMessageType.StatusChanged: { NetConnectionStatus status = (NetConnectionStatus)packetin.ReadByte(); //Player doesnt want to play anymore if (status == NetConnectionStatus.Disconnected || status == NetConnectionStatus.Disconnecting) { _mineserver.Console.ConsoleWrite(_mineserver.PlayerManager.GetPlayerByConnection(_msgSender).Name + " Disconnected"); _mineserver.ServerSender.SendPlayerLeft(_mineserver.PlayerManager.GetPlayerByConnection(_msgSender)); _mineserver.PlayerManager.RemovePlayer(_mineserver.PlayerManager.GetPlayerByConnection(_msgSender)); } break; } case NetIncomingMessageType.DiscoveryRequest: { _mineserver.ServerSender.SendDiscoverResponse(packetin.SenderEndpoint); break; } case NetIncomingMessageType.ConnectionApproval: { //If the server is full then deny if (_netserver.ConnectionsCount == _netserver.Configuration.MaximumConnections) { _msgSender.Deny("serverfull"); break; } int version = packetin.ReadInt32(); string name = packetin.ReadString(); //If the version is any other then the server deny if(_mineserver.VersionMatch(version) == false) { _msgSender.Deny("versionmismatch"); break; } ServerPlayer dummy = new ServerPlayer(_msgSender); if (_mineserver.PlayerManager.NameExists(name)) { name += dummy.Id; } dummy.Name = name; _mineserver.PlayerManager.AddPlayer(dummy); _msgSender.Approve(); Thread.Sleep(4000); _mineserver.GameWorld.GenerateSpawnPosition(_mineserver.PlayerManager.GetPlayerByConnection(_msgSender)); _mineserver.ServerSender.SendCurrentWorld(_mineserver.PlayerManager.GetPlayerByConnection(_msgSender)); _mineserver.ServerSender.SendInitialUpdate(_mineserver.PlayerManager.GetPlayerByConnection(_msgSender)); _mineserver.ServerSender.SendPlayerJoined(_mineserver.PlayerManager.GetPlayerByConnection(_msgSender)); _mineserver.ServerSender.SendOtherPlayersInWorld(_mineserver.PlayerManager.GetPlayerByConnection(_msgSender)); _mineserver.Console.ConsoleWrite(name + " Connected"); break; } case NetIncomingMessageType.Data: { PacketType dataType = (PacketType)packetin.ReadByte(); switch (dataType) { case PacketType.PlayerMovementUpdate: { _mineserver.PlayerManager.GetPlayerByConnection(_msgSender).Position = packetin.ReadVector3(); _mineserver.ServerSender.SendMovementUpdate(_mineserver.PlayerManager.GetPlayerByConnection(_msgSender)); break; } case PacketType.PlayerBlockSet: { Vector3 pos = packetin.ReadVector3(); _mineserver.MapManager.WorldMap[(int)pos.X, (int)pos.Y, (int)pos.Z] = (BlockTypes)packetin.ReadByte(); break; } } break; } } } } }
private void ConnectionRequest(NetConnection connection, NetIncomingMessage message) { string playerName = null; try { playerName = message.ReadString(); } catch { } if (playerName == null) connection.Deny("invalid message"); else { NetworkPlayer player = new NetworkPlayer(playerName, connection); PendingPlayers.Enqueue(player); connection.Tag = player; } }