本文整理汇总了C#中PacketWriter.Flush方法的典型用法代码示例。如果您正苦于以下问题:C# PacketWriter.Flush方法的具体用法?C# PacketWriter.Flush怎么用?C# PacketWriter.Flush使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类PacketWriter
的用法示例。
在下文中一共展示了PacketWriter.Flush方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: FormatPacket
byte[] FormatPacket(ushort opcode, byte[] data, bool encrypted)
{
// Sanity check
if (data.Length >= 0x8000)
{
throw (new Exception("[SecurityAPI::FormatPacket] Payload is too large!"));
}
ushort data_length = (ushort)data.Length;
// Add the packet header to the start of the data
PacketWriter writer = new PacketWriter();
writer.Write(data_length); // packet size
writer.Write(opcode); // packet opcode
writer.Write((ushort)0); // packet security bytes
writer.Write(data);
writer.Flush();
// Determine if we need to mark the packet size as encrypted
if (encrypted && (m_security_flags.blowfish == 1 || (m_security_flags.security_bytes == 1 && m_security_flags.blowfish == 0)))
{
long seek_index = writer.BaseStream.Seek(0, SeekOrigin.Current);
ushort packet_size = (ushort)(data_length | 0x8000);
writer.BaseStream.Seek(0, SeekOrigin.Begin);
writer.Write((ushort)packet_size);
writer.Flush();
writer.BaseStream.Seek(seek_index, SeekOrigin.Begin);
}
// Only need to stamp bytes if this is a clientless object
if (m_client_security == false && m_security_flags.security_bytes == 1)
{
long seek_index = writer.BaseStream.Seek(0, SeekOrigin.Current);
byte sb1 = GenerateCountByte(true);
writer.BaseStream.Seek(4, SeekOrigin.Begin);
writer.Write(sb1);
writer.Flush();
byte sb2 = GenerateCheckByte(writer.GetBytes());
writer.BaseStream.Seek(5, SeekOrigin.Begin);
writer.Write(sb2);
writer.Flush();
writer.BaseStream.Seek(seek_index, SeekOrigin.Begin);
}
// If the packet should be physically encrypted, return an encrypted version of it
if (encrypted && m_security_flags.blowfish == 1)
{
byte[] raw_data = writer.GetBytes();
byte[] encrypted_data = m_blowfish.Encode(raw_data, 2, raw_data.Length - 2);
writer.BaseStream.Seek(2, SeekOrigin.Begin);
writer.Write(encrypted_data);
writer.Flush();
}
else
{
// Determine if we need to unmark the packet size from being encrypted but not physically encrypted
if (encrypted && (m_security_flags.security_bytes == 1 && m_security_flags.blowfish == 0))
{
long seek_index = writer.BaseStream.Seek(0, SeekOrigin.Current);
writer.BaseStream.Seek(0, SeekOrigin.Begin);
writer.Write((ushort)data_length);
writer.Flush();
writer.BaseStream.Seek(seek_index, SeekOrigin.Begin);
}
}
// Return the final data
return writer.GetBytes();
}
示例2: IoThread
void IoThread()
{
try {
client.SendTimeout = Timeout;
client.ReceiveTimeout = Timeout;
IP = ((IPEndPoint)(client.Client.RemoteEndPoint)).Address;
Name = "from " + IP; // placeholder for logging
stream = client.GetStream();
reader = new PacketReader( stream );
writer = new PacketWriter( stream );
throttleCheckTimer = DateTime.UtcNow + ThrottleInterval;
if( !LoginSequence() ) return;
while( canSend ) {
// Write normal packets to output
while( sendQueue.Count > 0 ) {
Packet packet;
lock( sendQueueLock ) {
packet = sendQueue.Dequeue();
}
writer.Write( packet.Bytes );
if( packet.OpCode == OpCode.Kick ) {
writer.Flush();
return;
}
}
// Write SetBlock packets to output
while( blockSendQueue.Count > 0 && throttlePacketCount < ThrottleThreshold && canSend ) {
Packet packet;
lock( blockSendQueueLock ) {
packet = blockSendQueue.Dequeue();
}
ProcessOutgoingSetBlock(ref packet);
writer.Write( packet.Bytes );
throttlePacketCount++;
}
if( DateTime.UtcNow > throttleCheckTimer ) {
throttlePacketCount = 0;
throttleCheckTimer += ThrottleInterval;
}
// Check if a map change is pending. Resend map if it is.
if( mapToJoin != Map ) {
Map = mapToJoin;
for( int i = 1; i < sbyte.MaxValue; i++ ) {
writer.Write( Packet.MakeRemoveEntity( i ).Bytes );
}
SendMap();
Server.SpawnPlayers( this );
}
// Read input from player
while( canReceive && stream.DataAvailable ) {
OpCode opCode = reader.ReadOpCode();
switch( opCode ) {
case OpCode.Message:
if( !ProcessMessagePacket() ) return;
break;
case OpCode.Teleport:
ProcessMovementPacket();
break;
case OpCode.SetBlockClient:
if( !ProcessSetBlockPacket() ) return;
break;
case OpCode.Ping:
break;
default:
Logger.Log( "Player {0} was kicked after sending an invalid opCode ({1}).",
Name, opCode );
KickNow( "Unknown packet opCode " + opCode );
return;
}
}
Thread.Sleep( SleepDelay );
}
} catch( IOException ) {} catch( SocketException ) {
#if !DEBUG
} catch( Exception ex ) {
Logger.LogError( "Player: Session crashed: {0}", ex );
#endif
} finally {
canQueue = false;
canSend = false;
Disconnect();
}
}