1
0
mirror of https://github.com/lidgren/lidgren-network-gen3.git synced 2026-05-15 22:56:30 +09:00

NetPeerConfiguration.AutoFlushSendQueue added. When set to false; application must call NetPeer.FlushSendQueue manually.

This commit is contained in:
lidgren
2011-09-12 09:34:50 +00:00
parent b4fd011e5b
commit 60a6b28189
4 changed files with 39 additions and 9 deletions

View File

@@ -225,13 +225,16 @@ namespace Lidgren.Network
//
// send queued messages
//
for (int i = m_sendChannels.Length - 1; i >= 0; i--) // Reverse order so reliable messages are sent first
if (m_peer.m_executeFlushSendQueue)
{
var channel = m_sendChannels[i];
NetException.Assert(m_sendBufferWritePtr < 1 || m_sendBufferNumMessages > 0);
if (channel != null)
channel.SendQueuedMessages(now);
NetException.Assert(m_sendBufferWritePtr < 1 || m_sendBufferNumMessages > 0);
for (int i = m_sendChannels.Length - 1; i >= 0; i--) // Reverse order so reliable messages are sent first
{
var channel = m_sendChannels[i];
NetException.Assert(m_sendBufferWritePtr < 1 || m_sendBufferNumMessages > 0);
if (channel != null)
channel.SendQueuedMessages(now);
NetException.Assert(m_sendBufferWritePtr < 1 || m_sendBufferNumMessages > 0);
}
}
//

View File

@@ -32,6 +32,7 @@ namespace Lidgren.Network
internal readonly NetPeerStatistics m_statistics;
internal long m_uniqueIdentifier;
internal bool m_executeFlushSendQueue;
private AutoResetEvent m_messageReceivedEvent = new AutoResetEvent(false);
private List<NetTuple<SynchronizationContext, SendOrPostCallback>> m_receiveCallbacks;
@@ -62,7 +63,7 @@ namespace Lidgren.Network
HandleReleasedFragment(msg);
return;
}
m_releasedIncomingMessages.Enqueue(msg);
if (m_messageReceivedEvent != null)
@@ -280,9 +281,13 @@ namespace Lidgren.Network
}
#if DEBUG
SendDelayedPackets();
SendDelayedPackets();
#endif
// update m_executeFlushSendQueue
if (m_configuration.m_autoFlushSendQueue)
m_executeFlushSendQueue = true;
// do connection heartbeats
lock (m_connections)
{
@@ -300,6 +305,7 @@ namespace Lidgren.Network
}
}
}
m_executeFlushSendQueue = false;
// send unsent unconnected messages
NetTuple<IPEndPoint, NetOutgoingMessage> unsent;
@@ -472,6 +478,14 @@ namespace Lidgren.Network
} while (m_socket.Available > 0);
}
/// <summary>
/// If NetPeerConfiguration.AutoFlushSendQueue() is false; you need to call this to send all messages queued using SendMessage()
/// </summary>
public void FlushSendQueue()
{
m_executeFlushSendQueue = true;
}
internal void HandleIncomingDiscoveryRequest(double now, IPEndPoint senderEndpoint, int ptr, int payloadByteLength)
{
if (m_configuration.IsMessageTypeEnabled(NetIncomingMessageType.DiscoveryRequest))
@@ -631,4 +645,4 @@ namespace Lidgren.Network
return m_readHelperMessage;
}
}
}
}

View File

@@ -40,6 +40,7 @@ namespace Lidgren.Network
internal bool m_useMessageRecycling;
internal float m_connectionTimeout;
internal bool m_enableUPnP;
internal bool m_autoFlushSendQueue;
internal NetIncomingMessageType m_disabledTypes;
internal int m_port;
@@ -86,6 +87,7 @@ namespace Lidgren.Network
m_useMessageRecycling = true;
m_resendHandshakeInterval = 3.0f;
m_maximumHandshakeAttempts = 5;
m_autoFlushSendQueue = true;
// Maximum transmission unit
// Ethernet can take 1500 bytes of payload, so lets stay below that.
@@ -258,6 +260,15 @@ namespace Lidgren.Network
}
}
/// <summary>
/// Enables or disables automatic flushing of the send queue. If disabled, you must manully call NetPeer.FlushSendQueue() to flush sent messages to network.
/// </summary>
public bool AutoFlushSendQueue
{
get { return m_autoFlushSendQueue; }
set { m_autoFlushSendQueue = value; }
}
/// <summary>
/// Gets or sets the local ip address to bind to. Defaults to IPAddress.Any. Cannot be changed once NetPeer is initialized.
/// </summary>

View File

@@ -22,6 +22,7 @@ namespace ChatClient
s_form = new Form1();
NetPeerConfiguration config = new NetPeerConfiguration("chat");
config.AutoFlushSendQueue = false;
s_client = new NetClient(config);
s_client.RegisterReceivedCallback(new SendOrPostCallback(GotMessage));
@@ -95,6 +96,7 @@ namespace ChatClient
NetOutgoingMessage om = s_client.CreateMessage(text);
s_client.SendMessage(om, NetDeliveryMethod.ReliableOrdered);
Output("Sending '" + text + "'");
s_client.FlushSendQueue();
}
// called by the UI