Ejemplo n.º 1
0
        /// <summary>
        /// Create a new netIO class using a given socket.
        /// </summary>
        /// <param name="sock">The socket for this netIO class.</param>
        public NetIO(Socket sock, Dictionary<ushort, Packet> commandTable, Client client ,ClientManager manager)
        {
            this.sock = sock;
            this.stream = new NetworkStream(sock);
            this.commandTable = commandTable;
            this.client = client;
            this.currentClientManager = manager;
            Crypt = new Encryption();

            this.callbackSize = new AsyncCallback(this.ReceiveSize);
            this.callbackData = new AsyncCallback(this.ReceiveData);
            this.callbackKeyExchange= new AsyncCallback(this.ReceiveKeyExchange);
            // Use the static key untill the keys have been exchanged

            this.isDisconnected = false;
        }
Ejemplo n.º 2
0
        public NetIO(Socket sock, Dictionary<ushort, Packet> commandTable, Client client, bool isgateway)
        {
            this.sock = sock;
            this.stream = new NetworkStream(sock);
            this.commandTable = commandTable;
            this.client = client;
            this.isGateway = isgateway;

            this.callbackSize = new AsyncCallback(this.ReceiveSize);
            this.callbackData = new AsyncCallback(this.ReceiveData);
            this.nlock = new ReaderWriterLock();
            // Use the static key untill the keys have been exchanged
            this.clientKey = new byte[16];
            Encryption.StaticKey.CopyTo(this.clientKey, 0);
            this.serverKey = new byte[16];
            Encryption.StaticKey.CopyTo(this.serverKey, 0);

            this.isDisconnected = false;

            // Receive the size of the next packet and call ReceiveSize when finished
            if (sock.Connected)
            {
                try { stream.BeginRead(buffer, 0, 2, this.callbackSize, null); }
                catch (Exception ex)
                {
                    Logger.ShowError(ex, null);
                    try//this could crash the gateway somehow,so better ignore the Exception
                    {
                        this.Disconnect();
                    }
                    catch (Exception)
                    {
                    }
                    Logger.ShowWarning("Invalid packet head from:" + sock.RemoteEndPoint.ToString(), null);
                    return;
                }
            }
            else { this.Disconnect(); return; }
        }
Ejemplo n.º 3
0
 public override void Parse(SagaLib.Client client)
 {
     ((MapClient)(client)).OnAddShortcut(this);
 }
Ejemplo n.º 4
0
 public override void Parse(SagaLib.Client client)
 {
     ((MapClient)(client)).OnSetSpecialSkill(this);
 }
Ejemplo n.º 5
0
 public override void OnClientDisconnect(Client client)
 {
     MapClient client_ = (MapClient)client;
     if (this.clients.ContainsKey(client_.SessionID)) this.clients.Remove(client_.SessionID);
 }
Ejemplo n.º 6
0
        public override void OnClientDisconnect(Client client_t)
        {
            LoginClient client = (LoginClient)client_t;

            if (client.isMapServer)
            {
                Logger.ShowWarning("A map server just disconnected.",null);
                LoginServer.charServerList[client.mapServer.worldID].DeleteMapServer(client.mapServer);
            }
            this.clients.Remove(client.SessionID);
        }
Ejemplo n.º 7
0
 /// <summary>
 /// Parse this packet (only used for GetPackets)
 /// </summary>
 public virtual void Parse(Client client)
 {
     return;
 }
Ejemplo n.º 8
0
 public override void Parse(SagaLib.Client client)
 {
     ((MapClient)(client)).OnSendJump(this);
 }
Ejemplo n.º 9
0
 public override void Parse(SagaLib.Client client)
 {
     ((MapClient)(client)).OnPartyQuit(this);
 }
Ejemplo n.º 10
0
 public override void Parse(SagaLib.Client client)
 {
     ((MapClient)(client)).OnWeaponUpgrade(this);
 }
Ejemplo n.º 11
0
 public override void Parse(SagaLib.Client client)
 {
     ((MapClient)(client)).OnNPCShopSell(this);
 }
Ejemplo n.º 12
0
 public override void Parse(SagaLib.Client client)
 {
     ((MapClient)(client)).OnMarketUpdateComment(this);
 }
Ejemplo n.º 13
0
 public override void Parse(SagaLib.Client client)
 {
     ((LoginClient)(client)).OnPing(this);
 }
Ejemplo n.º 14
0
        public override void Parse(SagaLib.Client client)
        {
            MapClient cli = (MapClient)client;

            cli.OnSupplyExchange(this);
        }
Ejemplo n.º 15
0
 public override void Parse(SagaLib.Client client)
 {
     ((MapSession)(client)).OnSendKey(this);
 }
Ejemplo n.º 16
0
 public override void Parse(SagaLib.Client client)
 {
     ((MapClient)(client)).OnMotion(this);
 }
Ejemplo n.º 17
0
 public override void Parse(SagaLib.Client client)
 {
     ((LoginSession)(client)).OnSendGUID(this);
 }
Ejemplo n.º 18
0
 public override void Parse(SagaLib.Client client)
 {
     ((MapClient)(client)).OnGetMail(this);
 }
Ejemplo n.º 19
0
 public override void Parse(SagaLib.Client client)
 {
     ((MapClient)(client)).OnJobChange(this);
 }
Ejemplo n.º 20
0
 public override void Parse(SagaLib.Client client)
 {
     ((MapClient)(client)).OnLogout(this);
 }
Ejemplo n.º 21
0
 public override void OnClientDisconnect(Client client_t)
 {
     clients.Remove((LoginClient)client_t);
 }
Ejemplo n.º 22
0
 public override void Parse(SagaLib.Client client)
 {
     ((MapClient)(client)).OnSkillCast(this);
 }
Ejemplo n.º 23
0
        private void ReceiveData(IAsyncResult ar)
        {
            try
            {
                this.nlock.AcquireWriterLock(Timeout.Infinite);
                if (this.isDisconnected)
                {
                    this.nlock.ReleaseWriterLock();
                    return;
                }
                if (!sock.Connected)
                {
                    this.nlock.ReleaseWriterLock();
                    ClientManager.EnterCriticalArea();
                    this.Disconnect();
                    ClientManager.LeaveCriticalArea();
                    return;
                }
                try { stream.EndRead(ar); }
                catch (Exception)
                {
                    this.nlock.ReleaseWriterLock();
                    ClientManager.EnterCriticalArea();
                    this.Disconnect();
                    ClientManager.LeaveCriticalArea();
                    return;
                }
                byte[] raw = (byte[])ar.AsyncState;
                if (this.isGateway) raw = Encryption.Decrypt(raw, 2, this.ClientKey);
                if (!isGateway)
                {
                    ushort messageID = (ushort)(raw[7] + (raw[6] << 8));

                    if (!this.commandTable.ContainsKey((messageID)))
                    {
                        if (!this.fullHeader)
                        {
                            Logger.ShowWarning(string.Format("Got unknown packet {0:X} {1:X} from " + sock.RemoteEndPoint.ToString(), raw[6], raw[7]), null);

                        }
                        else
                        {
                            if (commandTable.ContainsKey((ushort)0xFFFF))
                            {
                                if (this.commandTable[(ushort)0xFFFF].SizeIsOk((ushort)raw.Length))
                                {
                                    Packet p = this.commandTable[(ushort)0xFFFF].New();
                                    p.data = raw;
                                    p.size = (ushort)(raw.Length);

                                    ClientManager.EnterCriticalArea();
                                    try
                                    {
                                        p.Parse(this.client);
                                    }
                                    catch (Exception ex)
                                    {
                                        Logger.ShowError(ex);
                                    }
                                    ClientManager.LeaveCriticalArea();
                                }
                                else
                                {
                                    string error = "Invalid packet size from client " + sock.RemoteEndPoint.ToString();
                                    Console.WriteLine(error);
                                    Log.WriteLog(error);
                                    ClientManager.EnterCriticalArea();
                                    this.Disconnect();
                                    ClientManager.LeaveCriticalArea();
                                    return;
                                }
                            }
                            else
                            {
                                Logger.ShowWarning("Universal Packet 0xFFFF not defined!", null);
                            }
                        }
                    }
                    else
                    {
                        if (this.commandTable[messageID].SizeIsOk((ushort)raw.Length))
                        {
                            Packet p = this.commandTable[messageID].New();
                            p.data = raw;
                            p.size = (ushort)(raw.Length);
                            Client client;
                            if (p.SessionID != 0)
                            {
                                client = this.currentClientManager.GetClient(p.SessionID);
                                if (client == null) client = this.client;
                            }
                            else
                            {
                                client = this.client;
                            }
                            ClientManager.EnterCriticalArea();
                            try
                            {
                                if (client.netIO == null) client.netIO = this;
                                p.Parse(client);
                            }
                            catch (Exception ex)
                            {
                                Logger.ShowError(ex);
                            }
                            ClientManager.LeaveCriticalArea();
                        }
                        else
                        {
                            string error = string.Format("Invalid packet size(Packet:{0:X4}) from client {1}",messageID, sock.RemoteEndPoint.ToString());
                            Console.WriteLine(error);
                            Log.WriteLog(error);
                            this.nlock.ReleaseWriterLock();
                            ClientManager.EnterCriticalArea();
                            this.Disconnect();
                            ClientManager.LeaveCriticalArea();
                            return;
                        }
                    }
                }
                else
                {
                    ushort messageID;
                    if (!fullHeader)
                    {
                        messageID = (ushort)(raw[7] + (raw[6] << 8));

                        if (!this.commandTable.ContainsKey((messageID)))
                        {
                            Logger.ShowWarning(string.Format("Got unknown packet {0:X} {1:X} from " + sock.RemoteEndPoint.ToString(), raw[6], raw[7]), null);

                        }
                        else
                        {
                            if (this.commandTable[messageID].SizeIsOk((ushort)raw.Length))
                            {
                                Packet p = this.commandTable[messageID].New();
                                p.data = raw;
                                p.size = (ushort)(raw.Length);
                                p.isGateway = this.isGateway;
                                ClientManager.EnterCriticalArea();
                                try
                                {
                                    p.Parse(this.client);
                                }
                                catch (Exception ex)
                                {
                                    Logger.ShowError(ex);
                                }
                                ClientManager.LeaveCriticalArea();
                            }
                            else
                            {
                                string error = "Invalid packet size from client " + sock.RemoteEndPoint.ToString();
                                Console.WriteLine(error);
                                Log.WriteLog(error);
                                this.nlock.ReleaseWriterLock();
                                ClientManager.EnterCriticalArea();
                                this.Disconnect();
                                ClientManager.LeaveCriticalArea();
                                return;
                            }
                        }
                    }
                    else
                    {
                        if (commandTable.ContainsKey((ushort)0xFFFF))
                        {
                            if (this.commandTable[(ushort)0xFFFF].SizeIsOk((ushort)raw.Length))
                            {
                                Packet p = this.commandTable[(ushort)0xFFFF].New();
                                p.data = raw;
                                p.size = (ushort)(raw.Length);
                                p.isGateway = this.isGateway;
                                ClientManager.EnterCriticalArea();
                                try
                                {
                                    p.Parse(this.client);
                                }
                                catch (Exception ex)
                                {
                                    Logger.ShowError(ex);
                                }
                                ClientManager.LeaveCriticalArea();
                            }
                            else
                            {
                                string error = "Invalid packet size from client " + sock.RemoteEndPoint.ToString();
                                Console.WriteLine(error);
                                Log.WriteLog(error);
                                this.nlock.ReleaseWriterLock();
                                ClientManager.EnterCriticalArea();
                                this.Disconnect();
                                ClientManager.LeaveCriticalArea();
                                return;
                            }
                        }
                        else
                        {
                            Logger.ShowWarning("Universal Packet 0xFFFF not defined!", null);
                        }
                    }
                }
                try { this.nlock.ReleaseWriterLock(); stream.BeginRead(buffer, 0, 2, this.callbackSize, null); }
                catch (Exception)
                {
                    ClientManager.EnterCriticalArea();
                    this.Disconnect();
                    ClientManager.LeaveCriticalArea();
                    return;
                }
            }
            catch (Exception e) { Logger.ShowError(e, null); }
        }
Ejemplo n.º 24
0
        public override void Parse(SagaLib.Client client)
        {
            MapClient cli = (MapClient)client;

            cli.OnWarp(this);
        }
Ejemplo n.º 25
0
 public virtual void OnClientDisconnect(Client client){ }
Ejemplo n.º 26
0
 public override void Parse(SagaLib.Client client)
 {
     ((LoginClient)(client)).OnCharCreate(this);
 }
Ejemplo n.º 27
0
        public override void OnClientDisconnect(Client client_t)
        {
            GatewayClient client = (GatewayClient)client_t;

            this.clients.Remove(client.SessionID);
        }
Ejemplo n.º 28
0
 public override void Parse(SagaLib.Client client)
 {
     ((MapClient)(client)).OnTradeMoney(this);
 }