using System;
using System.Collections.Generic;
namespace Lidgren.Network
{
///
/// Specialized version of NetPeer used for "server" peers
///
public class NetServer : NetPeer
{
///
/// NetServer constructor
///
public NetServer(NetPeerConfiguration config)
: base(config)
{
config.AcceptIncomingConnections = true;
}
///
/// Send a message to all connections
///
/// The message to send
/// How to deliver the message
public void SendToAll(NetOutgoingMessage msg, NetDeliveryMethod method)
{
// Modifying m_connections will modify the list of the connections of the NetPeer. Do only reads here
var all = m_connections;
if (all.Count <= 0) {
if (msg.m_isSent == false)
Recycle(msg);
return;
}
SendMessage(msg, all, method, 0);
}
///
/// Send a message to all connections
///
/// The message to send
/// How to deliver the message
/// Which sequence channel to use for the message
public void SendToAll(NetOutgoingMessage msg, NetDeliveryMethod method, int sequenceChannel)
{
// Modifying m_connections will modify the list of the connections of the NetPeer. Do only reads here
var all = m_connections;
if (all.Count <= 0) {
if (msg.m_isSent == false)
Recycle(msg);
return;
}
SendMessage(msg, all, method, sequenceChannel);
}
///
/// Send a message to all connections except one
///
/// The message to send
/// How to deliver the message
/// Don't send to this particular connection
/// Which sequence channel to use for the message
public void SendToAll(NetOutgoingMessage msg, NetConnection except, NetDeliveryMethod method, int sequenceChannel)
{
// Modifying m_connections will modify the list of the connections of the NetPeer. Do only reads here
var all = m_connections;
if (all.Count <= 0) {
if (msg.m_isSent == false)
Recycle(msg);
return;
}
if (except == null)
{
SendMessage(msg, all, method, sequenceChannel);
return;
}
List recipients = new List(all.Count - 1);
foreach (var conn in all)
if (conn != except)
recipients.Add(conn);
if (recipients.Count > 0)
SendMessage(msg, recipients, method, sequenceChannel);
}
///
/// Returns a string that represents this object
///
public override string ToString()
{
return "[NetServer " + ConnectionsCount + " connections]";
}
}
}