Files

61 lines
2.2 KiB
C#
Raw Permalink Normal View History

2022-09-08 12:41:56 -07:00
using Lidgren.Network;
using System;
2022-08-11 16:25:38 +08:00
using System.Collections.Generic;
using System.Threading;
namespace RageCoop.Core
{
2022-09-08 12:41:56 -07:00
internal class CoopPeer : NetPeer, IDisposable
2022-08-11 16:25:38 +08:00
{
public EventHandler<NetIncomingMessage> OnMessageReceived;
private readonly Thread ListenerThread;
2022-09-08 12:41:56 -07:00
private bool _stopping = false;
public CoopPeer(NetPeerConfiguration config) : base(config)
2022-08-11 16:25:38 +08:00
{
Start();
NetIncomingMessage msg;
2022-09-08 12:41:56 -07:00
ListenerThread = new Thread(() =>
{
while (!_stopping)
{
msg = WaitMessage(200);
if (msg != null)
{
OnMessageReceived?.Invoke(this, msg);
}
}
});
2022-08-11 16:25:38 +08:00
ListenerThread.Start();
}
/// <summary>
/// Terminate all connections and background thread
/// </summary>
public void Dispose()
{
2022-09-08 12:41:56 -07:00
_stopping = true;
2022-08-11 16:25:38 +08:00
Shutdown("Bye!");
ListenerThread.Join();
}
public void SendTo(Packet p, NetConnection connection, ConnectionChannel channel = ConnectionChannel.Default, NetDeliveryMethod method = NetDeliveryMethod.UnreliableSequenced)
{
NetOutgoingMessage outgoingMessage = CreateMessage();
p.Pack(outgoingMessage);
SendMessage(outgoingMessage, connection, method, (int)channel);
}
public void SendTo(Packet p, IList<NetConnection> connections, ConnectionChannel channel = ConnectionChannel.Default, NetDeliveryMethod method = NetDeliveryMethod.UnreliableSequenced)
{
NetOutgoingMessage outgoingMessage = CreateMessage();
p.Pack(outgoingMessage);
SendMessage(outgoingMessage, connections, method, (int)channel);
}
2022-09-08 12:41:56 -07:00
public void Send(Packet p, IList<NetConnection> cons, ConnectionChannel channel = ConnectionChannel.Default, NetDeliveryMethod method = NetDeliveryMethod.UnreliableSequenced)
2022-08-11 16:25:38 +08:00
{
NetOutgoingMessage outgoingMessage = CreateMessage();
p.Pack(outgoingMessage);
SendMessage(outgoingMessage, cons, method, (int)channel);
}
}
}