public void PacketError(ClientWrapper client, Exception exception) { if (PacketErrors.ContainsKey(client.ClientIdentifier)) { int errors = PacketErrors[client.ClientIdentifier]; PacketErrors[client.ClientIdentifier] = errors + 1; if (ServerSettings.DisplayPacketErrors) { ConsoleFunctions.WriteWarningLine("Packet error for player: \"" + client.Player.Username + "\" Packet errors: " + PacketErrors[client.ClientIdentifier] + "\nError:\n" + exception.Message); } if (PacketErrors[client.ClientIdentifier] >= 3) { //RemoveClient(client); if (ServerSettings.ReportExceptionsToClient) { new Disconnect(client) {Reason = new McChatMessage("You were kicked from the server!\n" + exception.Message)}.Write(); } else { new Disconnect(client) { Reason = new McChatMessage("You were kicked from the server!") }.Write(); } client.Kicked = true; } } else { //Something really wrong } }
protected Package(ClientWrapper client, DataBuffer buffer) { if (client == null) return; if (!client.TcpClient.Connected) return; Client = client; Stream = client.TcpClient.GetStream(); Buffer = buffer; }
public void CleanErrors(ClientWrapper client) { if (PacketErrors.ContainsKey(client.ClientIdentifier)) { PacketErrors[client.ClientIdentifier] = 0; } else { //Something really wrong } }
public PackageFactory(ClientWrapper client, DataBuffer buffer) { #region Ping PingPackages.Add(new Handshake(client, buffer)); #endregion #region Login LoginPackages.Add(new EncryptionResponse(client, buffer)); LoginPackages.Add(new LoginStart(client, buffer)); #endregion #region Status StatusPackages.Add(new Request(client, buffer)); StatusPackages.Add(new Ping(client, buffer)); #endregion #region Play PlayPackages.Add(new ChatMessage(client, buffer)); PlayPackages.Add(new Animation(client, buffer)); PlayPackages.Add(new PlayerBlockPlacement(client, buffer)); PlayPackages.Add(new HeldItemChange(client, buffer)); PlayPackages.Add(new EntityAction(client, buffer)); PlayPackages.Add(new PlayerAbilities(client, buffer)); PlayPackages.Add(new PluginMessage(client, buffer)); PlayPackages.Add(new KeepAlive(client, buffer)); PlayPackages.Add(new PlayerPositionAndLook(client, buffer)); PlayPackages.Add(new PlayerPosition(client, buffer)); PlayPackages.Add(new PlayerLook(client, buffer)); PlayPackages.Add(new OnGround(client, buffer)); PlayPackages.Add(new ClientSettings(client, buffer)); PlayPackages.Add(new PlayerDigging(client, buffer)); PlayPackages.Add(new ClientStatus(client, buffer)); PlayPackages.Add(new ClickWindow(client, buffer)); PlayPackages.Add(new UseEntity(client, buffer)); PlayPackages.Add(new CloseWindow(client, buffer)); PlayPackages.Add(new UseItem(client, buffer)); PlayPackages.Add(new CreativeInventoryAction(client, buffer)); PlayPackages.Add(new UpdateSign(client, buffer)); #endregion _client = client; _buffer = buffer; }
public bool HandleMessage(ClientWrapper client, DataBuffer buffer) { string raw = buffer.ReadString(); string channel = raw.Split('|')[0]; string command = raw.Split('|')[1]; foreach (var msg in Messages) { if (msg.Key == channel) { if (msg.Value.Command == command) { msg.Value.HandleData(client, buffer); } return true; } } return false; }
internal void RemoveClient(ClientWrapper client) { if (Clients.ContainsKey(client.ClientIdentifier)) { Clients.Remove(client.ClientIdentifier); PacketErrors.Remove(client.ClientIdentifier); GC.Collect(); } }
internal void AddClient(ref ClientWrapper client) { if (client.ClientIdentifier == -1) { CurrentIdentifier++; client.ClientIdentifier = CurrentIdentifier; Clients.Add(CurrentIdentifier, client); PacketErrors.Add(CurrentIdentifier, 0); ClientPing.Add(CurrentIdentifier, UnixTimeNow()); } }
public void ReportPing(ClientWrapper client) { if (ClientPing.ContainsKey(client.ClientIdentifier)) { ClientPing[client.ClientIdentifier] = UnixTimeNow(); } }
public DataBuffer(ClientWrapper client) { _client = client; }
internal void BroadcastPlayerRemoval(ClientWrapper caller) { new PlayerListItem(caller) { Action = 4, Uuid = caller.Player.Uuid }.Broadcast(this); }
private void HandleClientCommNew(object client) { var tcpClient = (TcpClient) client; var clientStream = tcpClient.GetStream(); var Client = new ClientWrapper(tcpClient); Globals.ClientManager.AddClient(ref Client); while (true) { try { while (!clientStream.DataAvailable) { if (Client.Kicked) { break; } Thread.Sleep(5); } if (Client.Kicked) { break; } if (ServerSettings.UseCompression && Client.PacketMode == PacketMode.Play) { int packetLength = ReadVarInt(clientStream); int dataLength = ReadVarInt(clientStream); int actualDataLength = packetLength - GetVarIntBytes(dataLength).Length; if (dataLength == 0) { if (!ReadCompressed(Client, clientStream, actualDataLength)) break; } else { if (!ReadUncompressed(Client, clientStream, dataLength)) break; } } else { int dlength = ReadVarInt(clientStream); if (!ReadUncompressed(Client, clientStream, dlength)) break; } } catch (Exception ex) { //Exception, disconnect! ConsoleFunctions.WriteDebugLine("Error: \n" + ex); if (ServerSettings.ReportExceptionsToClient) { new Disconnect(Client) { Reason = new McChatMessage("§fServer threw an exception!\n\nFor the nerdy people: \n" + ex.Message) }.Write(); } else { new Disconnect(Client) { Reason = new McChatMessage("§fYou were kicked because of an unknown problem!") }.Write(); } break; } } if (Client.Kicked) { new Disconnect(Client) { Reason = new McChatMessage("§fYou were kicked because of a network problem!") }.Write(); } //Close the connection with the client. :) Client.ThreadPool.KillAllThreads(); //Client.StopKeepAliveTimer(); if (Client.Player != null) { Client.Player.SavePlayer(); Client.Player.Level.RemovePlayer(Client.Player.EntityId); Client.Player.Level.BroadcastPlayerRemoval(Client); } Client.TcpClient.Close(); Globals.ClientManager.RemoveClient(Client); Thread.CurrentThread.Abort(); }
private bool ReadUncompressed(ClientWrapper client, NetworkStream clientStream, int dlength) { var buffie = new byte[dlength]; int receivedData; receivedData = clientStream.Read(buffie, 0, buffie.Length); if (receivedData > 0) { var buf = new DataBuffer(client); if (client.Decrypter != null) { var date = new byte[4096]; client.Decrypter.TransformBlock(buffie, 0, buffie.Length, date, 0); buf.BufferedData = date; } else { buf.BufferedData = buffie; } buf.BufferedData = buffie; buf.Size = dlength; var packid = buf.ReadVarInt(); if (!new PackageFactory(client, buf).Handle(packid)) { ConsoleFunctions.WriteWarningLine("Unknown packet received! \"0x" + packid.ToString("X2") + "\""); } buf.Dispose(); return true; } else { return false; } }
public virtual void HandleData(ClientWrapper client, DataBuffer buffer) { }
internal void SetTarget(ClientWrapper client) { Client = client; Stream = client.TcpClient.GetStream(); Buffer = new DataBuffer(client); }
public override void HandleData(ClientWrapper client, DataBuffer buffer) { string c = buffer.ReadString(); ConsoleFunctions.WriteInfoLine(client.Player.Username + "'s client: " + c); }
public LocalDataBuffer(ClientWrapper client) : base(client) { }
internal void IntroduceNewPlayer(ClientWrapper caller) { foreach (var i in OnlinePlayers.Values) { if (i.Wrapper != caller) { new PlayerListItem(caller) { Action = 0, Gamemode = i.Gamemode, Username = i.Username, Uuid = i.Uuid }.Write(); //Send TAB Item new SpawnPlayer(caller) {Player = i}.Write(); //Spawn the old player to new player new SpawnPlayer(i.Wrapper) {Player = caller.Player}.Write(); //Spawn the new player to old player i.BroadcastEquipment(); } } }