本文整理汇总了C#中Packet.ToBytes方法的典型用法代码示例。如果您正苦于以下问题:C# Packet.ToBytes方法的具体用法?C# Packet.ToBytes怎么用?C# Packet.ToBytes使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Packet
的用法示例。
在下文中一共展示了Packet.ToBytes方法的11个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: SendPacket
// SendPacket: send a packet from the sim to the client via our fake sim endpoint
public void SendPacket(Packet packet, bool skipZero)
{
byte[] buffer = packet.ToBytes();
if (skipZero || (packet.Header.Data[0] & Helpers.MSG_ZEROCODED) == 0)
socket.SendTo(buffer, buffer.Length, SocketFlags.None, clientEndPoint);
else
{
int zeroLength = Helpers.ZeroEncode(buffer, buffer.Length, zeroBuffer);
socket.SendTo(zeroBuffer, zeroLength, SocketFlags.None, clientEndPoint);
}
}
示例2: Inject
// Inject: inject a packet
public void Inject(Packet packet, Direction direction)
{
if (direction == Direction.Incoming)
{
if (firstReceive)
{
proxy.queuedIncomingInjections.Add(packet);
return;
}
incomingInjections.Add(++incomingSequence);
packet.Header.Sequence = incomingSequence;
}
else
{
outgoingInjections.Add(++outgoingSequence);
packet.Header.Sequence = outgoingSequence;
}
#if DEBUG_SEQUENCE
Console.WriteLine("INJECT " + (direction == Direction.Incoming ? "<-" : "->") + " " + packet.Type + " #" + packet.Header.Sequence);
#endif
if ((packet.Header.Data[0] & Helpers.MSG_RELIABLE) != 0)
WaitForAck(packet, direction);
if (direction == Direction.Incoming)
{
byte[] buffer = packet.ToBytes();
if ((packet.Header.Data[0] & Helpers.MSG_ZEROCODED) == 0)
socket.SendTo(buffer, buffer.Length, SocketFlags.None, clientEndPoint);
else
{
int zeroLength = Helpers.ZeroEncode(buffer, buffer.Length, zeroBuffer);
socket.SendTo(zeroBuffer, zeroLength, SocketFlags.None, clientEndPoint);
}
}
else
proxy.SendPacket(packet, remoteEndPoint, false);
}
示例3: DataManager
private void DataManager(Packet p)
{
switch (p.packetType)
{
case PacketType.getNews:
Debug.Log("Got News!");
string news = "";
foreach (string values in p.data)
{
news += values;
}
MainMenu.news = news;
break;
case PacketType.Registration:
ID = p.data[0];
Packet packet = new Packet(PacketType.Registration, ID);
packet.data.Add(login);
packet.data.Add("Enter to Chat");
socket.Send(packet.ToBytes());
break;
case PacketType.Chat:
Debug.Log(p.data[0]);
break;
case PacketType.CloseConnection:
Debug.Log(p.data[1] + "||" + p.data[0]);
break;
}
}
示例4: send
public void send(Packet p)
{
socket.Send(p.ToBytes());
}
示例5: ProcessOutPacket
protected virtual void ProcessOutPacket(Packet Pack)
{
// Keep track of when this packet was sent out
Pack.TickCount = Environment.TickCount;
if (!Pack.Header.Resent)
{
// Set the sequence number
lock (SequenceLock)
{
if (Sequence >= MAX_SEQUENCE)
Sequence = 1;
else
Sequence++;
Pack.Header.Sequence = Sequence;
}
if (Pack.Header.Reliable) //DIRTY HACK
{
lock (NeedAck)
{
if (!NeedAck.ContainsKey(Pack.Header.Sequence))
{
try
{
NeedAck.Add(Pack.Header.Sequence, Pack);
}
catch (Exception e) // HACKY
{
e.ToString();
// Ignore
// Seems to throw a exception here occasionally
// of 'duplicate key' despite being locked.
// !?!?!?
}
}
else
{
// Client.Log("Attempted to add a duplicate sequence number (" +
// packet.Header.Sequence + ") to the NeedAck dictionary for packet type " +
// packet.Type.ToString(), Helpers.LogLevel.Warning);
}
}
// Don't append ACKs to resent packets, in case that's what was causing the
// delivery to fail
if (!Pack.Header.Resent)
{
// Append any ACKs that need to be sent out to this packet
lock (PendingAcks)
{
if (PendingAcks.Count > 0 && PendingAcks.Count < MAX_APPENDED_ACKS &&
Pack.Type != PacketType.PacketAck &&
Pack.Type != PacketType.LogoutRequest)
{
Pack.Header.AckList = new uint[PendingAcks.Count];
int i = 0;
foreach (uint ack in PendingAcks.Values)
{
Pack.Header.AckList[i] = ack;
i++;
}
PendingAcks.Clear();
Pack.Header.AppendedAcks = true;
}
}
}
}
}
byte[] ZeroOutBuffer = new byte[4096];
byte[] sendbuffer;
sendbuffer = Pack.ToBytes();
try
{
if (Pack.Header.Zerocoded)
{
int packetsize = Helpers.ZeroEncode(sendbuffer, sendbuffer.Length, ZeroOutBuffer);
m_application.SendPacketTo(ZeroOutBuffer, packetsize, SocketFlags.None, CircuitCode);//userEP);
}
else
{
m_application.SendPacketTo(sendbuffer, sendbuffer.Length, SocketFlags.None, CircuitCode); //userEP);
}
}
catch (Exception)
{
OpenSim.Framework.Console.MainConsole.Instance.WriteLine("OpenSimClient.cs:ProcessOutPacket() - WARNING: Socket exception occurred on connection " + userEP.ToString() + " - killing thread");
ClientThread.Abort();
}
}
示例6: SendPacket
/// <summary>
///
/// </summary>
/// <param name="packet"></param>
/// <param name="incrementSequence"></param>
public void SendPacket(Packet packet, bool incrementSequence)
{
byte[] buffer;
int bytes;
if (!connected && packet.Type != PacketType.UseCircuitCode)
{
Client.Log("Trying to send a " + packet.Type.ToString() + " packet when the socket is closed",
Helpers.LogLevel.Warning);
throw new NotConnectedException();
}
if (incrementSequence)
{
// Set the sequence number here since we are manually serializing the packet
packet.Header.Sequence = ++Sequence;
if (packet.Header.Reliable)
{
// Keep track of when this packet was sent out
packet.TickCount = Environment.TickCount;
lock (NeedAck)
{
if (!NeedAck.ContainsKey(packet.Header.Sequence))
{
NeedAck.Add(packet.Header.Sequence, packet);
}
else
{
Client.Log("Attempted to add a duplicate sequence number (" +
packet.Header.Sequence + ") to the NeedAck dictionary for packet type " +
packet.Type.ToString(), Helpers.LogLevel.Warning);
}
}
// Append any ACKs that need to be sent out to this packet
lock (PendingAcks)
{
if (PendingAcks.Count > 0 && packet.Type != PacketType.PacketAck &&
packet.Type != PacketType.LogoutRequest)
{
packet.Header.AckList = new uint[PendingAcks.Count];
int i = 0;
foreach (uint ack in PendingAcks)
{
packet.Header.AckList[i] = ack;
i++;
}
PendingAcks.Clear();
packet.Header.AppendedAcks = true;
}
}
}
}
// Serialize the packet
buffer = packet.ToBytes();
bytes = buffer.Length;
// Zerocode if needed
if (packet.Header.Zerocoded)
{
byte[] zeroBuffer = new byte[4096];
bytes = Helpers.ZeroEncode(buffer, bytes, zeroBuffer);
buffer = zeroBuffer;
}
try
{
Connection.Send(buffer, bytes, SocketFlags.None);
}
catch (SocketException e)
{
Client.Log(e.ToString(), Helpers.LogLevel.Error);
}
}
示例7: SendPacket
/// <summary>
/// Sends a packet
/// </summary>
/// <param name="packet">Packet to be sent</param>
/// <param name="incrementSequence">Increment sequence number?</param>
public void SendPacket(Packet packet, bool incrementSequence)
{
byte[] buffer;
int bytes;
// Keep track of when this packet was sent out
packet.TickCount = Environment.TickCount;
if (incrementSequence)
{
// Set the sequence number
lock (SequenceLock)
{
if (Sequence > Settings.MAX_SEQUENCE)
Sequence = 1;
else
Sequence++;
packet.Header.Sequence = Sequence;
}
// Scrub any appended ACKs since all of the ACK handling is done here
if (packet.Header.AckList.Length > 0)
packet.Header.AckList = new uint[0];
packet.Header.AppendedAcks = false;
if (packet.Header.Reliable)
{
lock (NeedAck)
{
if (!NeedAck.ContainsKey(packet.Header.Sequence))
NeedAck.Add(packet.Header.Sequence, packet);
else
Client.Log("Attempted to add a duplicate sequence number (" +
packet.Header.Sequence + ") to the NeedAck dictionary for packet type " +
packet.Type.ToString(), Helpers.LogLevel.Warning);
}
// Don't append ACKs to resent packets, in case that's what was causing the
// delivery to fail
if (!packet.Header.Resent)
{
// Append any ACKs that need to be sent out to this packet
lock (PendingAcks)
{
if (PendingAcks.Count > 0 && PendingAcks.Count < Client.Settings.MAX_APPENDED_ACKS &&
packet.Type != PacketType.PacketAck &&
packet.Type != PacketType.LogoutRequest)
{
packet.Header.AckList = new uint[PendingAcks.Count];
for (int i = 0; i < PendingAcks.Count; i++)
packet.Header.AckList[i] = PendingAcks.Values[i];
PendingAcks.Clear();
packet.Header.AppendedAcks = true;
}
}
}
}
}
// Serialize the packet
buffer = packet.ToBytes();
bytes = buffer.Length;
Stats.SentBytes += (ulong)bytes;
Stats.SentPackets++;
UDPPacketBuffer buf;
// Zerocode if needed
if (packet.Header.Zerocoded)
{
buf = new UDPPacketBuffer(ipEndPoint, true, false);
bytes = Helpers.ZeroEncode(buffer, bytes, buf.Data);
buf.DataLength = bytes;
}
else
{
buf = new UDPPacketBuffer(ipEndPoint, false, false);
buf.Data = buffer;
buf.DataLength = bytes;
}
AsyncBeginSend(buf);
}
示例8: SendPacket
/// <summary>
/// Sends a packet
/// </summary>
/// <param name="packet">Packet to be sent</param>
/// <param name="incrementSequence">Increment sequence number?</param>
///
public void SendPacket(Packet packet, bool incrementSequence)
{
byte[] buffer;
int bytes;
// Keep track of when this packet was sent out
packet.TickCount = Environment.TickCount;
if (incrementSequence)
{
// Set the sequence number
lock (SequenceLock)
{
if (Sequence > Settings.MAX_SEQUENCE)
Sequence = 1;
else
Sequence++;
packet.Header.Sequence = Sequence;
}
if (packet.Header.Reliable)
{
// Add this packet to the list of ACK responses we are waiting on from the server
lock (NeedAck)
{
if (!NeedAck.ContainsKey(packet.Header.Sequence))
NeedAck.Add(packet.Header.Sequence, packet);
else
Logger.Log("Attempted to add a duplicate sequence number (" +
packet.Header.Sequence + ") to the NeedAck dictionary for packet type " +
packet.Type.ToString(), Helpers.LogLevel.Warning, Client);
}
if (packet.Header.Resent)
{
// This packet has already been sent out once, strip any appended ACKs
// off it and reinsert them into the outgoing ACK queue under the
// assumption that this packet will continually be rejected from the
// server or that the appended ACKs are possibly making the delivery fail
if (packet.Header.AckList.Length > 0)
{
Logger.DebugLog(String.Format("Purging ACKs from packet #{0} ({1}) which will be resent.",
packet.Header.Sequence, packet.GetType()));
lock (PendingAcks)
{
foreach (uint sequence in packet.Header.AckList)
{
if (!PendingAcks.ContainsKey(sequence))
PendingAcks[sequence] = sequence;
}
}
packet.Header.AppendedAcks = false;
packet.Header.AckList = new uint[0];
}
}
else
{
// This packet is not a resend, check if the conditions are favorable
// to ACK appending
if (packet.Type != PacketType.PacketAck &&
packet.Type != PacketType.LogoutRequest)
{
lock (PendingAcks)
{
if (PendingAcks.Count > 0 &&
PendingAcks.Count < Client.Settings.MAX_APPENDED_ACKS)
{
// Append all of the queued up outgoing ACKs to this packet
packet.Header.AckList = new uint[PendingAcks.Count];
for (int i = 0; i < PendingAcks.Count; i++)
packet.Header.AckList[i] = PendingAcks.Values[i];
PendingAcks.Clear();
packet.Header.AppendedAcks = true;
}
}
}
}
}
else if (packet.Header.AckList.Length > 0)
{
// Sanity check for ACKS appended on an unreliable packet, this is bad form
Logger.Log("Sending appended ACKs on an unreliable packet", Helpers.LogLevel.Warning);
}
}
// Serialize the packet
buffer = packet.ToBytes();
bytes = buffer.Length;
//.........这里部分代码省略.........
示例9: Send
public void Send(INetworkInterface i, Packet packet)
{
i.Send(packet.ToBytes());
}
示例10: SendPacket
/// <summary>
/// Sends a packet
/// </summary>
/// <param name="packet">Packet to be sent</param>
/// <param name="incrementSequence">Increment sequence number?</param>
public void SendPacket(Packet packet, bool incrementSequence)
{
byte[] buffer;
int bytes;
// Keep track of when this packet was sent out
packet.TickCount = Environment.TickCount;
if (incrementSequence)
{
// Set the sequence number
lock (SequenceLock)
{
if (Sequence > Settings.MAX_SEQUENCE)
Sequence = 1;
else
Sequence++;
packet.Header.Sequence = Sequence;
}
// Scrub any appended ACKs since all of the ACK handling is done here
if (packet.Header.AckList.Length > 0)
packet.Header.AckList = new uint[0];
packet.Header.AppendedAcks = false;
if (packet.Header.Reliable)
{
lock (NeedAck)
{
if (!NeedAck.ContainsKey(packet.Header.Sequence))
NeedAck.Add(packet.Header.Sequence, packet);
else
Client.Log("Attempted to add a duplicate sequence number (" +
packet.Header.Sequence + ") to the NeedAck dictionary for packet type " +
packet.Type.ToString(), Helpers.LogLevel.Warning);
}
// Don't append ACKs to resent packets, in case that's what was causing the
// delivery to fail
if (!packet.Header.Resent)
{
// Append any ACKs that need to be sent out to this packet
lock (PendingAcks)
{
if (PendingAcks.Count > 0 && PendingAcks.Count < Client.Settings.MAX_APPENDED_ACKS &&
packet.Type != PacketType.PacketAck &&
packet.Type != PacketType.LogoutRequest)
{
packet.Header.AckList = new uint[PendingAcks.Count];
for (int i = 0; i < PendingAcks.Count; i++)
packet.Header.AckList[i] = PendingAcks.Values[i];
PendingAcks.Clear();
packet.Header.AppendedAcks = true;
}
}
}
}
}
// Serialize the packet
buffer = packet.ToBytes();
bytes = buffer.Length;
SentBytes += (ulong)bytes;
SentPackets++;
try
{
// Zerocode if needed
if (packet.Header.Zerocoded)
{
lock (ZeroOutBuffer)
{
bytes = Helpers.ZeroEncode(buffer, bytes, ZeroOutBuffer);
Connection.Send(ZeroOutBuffer, bytes, SocketFlags.None);
}
}
else
{
Connection.Send(buffer, bytes, SocketFlags.None);
}
}
catch (SocketException)
{
Client.Log("Tried to send a " + packet.Type.ToString() + " on a closed socket, shutting down " +
this.ToString(), Helpers.LogLevel.Info);
Network.DisconnectSim(this);
return;
}
}
示例11: SendPacket
/// <summary>
/// Sends a packet
/// </summary>
/// <param name="packet">Packet to be sent</param>
/// <param name="incrementSequence">Increment sequence number?</param>
public void SendPacket(Packet packet, bool incrementSequence, NetworkInfo User_info)
{
lock(this._sendPacketSync)
{
byte[] buffer;
int bytes;
if (!connected && packet.Type != PacketType.UseCircuitCode)
{
// Client.Log("Trying to send a " + packet.Type.ToString() + " packet when the socket is closed",
// Helpers.LogLevel.Info);
return;
}
if (packet.Header.AckList.Length > 0)
{
// Scrub any appended ACKs since all of the ACK handling is done here
packet.Header.AckList = new uint[0];
}
packet.Header.AppendedAcks = false;
// Keep track of when this packet was sent out
packet.TickCount = Environment.TickCount;
if (incrementSequence)
{
// Set the sequence number
lock (SequenceLock)
{
if (Sequence > Settings.MAX_SEQUENCE)
Sequence = 1;
else
Sequence++;
packet.Header.Sequence = Sequence;
}
if (packet.Header.Reliable)
{
lock (User_info.NeedAck)
{
if (!User_info.NeedAck.ContainsKey(packet.Header.Sequence))
{
User_info.NeedAck.Add(packet.Header.Sequence, packet);
}
else
{
// Client.Log("Attempted to add a duplicate sequence number (" +
// packet.Header.Sequence + ") to the NeedAck dictionary for packet type " +
// packet.Type.ToString(), Helpers.LogLevel.Warning);
}
}
// Don't append ACKs to resent packets, in case that's what was causing the
// delivery to fail
if (!packet.Header.Resent)
{
// Append any ACKs that need to be sent out to this packet
lock (User_info.PendingAcks)
{
if (User_info.PendingAcks.Count > 0 && User_info.PendingAcks.Count < Settings.MAX_APPENDED_ACKS &&
packet.Type != PacketType.PacketAck &&
packet.Type != PacketType.LogoutRequest)
{
packet.Header.AckList = new uint[User_info.PendingAcks.Count];
int i = 0;
foreach (uint ack in User_info.PendingAcks.Values)
{
packet.Header.AckList[i] = ack;
i++;
}
User_info.PendingAcks.Clear();
packet.Header.AppendedAcks = true;
}
}
}
}
}
// Serialize the packet
buffer = packet.ToBytes();
bytes = buffer.Length;
try
{
// Zerocode if needed
if (packet.Header.Zerocoded)
{
lock (ZeroOutBuffer)
{
bytes = Helpers.ZeroEncode(buffer, bytes, ZeroOutBuffer);
Connection.SendTo(ZeroOutBuffer, bytes, SocketFlags.None,User_info.endpoint);
}
//.........这里部分代码省略.........