当前位置: 首页>>代码示例>>C#>>正文


C# IPacket.WritePacket方法代码示例

本文整理汇总了C#中IPacket.WritePacket方法的典型用法代码示例。如果您正苦于以下问题:C# IPacket.WritePacket方法的具体用法?C# IPacket.WritePacket怎么用?C# IPacket.WritePacket使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在IPacket的用法示例。


在下文中一共展示了IPacket.WritePacket方法的7个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: Log

        private static MinecraftStream MinecraftStream { get; set; } // Used for getting raw packet data
        public static void Log(IPacket packet, bool clientToServer)
        {
            return; //Clogs up the console with packets
            var type = packet.GetType(); 
            var fields = type.GetFields();
            var builder = new StringBuilder();
            // Log time, direction, name
            builder.Append(DateTime.Now.ToString("{hh:mm:ss.fff} "));
            if (clientToServer)
                builder.Append("[CLIENT->SERVER] ");
            else
                builder.Append("[SERVER->CLIENT] ");
            builder.Append(FormatPacketName(type.Name));
            builder.Append(" (0x"); builder.Append(packet.Id.ToString("X2")); builder.Append(")");
            builder.AppendLine();

            // Log raw data
            MemoryStream.Seek(0, SeekOrigin.Begin);
            MemoryStream.SetLength(0);
            packet.WritePacket(MinecraftStream);
            builder.Append(DumpArrayPretty(MemoryStream.GetBuffer().Take((int)MemoryStream.Length).ToArray()));

            // Log fields
            foreach (var field in fields)
            {
                if (field.IsStatic)
                    continue;
                var name = field.Name;
                name = AddSpaces(name);
                var fValue = field.GetValue(packet);

                if (!(fValue is Array))
                    builder.Append(string.Format(" {0} ({1})", name, field.FieldType.Name));
                else
                {
                    var array = fValue as Array;
                    builder.Append(string.Format(" {0} ({1}[{2}])", name,
                        array.GetType().GetElementType().Name, array.Length));
                }

                if (fValue is byte[])
                    builder.Append(": " + DumpArray(fValue as byte[]) + "\n");
                else if (fValue is Array)
                {
                    builder.Append(": ");
                    var array = fValue as Array;
                    foreach (var item in array)
                        builder.Append(string.Format("{0}, ", item.ToString()));
                    builder.AppendLine();
                }
                else
                    builder.Append(": " + fValue + "\n");
            }
            Log(builder.ToString(), LogImportance.Low);
        }
开发者ID:cpancake,项目名称:Craft.Net,代码行数:56,代码来源:LogProvider.cs

示例2: WriteFunction

        private IAsyncResult WriteFunction(IPacket packet)
        {
            using (var ms = new MemoryStream())
            using (var stream = new MinecraftStream(ms, Mode))
            {
                packet.WritePacket(stream);
                var data = ms.ToArray();

                switch (Mode)
                {
                    case NetworkMode.Modern:
                        if (ModernCompressionEnabled)
                            return BeginWriteWithCompression(data, null, null);
                        else
                            return BeginWriteWithoutCompression(data, null, null);

                    case NetworkMode.Classic:
                        return BeginWriteClassic(data, null, null);
                }
            }

            return null;
        }
开发者ID:beppe9000,项目名称:MineLib.Network,代码行数:23,代码来源:MinecraftStream.cs

示例3: QueuePacket

        public void QueuePacket(IPacket packet)
        {
            if (Disconnected || (Connection != null && !Connection.Connected))
                return;

            using (MemoryStream writeStream = new MemoryStream())
            {
                using (MinecraftStream ms = new MinecraftStream(writeStream))
                {
                    writeStream.WriteByte(packet.ID);
                    packet.WritePacket(ms);
                }

                byte[] buffer = writeStream.ToArray();

                SocketAsyncEventArgs args = new SocketAsyncEventArgs();
                args.UserToken = packet;
                args.Completed += OperationCompleted;
                args.SetBuffer(buffer, 0, buffer.Length);

                if (Connection != null)
                {
                    if (!Connection.SendAsync(args))
                        OperationCompleted(this, args);
                }
            }
        }
开发者ID:Zoxive,项目名称:TrueCraft,代码行数:27,代码来源:RemoteClient.cs

示例4: WritePacket

 public void WritePacket(IPacket packet, PacketDirection direction)
 {
     lock (streamLock)
     {
         var newNetworkMode = packet.WritePacket(MinecraftStream, NetworkMode, direction);
         BufferedStream.WriteImmediately = true;
         int id = -1;
         var type = packet.GetType();
         // Find packet ID for this type
         for (int i = 0; i < NetworkModes[(int)NetworkMode].LongLength; i++)
         {
             if (NetworkModes[(int)NetworkMode][i][(int)direction] == type)
             {
                 id = i;
                 break;
             }
         }
         if (id == -1)
             throw new InvalidOperationException("Attempted to write invalid packet type.");
         MinecraftStream.WriteVarInt((int)BufferedStream.PendingWrites + MinecraftStream.GetVarIntLength(id));
         MinecraftStream.WriteVarInt(id);
         BufferedStream.WriteImmediately = false;
         BufferedStream.Flush();
         NetworkMode = newNetworkMode;
     }
 }
开发者ID:Booser,项目名称:Craft.Net,代码行数:26,代码来源:NetworkManager.cs

示例5: WritePacket

 public void WritePacket(IPacket packet)
 {
     /* Writes packet to a buffer,
      * then sends the buffer to the socket
      */
     MemoryStream tempStream = new MemoryStream();
     packet.WritePacket(new PacketWriter(tempStream));
     byte[] buffer = new byte[tempStream.Length];
     tempStream.Read(buffer, 0, buffer.Length);
     buffer = (buffer.Skip(HeaderSize).ToArray());
     CoCCrypto.Encrypt(buffer);
     CoCStream.Write(buffer, 0, buffer.Length);
 }
开发者ID:jibinthomas007,项目名称:CoCSharp,代码行数:13,代码来源:ProxyNetworkManager.cs

示例6: SendPacket

        /// <summary>
        /// Sends the specified packet to the socket asynchronously.
        /// </summary>
        /// <param name="packet">The <see cref="IPacket"/> that will be sent.</param>
        /// <exception cref="System.ArgumentNullException"/>
        public void SendPacket(IPacket packet)
        {
            if (packet == null)
                throw new ArgumentNullException("packet");

            using (var dePacketWriter = new PacketWriter(new MemoryStream()))
            {
                packet.WritePacket(dePacketWriter);
                var body = ((MemoryStream)dePacketWriter.BaseStream).ToArray();
                CoCCrypto.Encrypt(body);

                if (packet is UpdateKeyPacket)
                    UpdateCiphers(Seed, ((UpdateKeyPacket)packet).Key); // handle update key packet

                using (var enPacketWriter = new PacketWriter(new MemoryStream())) // write header
                {
                    enPacketWriter.WriteUInt16(packet.ID);
                    enPacketWriter.WriteInt24(body.Length);
                    enPacketWriter.WriteUInt16(0); // the unknown or the packet version
                    enPacketWriter.Write(body, 0, body.Length);

                    var rawPacket = ((MemoryStream)enPacketWriter.BaseStream).ToArray();

                    // should avoid new objs
                    var args = new SocketAsyncEventArgs();
                    args.SetBuffer(rawPacket, 0, rawPacket.Length);
                    if (!Connection.SendAsync(args))
                        AsyncOperationCompleted(Connection, args);
                }
            }
        }
开发者ID:castelbuilder123,项目名称:CoCSharp,代码行数:36,代码来源:NetworkManagerAsync.cs

示例7: LogPacket

        public virtual void LogPacket(IPacket packet, bool clientToServer)
        {
            if (clientToServer && !Settings.LogClient)      return;
            if (!clientToServer && !Settings.LogServer)     return;
            if (!Settings.PacketFilter.Contains(packet.Id)) return;
            var type = packet.GetType();
            var fields = type.GetFields();
            // Log time, direction, name
            Stream.Write(DateTime.Now.ToString("{hh:mm:ss.fff} "));
            if (clientToServer)
                Stream.Write("[CLIENT->SERVER] ");
            else
                Stream.Write("[SERVER->CLIENT] ");
            Stream.Write(FormatPacketName(type.Name));
            Stream.Write(" (0x"); Stream.Write(packet.Id.ToString("X2")); Stream.Write(")");
            Stream.WriteLine();

            // Log raw data
            MemoryStream.Seek(0, SeekOrigin.Begin);
            MemoryStream.SetLength(0);
            packet.WritePacket(MinecraftStream);
            Stream.Write(DumpArrayPretty(MemoryStream.GetBuffer().Take((int)MemoryStream.Length).ToArray()));

            // Log fields
            foreach (var field in fields)
            {
                var name = field.Name;
                if (field.Name == "PacketId")
                    continue;
                name = AddSpaces(name);
                var fValue = field.GetValue(packet);

                if (!(fValue is Array))
                    Stream.Write(string.Format(" {0} ({1})", name, field.FieldType.Name));
                else
                {
                    var array = fValue as Array;
                    Stream.Write(string.Format(" {0} ({1}[{2}])", name,
                        array.GetType().GetElementType().Name, array.Length));
                }

                if (fValue is byte[])
                    Stream.Write(": " + DumpArray(fValue as byte[]) + "\n");
                else if (fValue is Array)
                {
                    Stream.Write(": ");
                    var array = fValue as Array;
                    foreach (var item in array)
                        Stream.Write(string.Format("{0}, ", item.ToString()));
                    Stream.WriteLine();
                }
                else if (fValue is string)
                    Stream.Write(": \"" + fValue + "\"\n");
                else
                    Stream.Write(": " + fValue + "\n");
            }
            Stream.WriteLine();
            Stream.Flush();
        }
开发者ID:SirCmpwn,项目名称:SMProxy,代码行数:59,代码来源:Log.cs


注:本文中的IPacket.WritePacket方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。