You've already forked lidgren-network-gen3
mirror of
https://github.com/lidgren/lidgren-network-gen3.git
synced 2026-05-16 23:26: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);
|
||||
}
|
||||
|
||||
/// <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)
|
||||
{
|
||||
if (m_outgoingMessagesPool == null)
|
||||
|
||||
@@ -183,7 +183,29 @@ namespace Lidgren.Network
|
||||
}
|
||||
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
|
||||
internal void SendLibrary(NetOutgoingMessage msg, IPEndPoint recipient)
|
||||
{
|
||||
|
||||
@@ -19,6 +19,7 @@ USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||
*/
|
||||
using System;
|
||||
using System.Diagnostics;
|
||||
using System.Collections.Generic;
|
||||
|
||||
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>
|
||||
/// Returns default(T) if queue is empty
|
||||
/// </summary>
|
||||
|
||||
Reference in New Issue
Block a user