Beispiel #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;
        }
Beispiel #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; }
        }
Beispiel #3
0
 public override void Parse(SagaLib.Client client)
 {
     ((MapClient)(client)).OnAddShortcut(this);
 }
Beispiel #4
0
 public override void Parse(SagaLib.Client client)
 {
     ((MapClient)(client)).OnSetSpecialSkill(this);
 }
Beispiel #5
0
 public override void OnClientDisconnect(Client client)
 {
     MapClient client_ = (MapClient)client;
     if (this.clients.ContainsKey(client_.SessionID)) this.clients.Remove(client_.SessionID);
 }
Beispiel #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);
        }
Beispiel #7
0
 /// <summary>
 /// Parse this packet (only used for GetPackets)
 /// </summary>
 public virtual void Parse(Client client)
 {
     return;
 }
Beispiel #8
0
 public override void Parse(SagaLib.Client client)
 {
     ((MapClient)(client)).OnSendJump(this);
 }
Beispiel #9
0
 public override void Parse(SagaLib.Client client)
 {
     ((MapClient)(client)).OnPartyQuit(this);
 }
Beispiel #10
0
 public override void Parse(SagaLib.Client client)
 {
     ((MapClient)(client)).OnWeaponUpgrade(this);
 }
Beispiel #11
0
 public override void Parse(SagaLib.Client client)
 {
     ((MapClient)(client)).OnNPCShopSell(this);
 }
 public override void Parse(SagaLib.Client client)
 {
     ((MapClient)(client)).OnMarketUpdateComment(this);
 }
Beispiel #13
0
 public override void Parse(SagaLib.Client client)
 {
     ((LoginClient)(client)).OnPing(this);
 }
Beispiel #14
0
        public override void Parse(SagaLib.Client client)
        {
            MapClient cli = (MapClient)client;

            cli.OnSupplyExchange(this);
        }
Beispiel #15
0
 public override void Parse(SagaLib.Client client)
 {
     ((MapSession)(client)).OnSendKey(this);
 }
Beispiel #16
0
 public override void Parse(SagaLib.Client client)
 {
     ((MapClient)(client)).OnMotion(this);
 }
Beispiel #17
0
 public override void Parse(SagaLib.Client client)
 {
     ((LoginSession)(client)).OnSendGUID(this);
 }
Beispiel #18
0
 public override void Parse(SagaLib.Client client)
 {
     ((MapClient)(client)).OnGetMail(this);
 }
Beispiel #19
0
 public override void Parse(SagaLib.Client client)
 {
     ((MapClient)(client)).OnJobChange(this);
 }
Beispiel #20
0
 public override void Parse(SagaLib.Client client)
 {
     ((MapClient)(client)).OnLogout(this);
 }
 public override void OnClientDisconnect(Client client_t)
 {
     clients.Remove((LoginClient)client_t);
 }
Beispiel #22
0
 public override void Parse(SagaLib.Client client)
 {
     ((MapClient)(client)).OnSkillCast(this);
 }
Beispiel #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); }
        }
Beispiel #24
0
        public override void Parse(SagaLib.Client client)
        {
            MapClient cli = (MapClient)client;

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

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