コード例 #1
0
ファイル: Client.cs プロジェクト: Algorithman/TestCellAO
        /// <summary>
        /// </summary>
        /// <param name="messageBody">
        /// </param>
        public void SendInitiateCompressionMessage(MessageBody messageBody)
        {
            // TODO: Investigate if reciever is a timestamp
            Contract.Requires(messageBody != null);
            var message = new Message
            {
                Body   = messageBody,
                Header =
                    new Header
                {
                    MessageId  = 0xdfdf,
                    PacketType = messageBody.PacketType,
                    Unknown    = 0x0001,
                    Sender     = 0x03000000,
                    Receiver   = this.character.Identity.Instance
                }
            };

            byte[] buffer = this.messageSerializer.Serialize(message);

#if DEBUG
            Console.ForegroundColor = ConsoleColor.Green;
            Console.WriteLine(NiceHexOutput.Output(buffer));
            Console.ResetColor();
            LogUtil.Debug(NiceHexOutput.Output(buffer));
#endif
            this.packetNumber = 1;

            this.Send(buffer);
        }
コード例 #2
0
        /// <summary>
        /// </summary>
        /// <param name="receiver">
        /// </param>
        /// <param name="messageBody">
        /// </param>
        public void Send(int receiver, MessageBody messageBody)
        {
            // TODO: Investigate if reciever is a timestamp
            var message = new Message
            {
                Body   = messageBody,
                Header =
                    new Header
                {
                    MessageId  = BitConverter.ToUInt16(new byte[] { 0xDF, 0xDF }, 0),
                    PacketType = messageBody.PacketType,
                    Unknown    = 0x0001,
                    Sender     = 0x00000001,
                    Receiver   = receiver
                }
            };

            byte[] buffer = this.messageSerializer.Serialize(message);

            buffer[0] = BitConverter.GetBytes(this.packetNumber)[0];
            buffer[1] = BitConverter.GetBytes(this.packetNumber)[1];
            this.packetNumber++;

            if (Program.DebugNetwork)
            {
                LogUtil.Debug("Sent:\r\n" + NiceHexOutput.Output(buffer));
            }

            if (buffer.Length % 4 > 0)
            {
                Array.Resize(ref buffer, buffer.Length + (4 - (buffer.Length % 4)));
            }

            this.Send(buffer);
        }
コード例 #3
0
        /// <summary>
        /// </summary>
        /// <param name="buffer">
        /// </param>
        public void SendCompressed(byte[] buffer)
        {
            // We can not be multithreaded here. packet numbers would be jumbled
            lock (this.netStream)
            {
                // Discard the packet for now, if we can not write to the stream
                if (this.netStream.CanWrite)
                {
                    byte[] pn = BitConverter.GetBytes(this.packetNumber++);
                    buffer[0] = pn[1];
                    buffer[1] = pn[0];

                    try
                    {
                        this.zStream.Write(buffer, 0, buffer.Length);
                        this.zStream.Flush();
                    }
                    catch (Exception e)
                    {
                        LogUtil.Debug("Error writing to zStream");
                        LogUtil.ErrorException(e);
                        this.server.DisconnectClient(this);
                    }
                }
            }

            if (Program.DebugNetwork)
            {
                LogUtil.Debug(NiceHexOutput.Output(buffer));
            }
        }
コード例 #4
0
        /// <summary>
        /// </summary>
        /// <param name="buffer">
        /// </param>
        /// <returns>
        /// </returns>
        protected bool OnReceive(BufferSegment buffer)
        {
            // return false, if header cannot be complete (00FF55AA, <Length of packet>)

            // Loop if more than one packet frame received
            while (true)
            {
                if (this._remainingLength == 0)
                {
                    return(true);
                }

                if (this._remainingLength < 8)
                {
                    return(false);
                }

                int expectedLength = this.CheckData(buffer);
                if (expectedLength == -1)
                {
                    // MALFORMED PACKET RECEIVED !!!
                    LogUtil.Debug("Malformed packet received: ");
                    byte[] data = new byte[this._remainingLength];
                    buffer.SegmentData.CopyTo(data, this._remainingLength);
                    LogUtil.Debug(NiceHexOutput.Output(data));
                    this._remainingLength = 0;
                    this._offset          = 0;

                    // Lets clear the buffer and try this again, no need to drop the connection
                    return(true);
                }

                if (expectedLength + 8 > this._remainingLength)
                {
                    return(false);
                }

                if (this._remainingLength >= expectedLength + 8)
                {
                    // Handle packet payload here

                    byte[] dataBytes = new byte[expectedLength];
                    Array.Copy(buffer.SegmentData, 8 + this._offset, dataBytes, 0, expectedLength);

                    this.DataReceived(dataBytes);
                }

                if (expectedLength + 8 <= this._remainingLength)
                {
                    // If we have received a full packet frame
                    // then move the remaining data to a new buffer (with offset 0)
                    // only adjusting offset and length here
                    // Then do the whole thing again
                    this._remainingLength -= expectedLength + 8;
                    this._offset          += expectedLength + 8;
                }
            }
        }
コード例 #5
0
        /// <summary>
        /// </summary>
        /// <param name="messageBody">
        /// </param>
        public void SendInitiateCompressionMessage(MessageBody messageBody)
        {
            // TODO: Investigate if reciever is a timestamp
            var message = new Message
            {
                Body   = messageBody,
                Header =
                    new Header
                {
                    MessageId  = 0xdfdf,
                    PacketType = messageBody.PacketType,
                    Unknown    = 0x0001,

                    // TODO: Make compression choosable in config.xml
                    Sender = 0x01000000,

                    // 01000000 = uncompressed, 03000000 = compressed
                    Receiver = 0                   // this.character.Identity.Instance
                }
            };

            byte[] buffer = this.messageSerializer.Serialize(message);

            if (Program.DebugNetwork)
            {
                LogUtil.Debug(NiceHexOutput.Output(buffer));
            }

            this.packetNumber = 1;

            this.Send(buffer);

            // Now create the compressed stream
            try
            {
                if (!this.zStreamSetup)
                {
                    // CreateIM the zStream
                    this.netStream         = new NetworkStream(this.TcpSocket);
                    this.zStream           = new ZOutputStream(this.netStream, zlibConst.Z_BEST_SPEED);
                    this.zStream.FlushMode = zlibConst.Z_SYNC_FLUSH;
                    this.zStreamSetup      = true;
                }
            }
            catch (Exception e)
            {
                LogUtil.ErrorException(e);
            }
        }
コード例 #6
0
        /// <summary>
        /// </summary>
        /// <param name="buffer">
        /// </param>
        /// <returns>
        /// </returns>
        protected override bool OnReceive(BufferSegment buffer)
        {
            Message message = null;

            var packet = new byte[this._remainingLength];

            Array.Copy(buffer.SegmentData, packet, this._remainingLength);

            if (Program.DebugNetwork)
            {
                LogUtil.Debug("Offset: " + buffer.Offset.ToString() + " -- RemainingLength: " + this._remainingLength);
                LogUtil.Debug(NiceHexOutput.Output(packet));
            }

            this._remainingLength = 0;
            try
            {
                message = this.messageSerializer.Deserialize(packet);
            }
            catch (Exception)
            {
                uint messageNumber = this.GetMessageNumber(packet);
                this.Server.Warning(
                    this,
                    "Client sent malformed message {0}",
                    messageNumber.ToString(CultureInfo.InvariantCulture));
                return(false);
            }

            buffer.IncrementUsage();

            if (message == null)
            {
                uint messageNumber = this.GetMessageNumber(packet);
                this.Server.Warning(
                    this,
                    "Client sent unknown message {0}",
                    messageNumber.ToString(CultureInfo.InvariantCulture));
                return(false);
            }

            this.bus.Publish(new MessageReceivedEvent(this, message));

            return(true);
        }
コード例 #7
0
        /// <summary>
        /// </summary>
        /// <param name="receiver">
        /// </param>
        /// <param name="packetBytes">
        /// </param>
        public void Send(int receiver, byte[] packetBytes)
        {
            packetBytes[0] = BitConverter.GetBytes(this.packetNumber)[0];
            packetBytes[1] = BitConverter.GetBytes(this.packetNumber)[1];
            this.packetNumber++;

            if (Program.DebugNetwork)
            {
                LogUtil.Debug("\r\nSent:\r\n" + NiceHexOutput.Output(packetBytes));
            }

            if (packetBytes.Length % 4 > 0)
            {
                Array.Resize(ref packetBytes, packetBytes.Length + (4 - (packetBytes.Length % 4)));
            }

            this.Send(packetBytes);
        }
コード例 #8
0
ファイル: Client.cs プロジェクト: Algorithman/TestCellAO
        /// <summary>
        /// </summary>
        /// <param name="packet">
        /// </param>
        /// <exception cref="NotImplementedException">
        /// </exception>
        public void SendCompressed(byte[] packet)
        {
#if DEBUG
            Console.ForegroundColor = ConsoleColor.Green;
            Console.WriteLine(NiceHexOutput.Output(packet));
            Console.ResetColor();
            LogUtil.Debug("Sending Compressed:\r\n" + NiceHexOutput.Output(packet));
#endif
            Contract.Requires(packet != null);
            Contract.Requires(1 < packet.Length);
            int  tries = 0;
            bool done  = false;

            // 18.1 Fix
            byte[] pn = BitConverter.GetBytes(this.packetNumber);
            packet[0] = pn[1];
            packet[1] = pn[0];
            this.packetNumber++;
            while ((!done) && (tries < 3))
            {
                try
                {
                    done = true;
                    if (!this.zStreamSetup)
                    {
                        // Create the zStream
                        this.netStream         = new NetworkStream(this.TcpSocket);
                        this.zStream           = new ZOutputStream(this.netStream, zlibConst.Z_BEST_COMPRESSION);
                        this.zStream.FlushMode = zlibConst.Z_SYNC_FLUSH;
                        this.zStreamSetup      = true;
                    }

                    this.zStream.Write(packet, 0, packet.Length);
                    this.zStream.Flush();
                }
                catch (Exception)
                {
                    tries++;
                    done = false;
                    this.Server.DisconnectClient(this);
                    return;
                }
            }
        }
コード例 #9
0
        /// <summary>
        /// </summary>
        /// <param name="buffer">
        /// </param>
        /// <returns>
        /// </returns>
        protected override bool OnReceive(BufferSegment buffer)
        {
            if (this._remainingLength > 4)
            {
                byte[] packet = new byte[this._remainingLength];
                Array.Copy(buffer.SegmentData, 0, packet, 0, this._remainingLength);
                if (Program.DebugNetwork)
                {
                    LogUtil.Debug("\r\nReceived:\r\n" + NiceHexOutput.Output(packet));
                }

                ushort messageNumber = this.GetMessageNumber(packet);
                Parser parser        = new Parser();
                return(parser.Parse(this, packet, messageNumber));
            }

            // TODO: check what needs to be done if no suitable packet was found
            return(true);
        }
コード例 #10
0
        /// <summary>
        /// The read.
        /// </summary>
        /// <param name="client">
        /// </param>
        /// <param name="packet">
        /// </param>
        public static void Read(Client client, byte[] packet)
        {
            if (Program.DebugNetwork)
            {
                LogUtil.Debug("\r\nReceived:\r\n" + NiceHexOutput.Output(packet));
            }

            MemoryStream m_stream = new MemoryStream(packet);
            BinaryReader m_reader = new BinaryReader(m_stream);

            // now we should do password check and then send OK or Error
            // sending OK now
            m_stream.Position = 8;

            short  userNameLength = IPAddress.NetworkToHostOrder(m_reader.ReadInt16());
            string userName       = Encoding.ASCII.GetString(m_reader.ReadBytes(userNameLength));
            short  loginKeyLength = IPAddress.NetworkToHostOrder(m_reader.ReadInt16());
            string loginKey       = Encoding.ASCII.GetString(m_reader.ReadBytes(loginKeyLength));

            LoginEncryption loginEncryption = new LoginEncryption();

            if (loginEncryption.IsValidLogin(loginKey, client.ServerSalt, userName))
            {
                client.IsBot = true;
                byte[] chars = AccountCharacterList.Create(userName);
                if (Program.DebugNetwork)
                {
                    LogUtil.Debug("\r\nReceived:\r\n" + NiceHexOutput.Output(chars));
                }

                client.Send(chars);
            }
            else
            {
                byte[] loginerr = LoginError.Create();
                client.Send(loginerr);
                client.Server.DisconnectClient(client);
            }
        }
コード例 #11
0
        /// <summary>
        /// </summary>
        /// <param name="receiver">
        /// </param>
        /// <param name="messageBody">
        /// </param>
        public void Send(int receiver, MessageBody messageBody)
        {
            // TODO: Investigate if reciever is a timestamp
            var message = new Message
            {
                Body   = messageBody,
                Header =
                    new Header
                {
                    MessageId  = BitConverter.ToUInt16(new byte[] { 0xDF, 0xDF }, 0),
                    PacketType = messageBody.PacketType,
                    Unknown    = 0x0001,
                    Sender     = 0x00000001,
                    Receiver   = receiver
                }
            };

            byte[] buffer = this.messageSerializer.Serialize(message);

            buffer[0] = BitConverter.GetBytes(this.packetNumber)[0];
            buffer[1] = BitConverter.GetBytes(this.packetNumber)[1];
            this.packetNumber++;

#if DEBUG
            Console.ForegroundColor = ConsoleColor.Green;
            Console.WriteLine(NiceHexOutput.Output(buffer));
            Console.ResetColor();
            LogUtil.Debug("Sent:\r\n" + NiceHexOutput.Output(buffer));
#endif
            if (buffer.Length % 4 > 0)
            {
                Array.Resize(ref buffer, buffer.Length + (4 - (buffer.Length % 4)));
            }

            this.Send(buffer);
        }
コード例 #12
0
        /// <summary>
        /// The parse args.
        /// </summary>
        /// <param name="funcNum">
        /// The func num.
        /// </param>
        /// <param name="R">
        /// The r.
        /// </param>
        /// <returns>
        /// The <see cref="object[]"/>.
        /// </returns>
        /// <exception cref="IndexOutOfRangeException">
        /// </exception>
        private object[] ParseArgs(int funcNum, ref bool R)
        {
            bool flag = !this.FunctionSets.ContainsKey(funcNum.ToString());

            if (flag)
            {
                TextWriter lastitem = new StreamWriter("lastitem.txt");
                lastitem.WriteLine(NiceHexOutput.Output(this.br.Buffer));
                lastitem.Close();
                throw new IndexOutOfRangeException("Not handled function " + funcNum.ToString());
            }

            string[]      array = this.FunctionSets[funcNum.ToString()].Split(',');
            List <object> list  = new List <object>();

            string[] array2 = array;
            checked
            {
                for (int i = 0; i < array2.Length; i++)
                {
                    string str  = array2[i];
                    int    num  = int.Parse(str.Trim().Substring(0, str.Length - 1));
                    string text = str.Trim().ToLower().Substring(str.Length - 1, 1);

                    // Strings.LCase(Strings.Right(Strings.Trim(str), 1));
                    string left = text;
                    flag = left == "n";
                    if (flag)
                    {
                        int arg_A6_0 = 1;
                        int num2     = num;
                        int num3     = arg_A6_0;
                        while (true)
                        {
                            int arg_D9_0 = num3;
                            int num4     = num2;
                            if (arg_D9_0 > num4)
                            {
                                break;
                            }

                            int value = this.br.ReadInt32();
                            list.Add(value);
                            num3++;
                        }
                    }
                    else
                    {
                        flag = left == "h";
                        if (flag)
                        {
                            int arg_FD_0 = 1;
                            int num5     = num;
                            int num6     = arg_FD_0;
                            while (true)
                            {
                                int arg_12B_0 = num6;
                                int num4      = num5;
                                if (arg_12B_0 > num4)
                                {
                                    break;
                                }

                                string item = this.br.ReadHash();
                                list.Add(item);
                                num6++;
                            }
                        }
                        else
                        {
                            flag = left == "s";
                            if (flag)
                            {
                                int arg_14F_0 = 1;
                                int num7      = num;
                                int num8      = arg_14F_0;
                                while (true)
                                {
                                    int arg_1B5_0 = num8;
                                    int num4      = num7;
                                    if (arg_1B5_0 > num4)
                                    {
                                        break;
                                    }

                                    string item2 = string.Empty;
                                    int    num9  = this.br.ReadInt32() - 1;
                                    flag = num9 > 0;
                                    if (flag)
                                    {
                                        item2 = this.br.ReadString();
                                    }

                                    this.br.Skip(1);
                                    list.Add(item2);
                                    num8++;
                                }
                            }
                            else
                            {
                                flag = left == "x";
                                if (flag)
                                {
                                    this.br.Skip(num);
                                }
                                else
                                {
                                    R = true;
                                }
                            }
                        }
                    }
                }

                return(list.ToArray());
            }
        }