本文整理汇总了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);
}
示例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;
}
示例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);
}
}
}
示例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;
}
}
示例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);
}
示例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);
}
}
}
示例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();
}