You've already forked lidgren-network-gen3
mirror of
https://github.com/lidgren/lidgren-network-gen3.git
synced 2026-05-17 07:36:32 +09:00
- ReadMessages() added to batch read messages. Image sample changed to use the new batch method.
This commit is contained in:
@@ -148,6 +148,28 @@ namespace Lidgren.Network
|
|||||||
m_incomingMessagesPool.Enqueue(msg);
|
m_incomingMessagesPool.Enqueue(msg);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Recycles a list of NetIncomingMessage instances for reuse; taking pressure off the garbage collector
|
||||||
|
/// </summary>
|
||||||
|
public void Recycle(IEnumerable<NetIncomingMessage> toRecycle)
|
||||||
|
{
|
||||||
|
if (m_incomingMessagesPool == null)
|
||||||
|
return;
|
||||||
|
|
||||||
|
foreach (var msg in toRecycle)
|
||||||
|
{
|
||||||
|
#if DEBUG
|
||||||
|
if (m_incomingMessagesPool.Contains(msg))
|
||||||
|
throw new NetException("Recyling already recycled message! Thread race?");
|
||||||
|
#endif
|
||||||
|
byte[] storage = msg.m_data;
|
||||||
|
msg.m_data = null;
|
||||||
|
Recycle(storage);
|
||||||
|
msg.Reset();
|
||||||
|
m_incomingMessagesPool.Enqueue(msg);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
internal void Recycle(NetOutgoingMessage msg)
|
internal void Recycle(NetOutgoingMessage msg)
|
||||||
{
|
{
|
||||||
if (m_outgoingMessagesPool == null)
|
if (m_outgoingMessagesPool == null)
|
||||||
|
|||||||
@@ -183,7 +183,29 @@ namespace Lidgren.Network
|
|||||||
}
|
}
|
||||||
return retval;
|
return retval;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Read a pending message from any connection, if any
|
||||||
|
/// </summary>
|
||||||
|
public int ReadMessages(IList<NetIncomingMessage> addTo)
|
||||||
|
{
|
||||||
|
int added = m_releasedIncomingMessages.TryDrain(addTo);
|
||||||
|
if (added > 0)
|
||||||
|
{
|
||||||
|
for (int i = 0; i < added; i++)
|
||||||
|
{
|
||||||
|
var index = addTo.Count - added + i;
|
||||||
|
var nim = addTo[index];
|
||||||
|
if (nim.MessageType == NetIncomingMessageType.StatusChanged)
|
||||||
|
{
|
||||||
|
NetConnectionStatus status = (NetConnectionStatus)nim.PeekByte();
|
||||||
|
nim.SenderConnection.m_visibleStatus = status;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return added;
|
||||||
|
}
|
||||||
|
|
||||||
// send message immediately
|
// send message immediately
|
||||||
internal void SendLibrary(NetOutgoingMessage msg, IPEndPoint recipient)
|
internal void SendLibrary(NetOutgoingMessage msg, IPEndPoint recipient)
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -19,6 +19,7 @@ USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|||||||
*/
|
*/
|
||||||
using System;
|
using System;
|
||||||
using System.Diagnostics;
|
using System.Diagnostics;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
|
||||||
namespace Lidgren.Network
|
namespace Lidgren.Network
|
||||||
{
|
{
|
||||||
@@ -159,6 +160,30 @@ namespace Lidgren.Network
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Gets an item from the head of the queue, or returns default(T) if empty
|
||||||
|
/// </summary>
|
||||||
|
public int TryDrain(IList<T> addTo)
|
||||||
|
{
|
||||||
|
if (m_size == 0)
|
||||||
|
return 0;
|
||||||
|
|
||||||
|
lock (m_lock)
|
||||||
|
{
|
||||||
|
int added = m_size;
|
||||||
|
while (m_size > 0)
|
||||||
|
{
|
||||||
|
var item = m_items[m_head];
|
||||||
|
addTo.Add(item);
|
||||||
|
|
||||||
|
m_items[m_head] = default(T);
|
||||||
|
m_head = (m_head + 1) % m_items.Length;
|
||||||
|
m_size--;
|
||||||
|
}
|
||||||
|
return added;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Returns default(T) if queue is empty
|
/// Returns default(T) if queue is empty
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
|||||||
@@ -18,6 +18,7 @@ namespace ImageClient
|
|||||||
public int NumReceivedSegments;
|
public int NumReceivedSegments;
|
||||||
|
|
||||||
private double m_startedFetching;
|
private double m_startedFetching;
|
||||||
|
private List<NetIncomingMessage> m_readList;
|
||||||
|
|
||||||
public ImageGetter(string host, NetPeerConfiguration copyConfig)
|
public ImageGetter(string host, NetPeerConfiguration copyConfig)
|
||||||
{
|
{
|
||||||
@@ -25,6 +26,7 @@ namespace ImageClient
|
|||||||
|
|
||||||
NetPeerConfiguration config = copyConfig.Clone();
|
NetPeerConfiguration config = copyConfig.Clone();
|
||||||
config.EnableMessageType(NetIncomingMessageType.DiscoveryResponse);
|
config.EnableMessageType(NetIncomingMessageType.DiscoveryResponse);
|
||||||
|
m_readList = new List<NetIncomingMessage>();
|
||||||
|
|
||||||
Client = new NetClient(config);
|
Client = new NetClient(config);
|
||||||
Client.Start();
|
Client.Start();
|
||||||
@@ -50,8 +52,11 @@ namespace ImageClient
|
|||||||
|
|
||||||
public void Heartbeat()
|
public void Heartbeat()
|
||||||
{
|
{
|
||||||
NetIncomingMessage inc;
|
int numRead = Client.ReadMessages(m_readList);
|
||||||
while ((inc = Client.ReadMessage()) != null)
|
if (numRead < 1)
|
||||||
|
return;
|
||||||
|
|
||||||
|
foreach(var inc in m_readList)
|
||||||
{
|
{
|
||||||
switch(inc.MessageType)
|
switch(inc.MessageType)
|
||||||
{
|
{
|
||||||
@@ -166,10 +171,11 @@ namespace ImageClient
|
|||||||
|
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
// recycle message to avoid garbage
|
|
||||||
Client.Recycle(inc);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// recycle messages to avoid garbage
|
||||||
|
Client.Recycle(m_readList);
|
||||||
|
m_readList.Clear();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user