Esempio n. 1
0
        public void WaitForInit()
        {
            RecBuffer recBuffer = new RecBuffer(2, false);
            Thread    th        = new Thread(delegate() { ReceivePacket(recBuffer); });

            th.Start();
            //ReceivePacket(recBuffer);
        }
Esempio n. 2
0
        private void PacketCallback(IAsyncResult pIAR)
        {
            try
            {
                int rec = _sock.EndReceive(pIAR);
                if (rec == 0 && OnDisconnectedHandler != null)
                {
                    DisconnectedEventArgs eventArgs = new DisconnectedEventArgs(DisconnectType.Server);
                    OnDisconnectedHandler(this, eventArgs);
                    return;
                }

                RecBuffer recBuffer = (RecBuffer)pIAR.AsyncState;
                recBuffer.Index += rec;

                if (recBuffer.IsReady)
                {
                    HandleData(recBuffer);
                }
                else
                {
                    ReceivePacket(recBuffer);
                }
            }
            #region Error handler

            /*
             * catch (SocketException se)
             * {
             *
             * if (se.SocketErrorCode == SocketError.ConnectionAborted || se.SocketErrorCode == SocketError.ConnectionReset)
             * {
             *  if (OnDisconnected != null)
             *  {
             *      OnDisconnected(this);
             *  }
             * }
             * else
             * {
             *  Extensions.Logger.LogMessage("Error - PacketCallback - " + se.ToString());
             * }
             * }
             * catch (ObjectDisposedException)
             * {
             * Extensions.Logger.LogMessage("Error - PacketCallback - Socket closed.");
             * }
             * catch (NullReferenceException)
             * {
             * Extensions.Logger.LogMessage("Error - PacketCallback - NullReferenceException.");
             * }
             */
            #endregion
            catch (Exception ex)
            {
                if (OnDisconnectedHandler != null)
                {
                    DisconnectedEventArgs eventArgs = new DisconnectedEventArgs(ex.ToString());
                    OnDisconnectedHandler(this, eventArgs);
                }
            }
        }
Esempio n. 3
0
 private void ReceivePacket(RecBuffer pBuffer)
 {
     _sock.BeginReceive(pBuffer.Data, pBuffer.Index, pBuffer.Position, SocketFlags.None, new AsyncCallback(PacketCallback), pBuffer);
     //_sock.BeginReceive(pRawPacket, 0, pRawPacket.Length, SocketFlags.None, SendCallback, pAsyncState); //DO NOT USE
 }
Esempio n. 4
0
        public void WaitForPacket()
        {
            RecBuffer recBuffer = new RecBuffer(4, true);

            ReceivePacket(recBuffer);
        }
Esempio n. 5
0
        private void HandleData(RecBuffer pBuffer)
        {
            if (pBuffer.IsHeader)
            {
                byte[] headerData = pBuffer.Data;

                int packetSize = pBuffer.IsEncrypted ?
                                 MapleCrypt.GetPacketLength(headerData) :
                                 BitConverter.ToInt16(headerData, 0);

                pBuffer.Reset(false, packetSize);
                ReceivePacket(pBuffer);
            }
            else
            {
                if (pBuffer.IsEncrypted)
                {
                    byte[] packetData = pBuffer.Data;

                    lock (_recvAES)
                    {
                        _recvAES.Transform(packetData);
                    }
                    if (Program.usingMapleCrypto)
                    {
                        MapleCustom.Decrypt(packetData);
                    }

                    if (OnPacketRecvHandler != null)
                    {
                        PacketEventArgs eventArgs = new PacketEventArgs(packetData);
                        OnPacketRecvHandler(this, eventArgs);
                    }

                    pBuffer.Reset(true, 4);
                    ReceivePacket(pBuffer);
                }
                else
                {
                    using (PacketReader pPacket = new PacketReader(pBuffer.Data))
                    {
                        ushort Version    = pPacket.ReadUShort();
                        string Subversion = pPacket.ReadString();

                        byte[] SIV = pPacket.ReadBytes(4);
                        byte[] RIV = pPacket.ReadBytes(4);

                        _sendAES = new MapleCrypt(Version, SIV, false);
                        _recvAES = new MapleCrypt(Version, RIV, false);

                        byte Locale = pPacket.Read();

                        if (OnHandshakeHandler != null)
                        {
                            HandshakeEventArgs eventArgs = new HandshakeEventArgs(Version, Subversion, SIV, RIV, Locale);
                            OnHandshakeHandler(this, eventArgs);
                        }

                        pBuffer.Dispose();
                        WaitForPacket();
                    }
                }
            }
        }