本文整理汇总了C#中IPacket.Write方法的典型用法代码示例。如果您正苦于以下问题:C# IPacket.Write方法的具体用法?C# IPacket.Write怎么用?C# IPacket.Write使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类IPacket
的用法示例。
在下文中一共展示了IPacket.Write方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: SendPacket
private bool SendPacket(IPacket packet, bool guaranteed, int revision, EventHandler<IPacket> handler = null)
{
if (IsDisposed)
throw new ObjectDisposedException(GetType().Name);
if (packet == null)
throw new ArgumentNullException(nameof(packet));
// wenn die Drohne nicht erreichbar ist
if (!IsConnected)
{
if (Config.IgnoreGuaranteedWhenOffline)
guaranteed = false;
// alle Pakete (außer Ping) ignorieren wenn die Drohne offline ist
if (packet.Type != PacketType.Ping && Config.IgnorePacketsWhenOffline)
return false;
}
lock (controlSocket)
{
bool alreadySent;
lock (packetsToAcknowledge)
{
alreadySent = packetsToAcknowledge.ContainsKey(revision);
if (guaranteed)
{
if (!stopwatch.IsRunning)
stopwatch.Start();
packetsToAcknowledge[revision] = packet;
packetSendTime[revision] = stopwatch.ElapsedMilliseconds;
if (handler != null)
packetAcknowlegdeEvents[revision] = handler;
}
}
packetBuffer.ResetPosition();
// Paket-Header schreiben
packetBuffer.Write((byte)'F');
packetBuffer.Write((byte)'L');
packetBuffer.Write((byte)'Y');
// Alle Daten werden nach dem Netzwerkstandard BIG-Endian übertragen
packetBuffer.Write(revision);
// wenn die Drohne eine Antwort schickt dann wird kein Ack-Paket angefordert, sonst kann es passieren, dass das Ack-Paket die eigentliche Antwort verdrängt
packetBuffer.Write(guaranteed && !packet.Type.DoesClusterAnswer());
packetBuffer.Write((byte)packet.Type);
// Paket Inhalt schreiben
packet.Write(packetBuffer);
controlSocket.BeginSend(packetStream.GetBuffer(), (int)packetBuffer.Position, SendPacket, null);
if (Config.VerbosePacketSending && (packet.Type != PacketType.Ping || Config.LogPingPacket))
Log.Verbose("[{0}] Packet: [{1}] {2}, size: {3} bytes {4} {5}", Address.ToString(), revision, packet.Type, packetBuffer.Position, guaranteed ? "(guaranteed)" : "", alreadySent ? "(resend)" : "");
}
return true;
}
示例2: SendPacket
public void SendPacket(IPacket packet)
{
packet.Write(Stream);
OnPacketSent(this, packet);
}
示例3: SendPacket
public void SendPacket(IPacket packet, Destination dest)
{
if (_isDisposed)
throw new ObjectDisposedException("this");
if (!IsAlive || (dest == Destination.Server && ServerPaused) || (dest == Destination.Client && ClientPaused))
{
_packetBacklog.Add(new PacketBacklogItem { Packet = packet, Destination = dest });
return;
}
//FlushPackets();
if (packet.Ignore)
return;
using (StarWriter writer = new StarWriter())
{
packet.Write(writer);
bool compressed = packet.AlwaysCompress || writer.BaseStream.Length > 8192;
byte[] buffer;
if (compressed)
{
writer.BaseStream.Seek(0, SeekOrigin.Begin);
buffer = ZlibUtils.Compress(writer.ToArray());
}
else
{
buffer = writer.ToArray();
}
int length = compressed ? -buffer.Length : buffer.Length;
byte[] lengthBuffer = VLQ.CreateSigned(length);
var buffers = new List<ArraySegment<byte>>();
buffers.Add(new ArraySegment<byte>(new[] { packet.PacketId }));
buffers.Add(new ArraySegment<byte>(lengthBuffer));
buffers.Add(new ArraySegment<byte>(buffer));
SocketAsyncEventArgs sendArgs = new SocketAsyncEventArgs();
sendArgs.UserToken = packet;
sendArgs.Completed += SocketOperationCompleted;
sendArgs.BufferList = buffers;
switch (dest)
{
case Destination.Server:
if (ServerSocket != null && !ServerSocket.SendAsync(sendArgs))
SocketOperationCompleted(this, sendArgs);
break;
case Destination.Client:
if (ClientSocket != null && !ClientSocket.SendAsync(sendArgs))
SocketOperationCompleted(this, sendArgs);
break;
default:
throw new InvalidOperationException("Invalid direction!");
}
}
}
示例4: SendPacket
private bool SendPacket(IPacket packet, bool guaranteed, int revision)
{
if (IsDisposed)
throw new ObjectDisposedException(GetType().Name);
if (packet == null)
throw new ArgumentNullException("packet");
if (Address == null)
return false;
// wenn das Cluster nicht erreichbar ist
if (Ping < 0)
{
if (Kugelmatik.Config.IgnoreGuaranteedWhenOffline)
guaranteed = false;
// alle Pakete (außer Ping) ignorieren wenn das Cluster offline ist
if (packet.Type != PacketType.Ping && Kugelmatik.Config.IgnorePacketsWhenOffline)
return false;
}
if (Kugelmatik.Config.IgnoreGuaranteed)
guaranteed = false;
lock (locker)
{
bool alreadySent = packetsToAcknowledge.ContainsKey(revision);
if (guaranteed)
{
if (!stopwatch.IsRunning)
stopwatch.Start();
packetsToAcknowledge[revision] = packet;
packetSendTime[revision] = stopwatch.ElapsedMilliseconds;
}
packetBuffer.Position = 0;
// Paket-Header schreiben
packetWriter.Write((byte)'K');
packetWriter.Write((byte)'K');
packetWriter.Write((byte)'S');
// wenn das Cluster eine Antwort schickt dann wird kein Ack-Paket angefordert, sonst kann es passieren, dass das Ack-Paket die eigentliche Antwort verdrängt
if (!packet.Type.DoesClusterAnswer() && guaranteed)
packetWriter.Write((byte)1);
else
packetWriter.Write((byte)0);
packetWriter.Write((byte)packet.Type);
packetWriter.Write(revision);
// Paket Inhalt schreiben
packet.Write(packetWriter);
socket.BeginSend(packetBuffer.GetBuffer(), (int)packetBuffer.Position, SendPacket, null);
if (Kugelmatik.Config.VerbosePacketSending && (packet.Type != PacketType.Ping || Kugelmatik.Config.LogPingPacket))
Log.Verbose("[{0}, {1}] Packet: [{2}] {3}, size: {4} bytes {5} {6}", X, Y, revision, packet.Type, packetBuffer.Position, guaranteed ? "(guaranteed)" : "", alreadySent ? "(resend)" : "");
}
return true;
}