/// <summary>
 /// Initializes a new instance of the <see cref="PlayerConnectionEventArgs"/> class.
 /// </summary>
 /// <param name="ConnectionState">State of the connection.</param>
 /// <param name="Client">The client.</param>
 /// <remarks></remarks>
 public PlayerConnectionEventArgs(ConnectionState ConnectionState, RemoteClient Client)
 {
     this.ConnectionState = ConnectionState;
     this.Client = Client;
 }
        /// <summary>
        /// Handles the packet.
        /// </summary>
        /// <param name="Server">The server.</param>
        /// <param name="Client">The client.</param>
        /// <remarks></remarks>
        public override void HandlePacket(ClassicServer Server, RemoteClient Client)
        {
            Client.LoggedIn = true;
            Client.UserName = UserNameOrServerName;
            Client.World = Server.MainLevel;
            Client.PacketQueue.Enqueue(new IdentificationPacket(Server.Server.Name, Server.Server.MotD, Client.UserType));
            Client.PlayerID = (byte)(Server.Clients.Count() - 1);
            Client.PacketQueue.Enqueue(new WorldInitializePacket());

            MemoryStream ms = new MemoryStream();
            GZipStream s = new GZipStream(ms, CompressionMode.Compress, true);
            s.Write(BitConverter.GetBytes(IPAddress.HostToNetworkOrder(World.Find(Client.World).Data.Length)), 0, sizeof(int));
            s.Write(World.Find(Client.World).Data, 0, World.Find(Client.World).Data.Length);
            s.Close();
            byte[] data = ms.GetBuffer();
            ms.Close();

            double numChunks = data.Length / 1024;
            double chunksSent = 0;
            for (int i = 0; i < data.Length; i += 1024)
            {
                byte[] chunkData = new byte[1024];

                short chunkLength = 1024;
                if (data.Length - i < chunkLength)
                    chunkLength = (short)(data.Length - i);

                Array.Copy(data, i, chunkData, 0, chunkLength);

                Client.PacketQueue.Enqueue(new WorldDataChunkPacket(chunkLength, chunkData, (byte)((chunksSent / numChunks) * 100)));
                chunksSent++;
            }

            Client.PacketQueue.Enqueue(new WorldFinalizePacket(World.Find(Client.World).Width, World.Find(Client.World).Height, World.Find(Client.World).Depth));
            Client.Position = World.Find(Client.World).Spawn.Clone();
            Client.Yaw = 0; Client.Pitch = 0;
            unchecked { Client.PacketQueue.Enqueue(new PositionAndOrientationPacket((byte)-1, Client.Position, Client.Yaw, Client.Pitch)); }
            Server.LoadAllClientsToClient(Client);
            Server.EnqueueToAllClientsInWorld(new SpawnPlayerPacket(Client.PlayerID, Client.UserName, Client.Position, Client.Yaw, Client.Pitch), Client.World, Client);
        }
Example #3
0
 /// <summary>
 /// Writes a packet to a RemoteClient.
 /// </summary>
 /// <param name="Client">The RemoteClient to be written to.</param>
 public abstract void WritePacket(RemoteClient Client);
 /// <summary>
 /// Reads the packet.
 /// </summary>
 /// <param name="Client">The client.</param>
 /// <remarks></remarks>
 public override void ReadPacket(RemoteClient Client)
 {
     this.PlayerID = (byte)Client.TcpClient.GetStream().ReadByte();
     this.Position.X = ReadShort(Client.TcpClient.GetStream());
     this.Position.Y = ReadShort(Client.TcpClient.GetStream());
     this.Position.Z = ReadShort(Client.TcpClient.GetStream());
     this.Yaw = (byte)Client.TcpClient.GetStream().ReadByte();
     this.Pitch = (byte)Client.TcpClient.GetStream().ReadByte();
 }
        /// <summary>
        /// Does the work.
        /// </summary>
        /// <param name="o">The object.</param>
        /// <remarks></remarks>
        protected void DoWork(object o)
        {
            if (listener.Pending())
            {
                RemoteClient c = new RemoteClient();
                c.TcpClient = listener.AcceptTcpClient();
                c.EndPoint  = c.TcpClient.Client.RemoteEndPoint;
                Clients.Add(c);
            }

            int totalQueuedPackets = 0;

            for (int i = 0; i < Clients.Count; i++)
            {
                totalQueuedPackets += Clients[i].PacketQueue.Count;
            }
            // Any leftover time from each client is given to any client that could use it
            double additionalTime = 0;

            for (int i = 0; i < Clients.Count; i++)
            {
                if (!Clients[i].TcpClient.Connected)
                {
                    if (OnPlayerConnectionChanged != null && Clients[i].LoggedIn)
                    {
                        OnPlayerConnectionChanged(this, new PlayerConnectionEventArgs(ConnectionState.Disconnected, Clients[i]));
                    }
                    EnqueueToAllClientsInWorld(new DespawnPlayerPacket((byte)i), Clients[i].World);
                }
                else
                {
                    try
                    {
                        // Read in a packet
                        if (Clients[i].TcpClient.Available != 0)
                        {
                            Packet p = Packet.GetPacket((PacketID)Clients[i].TcpClient.GetStream().ReadByte(), true);
                            if (PrePacketEvent != null)
                            {
                                PrePacketEvent(this, new PrePacketEventArgs(p, Clients[i]));
                            }
                            if (cancelprepacket)
                            {
                                cancelprepacket = false;
                                goto here;
                            }
                            p.ReadPacket(Clients[i]);
                            p.HandlePacket(this, Clients[i]);
                            if (p.PacketID == PacketID.Identification && OnPlayerConnectionChanged != null)
                            {
                                OnPlayerConnectionChanged(this, new PlayerConnectionEventArgs(ConnectionState.Connected, Clients[i]));
                            }
                        }
here:
                        // Write out from the packet queue
                        if (Clients[i].PacketQueue.Count == 0)
                        {
                            continue;
                        }
                        // For the sake of server speed, each client is limited in the amount
                        // of time per tick they are allocated for sending of packets.
                        DateTime startTime         = DateTime.Now;
                        double   maxClientSendTime = double.PositiveInfinity;

                        if (!(totalQueuedPackets == 0 || Clients[i].PacketQueue.Count / totalQueuedPackets == 0))
                        {
                            maxClientSendTime = (1 / 20) * (Clients[i].PacketQueue.Count / totalQueuedPackets) + additionalTime;
                        }

                        additionalTime = 0;
                        while (Clients[i].PacketQueue.Count != 0 && (DateTime.Now - startTime).TotalMilliseconds <= maxClientSendTime)
                        {
                            // Send out packets from the queue
                            PacketID id = Clients[i].PacketQueue.Peek().PacketID;
                            Clients[i].PacketQueue.Dequeue().WritePacket(Clients[i]);
                            if (id == PacketID.DisconnectPlayer)
                            {
                                if (Clients[i].TcpClient.Connected)
                                {
                                    Clients[i].TcpClient.Close();
                                }
                                if (OnPlayerConnectionChanged != null)
                                {
                                    OnPlayerConnectionChanged(this, new PlayerConnectionEventArgs(ConnectionState.Disconnected, Clients[i]));
                                }
                                EnqueueToAllClientsInWorld(new DespawnPlayerPacket((byte)i), Clients[i].World);
                                Clients.RemoveAt(i);
                                i--;
                                break;
                            }
                        }
                        if ((DateTime.Now - startTime).TotalMilliseconds <= maxClientSendTime)
                        {
                            additionalTime += maxClientSendTime - (DateTime.Now - startTime).TotalMilliseconds;
                        }
                    }
                    catch
                    {
                        if (OnPlayerConnectionChanged != null && Clients[i].LoggedIn)
                        {
                            OnPlayerConnectionChanged(this, new PlayerConnectionEventArgs(ConnectionState.Disconnected, Clients[i]));
                        }
                        EnqueueToAllClients(new DespawnPlayerPacket((byte)i), Clients[i]); //TODO: Find out why this exceptions.
                        Clients.RemoveAt(i);
                        i--;
                    }
                }
            }

            netWorker = new Timer(new TimerCallback(DoWork), null, 0, Timeout.Infinite);
        }
 /// <summary>
 /// Enqueues to all clients in a certain world.
 /// </summary>
 /// <param name="Packet">The packet.</param>
 /// <param name="World">The world.</param>
 /// <param name="IgnoreClient">If specified, the client won't recieve the packet.</param>
 /// <remarks></remarks>
 public void EnqueueToAllClientsInWorld(Packet Packet, string World, RemoteClient IgnoreClient = null)
 {
     foreach (RemoteClient Client in Clients)
         if (IgnoreClient != Client && Client.World.ToLower() == World.ToLower())
             Client.PacketQueue.Enqueue(Packet);
 }
        /// <summary>
        /// Does the work.
        /// </summary>
        /// <param name="o">The object.</param>
        /// <remarks></remarks>
        protected void DoWork(object o)
        {
            if (listener.Pending())
            {
                RemoteClient c = new RemoteClient();
                c.TcpClient = listener.AcceptTcpClient();
                c.EndPoint = c.TcpClient.Client.RemoteEndPoint;
                Clients.Add(c);
            }

            int totalQueuedPackets = 0;
            for (int i = 0; i < Clients.Count; i++)
                totalQueuedPackets += Clients[i].PacketQueue.Count;
            // Any leftover time from each client is given to any client that could use it
            double additionalTime = 0;

            for (int i = 0; i < Clients.Count; i++)
            {
                if (!Clients[i].TcpClient.Connected)
                {
                    if (OnPlayerConnectionChanged != null && Clients[i].LoggedIn)
                        OnPlayerConnectionChanged(this, new PlayerConnectionEventArgs(ConnectionState.Disconnected, Clients[i]));
                    EnqueueToAllClientsInWorld(new DespawnPlayerPacket((byte)i), Clients[i].World);
                }
                else
                {
                    try
                    {
                        // Read in a packet
                        if (Clients[i].TcpClient.Available != 0)
                        {
                            Packet p = Packet.GetPacket((PacketID)Clients[i].TcpClient.GetStream().ReadByte(), true);
                            if (PrePacketEvent != null)
                                PrePacketEvent(this, new PrePacketEventArgs(p, Clients[i]));
                            if (cancelprepacket)
                            {
                                cancelprepacket = false;
                                goto here;
                            }
                            p.ReadPacket(Clients[i]);
                            p.HandlePacket(this, Clients[i]);
                            if (p.PacketID == PacketID.Identification && OnPlayerConnectionChanged != null)
                                OnPlayerConnectionChanged(this, new PlayerConnectionEventArgs(ConnectionState.Connected, Clients[i]));
                        }
                        here:
                        // Write out from the packet queue
                        if (Clients[i].PacketQueue.Count == 0)
                            continue;
                        // For the sake of server speed, each client is limited in the amount
                        // of time per tick they are allocated for sending of packets.
                        DateTime startTime = DateTime.Now;
                        double maxClientSendTime = double.PositiveInfinity;

                        if (!(totalQueuedPackets == 0 || Clients[i].PacketQueue.Count / totalQueuedPackets == 0))
                            maxClientSendTime = (1 / 20) * (Clients[i].PacketQueue.Count / totalQueuedPackets) + additionalTime;

                        additionalTime = 0;
                        while (Clients[i].PacketQueue.Count != 0 && (DateTime.Now - startTime).TotalMilliseconds <= maxClientSendTime)
                        {
                            // Send out packets from the queue
                            PacketID id = Clients[i].PacketQueue.Peek().PacketID;
                            Clients[i].PacketQueue.Dequeue().WritePacket(Clients[i]);
                            if (id == PacketID.DisconnectPlayer)
                            {
                                if (Clients[i].TcpClient.Connected)
                                    Clients[i].TcpClient.Close();
                                if (OnPlayerConnectionChanged != null)
                                    OnPlayerConnectionChanged(this, new PlayerConnectionEventArgs(ConnectionState.Disconnected, Clients[i]));
                                EnqueueToAllClientsInWorld(new DespawnPlayerPacket((byte)i), Clients[i].World);
                                Clients.RemoveAt(i);
                                i--;
                                break;
                            }
                        }
                        if ((DateTime.Now - startTime).TotalMilliseconds <= maxClientSendTime)
                            additionalTime += maxClientSendTime - (DateTime.Now - startTime).TotalMilliseconds;
                    }
                    catch
                    {
                        if (OnPlayerConnectionChanged != null && Clients[i].LoggedIn)
                            OnPlayerConnectionChanged(this, new PlayerConnectionEventArgs(ConnectionState.Disconnected, Clients[i]));
                        EnqueueToAllClients(new DespawnPlayerPacket((byte)i), Clients[i]); //TODO: Find out why this exceptions.
                        Clients.RemoveAt(i);
                        i--;
                    }
                }
            }

            netWorker = new Timer(new TimerCallback(DoWork), null, 0, Timeout.Infinite);
        }
Example #8
0
 /// <summary>
 /// Reads the packet.
 /// </summary>
 /// <param name="Client">The client.</param>
 /// <remarks></remarks>
 public override void ReadPacket(RemoteClient Client)
 {
     throw new InvalidOperationException();
 }
Example #9
0
 public PrePacketEventArgs(Packet packet, RemoteClient client)
 {
     this.Packet = packet; this.Client = client;
 }
 /// <summary>
 /// Reads the packet.
 /// </summary>
 /// <param name="Client">The client.</param>
 /// <remarks></remarks>
 public override void ReadPacket(RemoteClient Client)
 {
     Position.X = ReadShort(Client.TcpClient.GetStream());
     Position.Y = ReadShort(Client.TcpClient.GetStream());
     Position.Z = ReadShort(Client.TcpClient.GetStream());
     this.Mode = (byte)Client.TcpClient.GetStream().ReadByte();
     this.Type = (byte)Client.TcpClient.GetStream().ReadByte();
 }
 public PlayerDisconnectEvent(RemoteClient player)
 {
     this.player = player;
 }
 /// <summary>
 /// Handles the packet.
 /// </summary>
 /// <param name="Server">The server.</param>
 /// <param name="Client">The client.</param>
 /// <remarks></remarks>
 public override void HandlePacket(ClassicServer Server, RemoteClient Client)
 {
     World.Find(Client.World)._SetBlock(Position, Type);
     //Send the block to all clients.
     if (Mode != 0)
         Server.EnqueueToAllClients(new ServerSetBlockPacket(Position, Type));
     else
         Server.EnqueueToAllClients(new ServerSetBlockPacket(Position, 0));
 }
Example #13
0
 /// <summary>
 /// Initializes a new instance of the <see cref="PlayerConnectionEventArgs"/> class.
 /// </summary>
 /// <param name="ConnectionState">State of the connection.</param>
 /// <param name="Client">The client.</param>
 /// <remarks></remarks>
 public PlayerConnectionEventArgs(ConnectionState ConnectionState, RemoteClient Client)
 {
     this.ConnectionState = ConnectionState;
     this.Client          = Client;
 }
 /// <summary>
 /// Reads the packet.
 /// </summary>
 /// <param name="Client">The client.</param>
 /// <remarks></remarks>
 public override void ReadPacket(RemoteClient Client)
 {
     this.ProtocolVersion = (byte)Client.TcpClient.GetStream().ReadByte();
     this.UserNameOrServerName = ReadString(Client.TcpClient.GetStream());
     this.VerificationKeyOrMOTD = ReadString(Client.TcpClient.GetStream());
     Client.TcpClient.GetStream().ReadByte();
 }
 public PlayerChatEvent(RemoteClient player, string Message)
 {
     this.player = player;
     this.Message = Message;
 }
 public OnPlayerConnectEvent(RemoteClient player)
 {
     this.player = player;
 }
 /// <summary>
 /// Enqueues a packet to all clients.
 /// </summary>
 /// <param name="Packet">The packet.</param>
 /// <param name="IgnoreClient">If specified, the client won't recieve the packet.</param>
 /// <remarks></remarks>
 public void EnqueueToAllClients(Packet Packet, RemoteClient IgnoreClient = null)
 {
     foreach (RemoteClient Client in Clients)
         if (IgnoreClient != Client)
             Client.PacketQueue.Enqueue(Packet);
 }
 /// <summary>
 /// Handles the packet.
 /// </summary>
 /// <param name="Server">The server.</param>
 /// <param name="Client">The client.</param>
 /// <remarks></remarks>
 public override void HandlePacket(ClassicServer Server, RemoteClient Client)
 {
     throw new NotImplementedException();
 }
 /// <summary>
 /// Loads all players to client.
 /// </summary>
 /// <param name="Client">The client.</param>
 /// <remarks></remarks>
 public void LoadAllClientsToClient(RemoteClient Client)
 {
     foreach (RemoteClient others in Clients)
         if (others != Client)
             Client.PacketQueue.Enqueue(new SpawnPlayerPacket(others.PlayerID,
                 others.UserName, others.Position, others.Yaw, others.Pitch));
 }
 /// <summary>
 /// Reads the packet.
 /// </summary>
 /// <param name="Client">The client.</param>
 /// <remarks></remarks>
 public override void ReadPacket(RemoteClient Client)
 {
     throw new NotImplementedException();
 }
 /// <summary>
 /// Handles the packet.
 /// </summary>
 /// <param name="Server">The server.</param>
 /// <param name="Client">The client.</param>
 /// <remarks></remarks>
 public override void HandlePacket(ClassicServer Server, RemoteClient Client)
 {
     Client.Position = Position;
     Position.X = Position.X / 32;
     Position.Y = Position.Y / 32;
     Position.Z = Position.Z / 32;
     Client.Yaw = Yaw;
     Client.Pitch = Pitch;
     Server.EnqueueToAllClientsInWorld(new PositionAndOrientationPacket(Client.PlayerID, Position, Yaw, Pitch), Client.World, Client);
 }
Example #22
0
 /// <summary>
 /// Deals with an incoming packet from a RemoteClient/ClassicServer
 /// </summary>
 /// <param name="Server">The server involved</param>
 /// <param name="Client">The RemoteClient involved</param>
 public abstract void HandlePacket(ClassicServer Server, RemoteClient Client);
 /// <summary>
 /// Writes the packet.
 /// </summary>
 /// <param name="Client">The client.</param>
 /// <remarks></remarks>
 public override void WritePacket(RemoteClient Client)
 {
     Client.TcpClient.GetStream().Write(Payload, 0, Length);
 }
Example #24
0
 /// <summary>
 /// Reads a packet from a RemoteClient.
 /// </summary>
 /// <param name="Client">The sender of said packet.</param>
 public abstract void ReadPacket(RemoteClient Client);
 public OnPlayerChatEvent(RemoteClient c, string Message)
 {
     this.Message = Message; this.client = c;
 }
Example #26
0
 public static void KickPlayer(RemoteClient toKick)
 {
 }