private void Client_OnReceivedData(TSOClient.Network.PacketStream Packet)
        {
            switch (Packet.PacketID)
            {
            case 0x01:
                UIPacketHandlers.OnInitLoginNotify(NetworkFacade.Client, Packet);
                NetworkFacade.UpdateLoginProgress(2);
                break;

            case 0x02:
                NetworkFacade.LoginWait.Set();
                UIPacketHandlers.OnLoginFailResponse(ref NetworkFacade.Client, Packet);
                break;

            case 0x05:
                NetworkFacade.LoginOK = true;
                NetworkFacade.LoginWait.Set();

                NetworkFacade.UpdateLoginProgress(3);

                UIPacketHandlers.OnCharacterInfoResponse(Packet, NetworkFacade.Client);
                break;

            case 0x06:
                UIPacketHandlers.OnCityInfoResponse(Packet);
                break;
            }
        }
        /// <summary>
        /// Occurs when the client has been sucessfully authenticated by the loginserver.
        /// Called by UILoginDialog.cs.
        /// </summary>
        /// <param name="Client">The client that received the packet.</param>
        /// <param name="Packet">The packet that was received.</param>
        public static void OnInitLoginNotify(NetworkClient Client, PacketStream Packet)
        {
            byte Opcode = (byte)Packet.ReadByte();

            //Account was authenticated, so add the client to the player's account.
            PlayerAccount.Client = Client;

            if (!Directory.Exists("CharacterCache"))
            {
                Directory.CreateDirectory("CharacterCache");

                //The charactercache didn't exist, so send the current time, which is
                //newer than the server's stamp. This will cause the server to send the entire cache.
                UIPacketSenders.SendCharacterInfoRequest(DateTime.Now.ToString("yyyy.MM.dd hh:mm:ss"));
            }
            else
            {
                if (!File.Exists("CharacterCache\\Sims.tempcache"))
                {
                    //The charactercache didn't exist, so send the current time, which is
                    //newer than the server's stamp. This will cause the server to send the entire cache.
                    UIPacketSenders.SendCharacterInfoRequest(DateTime.Now.ToString("yyyy.MM.dd hh:mm:ss"));
                }
            }
        }
Esempio n. 3
0
 public static void Handle(PacketStream stream)
 {
     byte ID = (byte)stream.ReadByte();
     if (m_Handlers.ContainsKey(ID))
     {
         m_Handlers[ID].Handler(stream);
     }
 }
Esempio n. 4
0
 private void m_Client_OnReceivedData(PacketStream Packet)
 {
     switch (Packet.PacketID)
     {
         case 0x10: //TODO: Change ID for this packet!
             LotPacketHandlers.OnSimulationState(m_Client, Packet, this);
             break;
     }
 }
        /// <summary>
        /// Sends a packet to create a SimulationObject on ter server.
        /// Assumes the player is on a lot that he owns.
        /// </summary>
        /// <param name="CreatedObject">The SimulationObject to create.</param>
        public static void SendCreatedSimulationObject(SimulationObject CreatedObject)
        {
            //TODO: Change this ID!
            PacketStream CreateSimulationObjectPacket = new PacketStream(0x11, 0);

            BinaryFormatter BinFormatter = new BinaryFormatter();
            BinFormatter.Serialize(CreateSimulationObjectPacket, CreatedObject);

            PlayerAccount.Client.Send(FinalizePacket(0x11, new DESCryptoServiceProvider(),
                CreateSimulationObjectPacket.ToArray()));
        }
        public static void SendCharacterInfoRequest(string TimeStamp)
        {
            PacketStream Packet = new PacketStream(0x05, 0);
            //If this timestamp is newer than the server's timestamp, it means
            //the client doesn't have a charactercache. If it's older, it means
            //the cache needs to be updated. If it matches, the server sends an
            //empty responsepacket.
            //Packet.WriteString(TimeStamp);
            Packet.WriteByte((byte)TimeStamp.Length);
            Packet.WriteBytes(Encoding.ASCII.GetBytes(TimeStamp));

            byte[] PacketData = Packet.ToArray();

            PlayerAccount.Client.Send(FinalizePacket(0x05, new DESCryptoServiceProvider(), PacketData));
        }
        public static void OnSimulationState(NetworkClient Client, PacketStream Packet, LotScreen Lot)
        {
            List<SimulationObject> SimObjects = new List<SimulationObject>();

            byte Opcode = (byte)Packet.ReadByte();

            byte NumTicks = (byte)Packet.ReadByte();
            int NumObjects = Packet.ReadInt32();
            BinaryFormatter BinFormatter = new BinaryFormatter();

            for (int i = 0; i < NumObjects; i++)
            {
                SimulationObject SimObject = (SimulationObject)BinFormatter.Deserialize(Packet);
                SimObjects.Add(SimObject);
            }

            Lot.UpdateSimulationState(NumTicks, SimObjects);
        }
        /// <summary>
        /// LoginServer sent information about the player's characters.
        /// </summary>
        /// <param name="Packet">The packet that was received.</param>
        public static void OnCharacterInfoResponse(PacketStream Packet, NetworkClient Client)
        {
            byte Opcode = (byte)Packet.ReadByte();
            byte Length = (byte)Packet.ReadByte();
            byte DecryptedLength = (byte)Packet.ReadByte();

            Packet.DecryptPacket(PlayerAccount.EncKey, new DESCryptoServiceProvider(), DecryptedLength);

            //If the decrypted length == 1, it means that there were 0
            //characters that needed to be updated, or that the user
            //hasn't created any characters on his/her account yet.
            //Since the Packet.Length property is equal to the length
            //of the encrypted data, it cannot be used to get the length
            //of the decrypted data.
            if (DecryptedLength > 1)
            {
                byte NumCharacters = (byte)Packet.ReadByte();
                List<Sim> FreshSims = new List<Sim>();

                for (int i = 0; i < NumCharacters; i++)
                {
                    int CharacterID = Packet.ReadInt32();

                    Sim FreshSim = new Sim(Packet.ReadString());
                    FreshSim.CharacterID = CharacterID;
                    FreshSim.Timestamp = Packet.ReadString();
                    FreshSim.Name = Packet.ReadString();
                    FreshSim.Sex = Packet.ReadString();

                    FreshSims.Add(FreshSim);
                }

                NetworkFacade.Avatars = FreshSims;
                CacheSims(FreshSims);
            }

            PacketStream CityInfoRequest = new PacketStream(0x06, 0);
            CityInfoRequest.WriteByte(0x00); //Dummy

            Client.SendEncrypted(0x06, CityInfoRequest.ToArray());
        }
        public static void SendLoginRequest(NetworkClient Client, string Username, string Password)
        {
            //Variable length...
            PacketStream Packet = new PacketStream(0x00, 0);
            Packet.WriteByte(0x00);

            SaltedHash Hash = new SaltedHash(new SHA512Managed(), Username.Length);
            byte[] HashBuf = new byte[Encoding.ASCII.GetBytes(Password).Length +
                Encoding.ASCII.GetBytes(Username).Length];

            MemoryStream MemStream = new MemoryStream();

            PasswordDeriveBytes Pwd = new PasswordDeriveBytes(Encoding.ASCII.GetBytes(Password),
                Encoding.ASCII.GetBytes("SALT"), "SHA1", 10);
            byte[] EncKey = Pwd.GetBytes(8);
            PlayerAccount.EncKey = EncKey;

            MemStream.WriteByte((byte)Username.Length);
            MemStream.Write(Encoding.ASCII.GetBytes(Username), 0, Encoding.ASCII.GetBytes(Username).Length);

            HashBuf = Hash.ComputePasswordHash(Username, Password);
            PlayerAccount.Hash = HashBuf;

            MemStream.WriteByte((byte)HashBuf.Length);
            MemStream.Write(HashBuf, 0, HashBuf.Length);

            MemStream.WriteByte((byte)EncKey.Length);
            MemStream.Write(EncKey, 0, EncKey.Length);

            Packet.WriteByte((byte)(2 + MemStream.ToArray().Length + 4));
            Packet.WriteBytes(MemStream.ToArray());
            //TODO: Change this to write a global client version.
            Packet.WriteByte(0x00); //Version 1
            Packet.WriteByte(0x00); //Version 2
            Packet.WriteByte(0x00); //Version 3
            Packet.WriteByte(0x01); //Version 4

            Client.Send(Packet.ToArray());
        }
        /// <summary>
        /// Occurs when the client was not authenticated by the loginserver.
        /// Called by UILoginDialog.cs.
        /// </summary>
        /// <param name="Client">The client that received the packet.</param>
        /// <param name="Packet">The packet that was received.</param>
        /// <param name="Screen">A UIScreen instance on which to display a messagebox to inform the player of the
        ///                      failure state.</param>
        public static void OnLoginFailResponse(ref NetworkClient Client, PacketStream Packet)
        {
            byte Opcode = (byte)Packet.ReadByte();

            switch (Packet.ReadByte())
            {
                case 0x01:
                    UIAlertOptions Options = new UIAlertOptions();
                    Options.Title = "Network error";
                    Options.Message = "Invalid username!";
                    Options.Buttons = UIAlertButtons.OK;
                    UIScreen.ShowAlert(Options, true);
                    break;
                case 0x02:
                    Options = new UIAlertOptions();
                    Options.Title = "Network error";
                    Options.Message = "Invalid password!";
                    Options.Buttons = UIAlertButtons.OK;
                    UIScreen.ShowAlert(Options, true);
                    break;
            }

            Client.Disconnect();
        }
        private void m_Client_OnReceivedData(PacketStream Packet)
        {
            switch (Packet.PacketID)
            {

            }
        }
        public static void OnCityInfoResponse(PacketStream Packet)
        {
            byte Opcode = (byte)Packet.ReadByte();
            byte Length = (byte)Packet.ReadByte();
            byte DecryptedLength = (byte)Packet.ReadByte();

            Packet.DecryptPacket(PlayerAccount.EncKey, new DESCryptoServiceProvider(), DecryptedLength);

            byte NumCities = (byte)Packet.ReadByte();

            for (int i = 0; i < NumCities; i++)
            {
                string Name = Packet.ReadString();
                string Description = Packet.ReadString();
                string IP = Packet.ReadString();
                int Port = Packet.ReadInt32();
                CityInfoStatus Status = (CityInfoStatus)Packet.ReadByte();
                ulong Thumbnail = Packet.ReadUInt64();
                string UUID = Packet.ReadString();

                CityInfo Info = new CityInfo(Name, Description, Thumbnail, UUID, 0, IP, Port);
                NetworkFacade.Cities.Add(Info);
            }
        }
        /// <summary>
        /// Occurs when the client was not authenticated by the loginserver.
        /// Called by UILoginDialog.cs.
        /// </summary>
        /// <param name="Client">The client that received the packet.</param>
        /// <param name="Packet">The packet that was received.</param>
        /// <param name="Screen">A UIScreen instance on which to display a messagebox to inform the player of the
        ///                      failure state.</param>
        public static void OnLoginFailResponse(ref NetworkClient Client, PacketStream Packet, UIScreen Screen)
        {
            byte Opcode = (byte)Packet.ReadByte();

            switch (Packet.ReadByte())
            {
                case 0x01:
                    Screen.CreateMsgBox(250, 200, "Invalid accountname!");
                    break;
                case 0x02:
                    Screen.CreateMsgBox(250, 200, "Invalid password!");
                    break;
            }

            Client.Disconnect();
        }
        private void ReceiveCallback(IAsyncResult AR)
        {
            try
            {
                Socket Sock = (Socket)AR.AsyncState;
                int NumBytesRead = Sock.EndReceive(AR);

                Log.LogThis("Received: " + NumBytesRead + " bytes!", eloglevel.info);

                byte[] TmpBuf = new byte[NumBytesRead];
                Buffer.BlockCopy(m_RecvBuf, 0, TmpBuf, 0, NumBytesRead);

                //The packet is given an ID of 0x00 because its ID is currently unknown.
                PacketStream TempPacket = new PacketStream(0x00, NumBytesRead, TmpBuf);
                byte ID = TempPacket.PeekByte(0);
                int PacketLength = 0;

                bool FoundMatchingID = false;

                FoundMatchingID = FindMatchingPacketID(ID);

                if (FoundMatchingID)
                {
                    PacketLength = m_PacketIDs[ID];

                    Log.LogThis("PacketLength: " + PacketLength, eloglevel.info);
                    Log.LogThis("Found matching PacketID (" + ID + ")!\r\n\r\n", eloglevel.info);

                    if (NumBytesRead == PacketLength)
                    {
                        Log.LogThis("Got packet - exact length!\r\n\r\n", eloglevel.info);
                        m_RecvBuf = new byte[11024];

                        OnReceivedData(new PacketStream(ID, PacketLength, TempPacket.ToArray()));
                    }
                    else if (NumBytesRead < PacketLength)
                    {
                        m_TempPacket = new PacketStream(ID, PacketLength);
                        byte[] TmpBuffer = new byte[NumBytesRead];

                        //Store the number of bytes that were read in the temporary buffer.
                        Log.LogThis("Got data, but not a full packet - stored " +
                            NumBytesRead.ToString() + "bytes!\r\n\r\n", eloglevel.info);
                        Buffer.BlockCopy(m_RecvBuf, 0, TmpBuffer, 0, NumBytesRead);
                        m_TempPacket.WriteBytes(TmpBuffer);

                        //And reset the buffers!
                        m_RecvBuf = new byte[11024];
                        TmpBuffer = null;
                    }
                    else if (PacketLength == 0)
                    {
                        Log.LogThis("Received variable length packet!\r\n", eloglevel.info);

                        if (NumBytesRead > 2)
                        {
                            PacketLength = TempPacket.PeekByte(1);

                            if (NumBytesRead == PacketLength)
                            {
                                Log.LogThis("Received exact number of bytes for packet!\r\n", eloglevel.info);

                                m_RecvBuf = new byte[11024];
                                m_TempPacket = null;
                                OnReceivedData(new PacketStream(ID, PacketLength, TempPacket.ToArray()));
                            }
                            else if (NumBytesRead < PacketLength)
                            {
                                Log.LogThis("Didn't receive entire packet - stored: " + PacketLength + " bytes!\r\n",
                                    eloglevel.info);

                                TempPacket.SetLength(PacketLength);
                                m_TempPacket = TempPacket;
                                m_RecvBuf = new byte[11024];
                            }
                            else if (NumBytesRead > PacketLength)
                            {
                                Log.LogThis("Received more bytes than needed for packet. Excess: " +
                                    (NumBytesRead - PacketLength) + "\r\n", eloglevel.info);

                                byte[] TmpBuffer = new byte[NumBytesRead - PacketLength];
                                Buffer.BlockCopy(TempPacket.ToArray(), 0, TmpBuffer, 0, TmpBuffer.Length);
                                m_TempPacket = new PacketStream(TmpBuffer[0], NumBytesRead - PacketLength,
                                    TmpBuffer);

                                byte[] PacketBuffer = new byte[PacketLength];
                                Buffer.BlockCopy(TempPacket.ToArray(), 0, PacketBuffer, 0, PacketBuffer.Length);

                                m_RecvBuf = new byte[11024];
                                OnReceivedData(new PacketStream(ID, PacketLength, PacketBuffer));
                            }
                        }
                    }
                }
                else
                {
                    if (m_TempPacket != null)
                    {
                        if (m_TempPacket.Length < m_TempPacket.BufferLength)
                        {
                            //Received the exact number of bytes needed to complete the stored packet.
                            if ((m_TempPacket.BufferLength + NumBytesRead) == m_TempPacket.Length)
                            {
                                byte[] TmpBuffer = new byte[NumBytesRead];
                                Buffer.BlockCopy(m_RecvBuf, 0, TmpBuffer, 0, NumBytesRead);

                                m_RecvBuf = new byte[11024];
                                TmpBuffer = null;
                            }
                            //Received more than the number of bytes needed to complete the packet!
                            else if ((m_TempPacket.BufferLength + NumBytesRead) > m_TempPacket.Length)
                            {
                                int Target = (int)((m_TempPacket.BufferLength + NumBytesRead) - m_TempPacket.Length);
                                byte[] TmpBuffer = new byte[Target];

                                Buffer.BlockCopy(m_RecvBuf, 0, TmpBuffer, 0, Target);
                                m_TempPacket.WriteBytes(TmpBuffer);

                                //Now we have a full packet, so call the received event!
                                OnReceivedData(new PacketStream(m_TempPacket.PacketID,
                                    (int)m_TempPacket.Length, m_TempPacket.ToArray()));

                                //Copy the remaining bytes in the receiving buffer.
                                TmpBuffer = new byte[NumBytesRead - Target];
                                Buffer.BlockCopy(m_RecvBuf, Target, TmpBuffer, 0, (NumBytesRead - Target));

                                //Give the temporary packet an ID of 0x00 since we don't know its ID yet.
                                TempPacket = new PacketStream(0x00, NumBytesRead - Target, TmpBuffer);
                                ID = TempPacket.PeekByte(0);

                                //This SHOULD be an existing ID, but let's sanity-check it...
                                if (FindMatchingPacketID(ID))
                                {
                                    m_TempPacket = new PacketStream(ID, m_PacketIDs[ID], TempPacket.ToArray());

                                    //Congratulations, you just received another packet!
                                    if (m_TempPacket.Length == m_TempPacket.BufferLength)
                                    {
                                        OnReceivedData(new PacketStream(m_TempPacket.PacketID,
                                            (int)m_TempPacket.Length, m_TempPacket.ToArray()));

                                        //No more data to store on this read, so reset everything...
                                        m_TempPacket = null;
                                        TmpBuffer = null;
                                        m_RecvBuf = new byte[11024];
                                    }
                                }
                                else
                                {
                                    //Houston, we have a problem (this should never occur)!
                                }
                            }
                        }
                    }
                }

                m_Sock.BeginReceive(m_RecvBuf, 0, m_RecvBuf.Length, SocketFlags.None,
                    new AsyncCallback(ReceiveCallback), m_Sock);
            }
            catch (SocketException E)
            {
                Log.LogThis("SocketException: " + E.ToString(), eloglevel.info);
                Disconnect();
            }
        }
Esempio n. 15
0
        /// <summary>
        /// Called when the NetworkClient received a new packet.
        /// </summary>
        /// <param name="Packet">The packet received.</param>
        private void m_Client_OnReceivedData(PacketStream Packet)
        {
            switch (Packet.PacketID)
            {
                //InitLoginNotify - 21 bytes
                case 0x01:
                    m_OverallProgressbar.UpdateStatus("25%");
                    m_CurrentTaskbar.UpdateStatus("Attempting authorization...");

                    UIPacketHandlers.OnInitLoginNotify(m_Client, Packet);

                    break;
                //LoginFailResponse - 2 bytes
                case 0x02:
                    UIPacketHandlers.OnLoginFailResponse(ref m_Client, Packet, m_Screen);

                    break;
                //CharacterInfoResponse
                case 0x05:
                    m_OverallProgressbar.UpdateStatus("50%");
                    m_CurrentTaskbar.UpdateStatus("Success!");

                    UIPacketHandlers.OnCharacterInfoResponse(m_Client, Packet);
                    LuaInterfaceManager.CallFunction("LoginSuccess");

                    break;
            }
        }
Esempio n. 16
0
        private void ReceiveCallback(IAsyncResult AR)
        {
            try
            {
                Socket Sock         = (Socket)AR.AsyncState;
                int    NumBytesRead = Sock.EndReceive(AR);

                /** Cant do anything with this! **/
                if (NumBytesRead == 0)
                {
                    return;
                }

                Log.LogThis("Received: " + NumBytesRead + " bytes!", eloglevel.info);

                byte[] TmpBuf = new byte[NumBytesRead];
                Buffer.BlockCopy(m_RecvBuf, 0, TmpBuf, 0, NumBytesRead);

                //The packet is given an ID of 0x00 because its ID is currently unknown.
                PacketStream TempPacket = new PacketStream(0x00, NumBytesRead, TmpBuf);
                byte         ID         = TempPacket.PeekByte(0);

                int PacketLength = 0;
                var handler      = FindPacketHandler(ID);

                if (handler != null)
                {
                    PacketLength = handler.Length;

                    Log.LogThis("PacketLength: " + PacketLength, eloglevel.info);
                    Log.LogThis("Found matching PacketID (" + ID + ")!\r\n\r\n", eloglevel.info);

                    if (NumBytesRead == PacketLength)
                    {
                        Log.LogThis("Got packet - exact length!\r\n\r\n", eloglevel.info);
                        m_RecvBuf = new byte[11024];

                        OnPacket(new PacketStream(ID, PacketLength, TempPacket.ToArray()), handler);
                    }
                    else if (NumBytesRead < PacketLength)
                    {
                        m_TempPacket = new PacketStream(ID, PacketLength);
                        byte[] TmpBuffer = new byte[NumBytesRead];

                        //Store the number of bytes that were read in the temporary buffer.
                        Log.LogThis("Got data, but not a full packet - stored " +
                                    NumBytesRead.ToString() + "bytes!\r\n\r\n", eloglevel.info);
                        Buffer.BlockCopy(m_RecvBuf, 0, TmpBuffer, 0, NumBytesRead);
                        m_TempPacket.WriteBytes(TmpBuffer);

                        //And reset the buffers!
                        m_RecvBuf = new byte[11024];
                        TmpBuffer = null;
                    }
                    else if (PacketLength == 0)
                    {
                        Log.LogThis("Received variable length packet!\r\n", eloglevel.info);

                        if (NumBytesRead > (int)PacketHeaders.UNENCRYPTED) //Header is 3 bytes.
                        {
                            PacketLength = TempPacket.PeekUShort(1);

                            if (NumBytesRead == PacketLength)
                            {
                                Log.LogThis("Received exact number of bytes for packet!\r\n", eloglevel.info);

                                m_RecvBuf    = new byte[11024];
                                m_TempPacket = null;
                                OnPacket(new PacketStream(ID, PacketLength, TempPacket.ToArray()), handler);
                            }
                            else if (NumBytesRead < PacketLength)
                            {
                                Log.LogThis("Didn't receive entire packet - stored: " + PacketLength + " bytes!\r\n",
                                            eloglevel.info);

                                TempPacket.SetLength(PacketLength);
                                m_TempPacket = TempPacket;
                                m_RecvBuf    = new byte[11024];
                            }
                            else if (NumBytesRead > PacketLength)
                            {
                                Log.LogThis("Received more bytes than needed for packet. Excess: " +
                                            (NumBytesRead - PacketLength) + "\r\n", eloglevel.info);

                                byte[] TmpBuffer = new byte[NumBytesRead - PacketLength];
                                Buffer.BlockCopy(TempPacket.ToArray(), 0, TmpBuffer, 0, TmpBuffer.Length);
                                m_TempPacket = new PacketStream(TmpBuffer[0], NumBytesRead - PacketLength,
                                                                TmpBuffer);

                                byte[] PacketBuffer = new byte[PacketLength];
                                Buffer.BlockCopy(TempPacket.ToArray(), 0, PacketBuffer, 0, PacketBuffer.Length);

                                m_RecvBuf = new byte[11024];
                                OnPacket(new PacketStream(ID, PacketLength, PacketBuffer), handler);
                            }
                        }
                    }
                }
                else
                {
                    if (m_TempPacket != null)
                    {
                        if (m_TempPacket.Length < m_TempPacket.BufferLength)
                        {
                            //Received the exact number of bytes needed to complete the stored packet.
                            if ((m_TempPacket.BufferLength + NumBytesRead) == m_TempPacket.Length)
                            {
                                byte[] TmpBuffer = new byte[NumBytesRead];
                                Buffer.BlockCopy(m_RecvBuf, 0, TmpBuffer, 0, NumBytesRead);

                                m_RecvBuf = new byte[11024];
                                TmpBuffer = null;
                            }
                            //Received more than the number of bytes needed to complete the packet!
                            else if ((m_TempPacket.BufferLength + NumBytesRead) > m_TempPacket.Length)
                            {
                                int    Target    = (int)((m_TempPacket.BufferLength + NumBytesRead) - m_TempPacket.Length);
                                byte[] TmpBuffer = new byte[Target];

                                Buffer.BlockCopy(m_RecvBuf, 0, TmpBuffer, 0, Target);
                                m_TempPacket.WriteBytes(TmpBuffer);

                                //Now we have a full packet, so call the received event!
                                OnPacket(new PacketStream(m_TempPacket.PacketID,
                                                          (int)m_TempPacket.Length, m_TempPacket.ToArray()), handler);

                                //Copy the remaining bytes in the receiving buffer.
                                TmpBuffer = new byte[NumBytesRead - Target];
                                Buffer.BlockCopy(m_RecvBuf, Target, TmpBuffer, 0, (NumBytesRead - Target));

                                //Give the temporary packet an ID of 0x00 since we don't know its ID yet.
                                TempPacket = new PacketStream(0x00, NumBytesRead - Target, TmpBuffer);
                                ID         = TempPacket.PeekByte(0);
                                handler    = FindPacketHandler(ID);

                                //This SHOULD be an existing ID, but let's sanity-check it...
                                if (handler != null)
                                {
                                    m_TempPacket = new PacketStream(ID, handler.Length, TempPacket.ToArray());

                                    //Congratulations, you just received another packet!
                                    if (m_TempPacket.Length == m_TempPacket.BufferLength)
                                    {
                                        OnPacket(new PacketStream(m_TempPacket.PacketID,
                                                                  (int)m_TempPacket.Length, m_TempPacket.ToArray()), handler);

                                        //No more data to store on this read, so reset everything...
                                        m_TempPacket = null;
                                        TmpBuffer    = null;
                                        m_RecvBuf    = new byte[11024];
                                    }
                                }
                                else
                                {
                                    //Houston, we have a problem (this should never occur)!
                                }
                            }
                        }
                    }
                }

                m_Sock.BeginReceive(m_RecvBuf, 0, m_RecvBuf.Length, SocketFlags.None,
                                    new AsyncCallback(ReceiveCallback), m_Sock);
            }
            catch (SocketException E)
            {
                Log.LogThis("SocketException: " + E.ToString(), eloglevel.info);
                Disconnect();
            }
        }
Esempio n. 17
0
        private void OnPacket(PacketStream packet, PacketHandler handler)
        {
            if (OnReceivedData != null)
            {
                OnReceivedData(packet);
            }

            handler.Handler(packet);
        }