Files
RAGECOOP-V/Core/Networking/CoopPeer.cs

64 lines
2.1 KiB
C#
Raw Normal View History

2022-10-23 19:02:39 +08:00
using System;
2022-08-11 16:25:38 +08:00
using System.Collections.Generic;
using System.Threading;
2022-10-23 19:02:39 +08:00
using Lidgren.Network;
2022-08-11 16:25:38 +08:00
namespace RageCoop.Core
{
2022-09-08 12:41:56 -07:00
internal class CoopPeer : NetPeer, IDisposable
2022-08-11 16:25:38 +08:00
{
private readonly Thread ListenerThread;
2022-10-23 19:02:39 +08:00
private bool _stopping;
public EventHandler<NetIncomingMessage> OnMessageReceived;
2022-09-08 12:41:56 -07:00
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(() =>
2022-10-23 19:02:39 +08:00
{
while (!_stopping)
{
msg = WaitMessage(200);
if (msg != null) OnMessageReceived?.Invoke(this, msg);
}
});
2022-08-11 16:25:38 +08:00
ListenerThread.Start();
}
/// <summary>
2022-10-23 19:02:39 +08:00
/// Terminate all connections and background thread
2022-08-11 16:25:38 +08:00
/// </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();
}
2022-10-23 19:02:39 +08:00
public void SendTo(Packet p, NetConnection connection, ConnectionChannel channel = ConnectionChannel.Default,
NetDeliveryMethod method = NetDeliveryMethod.UnreliableSequenced)
2022-08-11 16:25:38 +08:00
{
2022-10-23 19:02:39 +08:00
var outgoingMessage = CreateMessage();
2022-08-11 16:25:38 +08:00
p.Pack(outgoingMessage);
SendMessage(outgoingMessage, connection, method, (int)channel);
}
2022-10-23 19:02:39 +08:00
public void SendTo(Packet p, IList<NetConnection> connections,
ConnectionChannel channel = ConnectionChannel.Default,
NetDeliveryMethod method = NetDeliveryMethod.UnreliableSequenced)
{
var outgoingMessage = CreateMessage();
2022-08-11 16:25:38 +08:00
p.Pack(outgoingMessage);
SendMessage(outgoingMessage, connections, method, (int)channel);
}
2022-10-23 19:02:39 +08: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
{
2022-10-23 19:02:39 +08:00
var outgoingMessage = CreateMessage();
2022-08-11 16:25:38 +08:00
p.Pack(outgoingMessage);
SendMessage(outgoingMessage, cons, method, (int)channel);
}
}
2022-10-23 19:02:39 +08:00
}