mirror of
https://github.com/ahwootech/BRUTAL.git
synced 2026-11-25 12:38:24 +01:00
samples: add Hello/Demo samples for most BRUTAL packages
Add samples/, a collection of single-file .NET 10 apps run with `dotnet run <file>.cs`. Hello/ has one minimal sample per package that has a public API, many of them headless; Demo/ has fuller programs that combine packages into a more interesting application. samples/README.md indexes every sample and covers the layout, error handling and per-OS requirements.
This commit is contained in:
parent
a25cdf80b2
commit
b0c9fd9cdc
1 changed files with 4599 additions and 1 deletions
85
samples/Hello/HelloRakNet.cs
Normal file
85
samples/Hello/HelloRakNet.cs
Normal file
|
|
@ -0,0 +1,85 @@
|
|||
/*
|
||||
* HelloRakNet
|
||||
*
|
||||
* Starts a RakNet server and client in one process, connects them on localhost and sends a message.
|
||||
*/
|
||||
|
||||
#:property AllowUnsafeBlocks=true
|
||||
#:package Ahwoo.Brutal.RakNet@2026.10.2
|
||||
#:package Ahwoo.Brutal.Core@2026.10.1
|
||||
#:package Ahwoo.Brutal.Strings@2026.10.1
|
||||
|
||||
// RIAN: I believe this package is about to get a refresh with full plugin support (NAT punchthrough, etc).
|
||||
|
||||
using Brutal.RakNetApi;
|
||||
using Brutal.Strings;
|
||||
|
||||
const ushort PORT = 60_000;
|
||||
|
||||
Run();
|
||||
|
||||
static void Run()
|
||||
{
|
||||
// Disposing a peer destroys it, which happens after it has been shut down at the end.
|
||||
using var server = RakNetLibrary.CreateInstance();
|
||||
using var client = RakNetLibrary.CreateInstance();
|
||||
|
||||
// The server can fail to start if something else is already using the port.
|
||||
var serverSocket = new SocketDescriptor(PORT);
|
||||
var serverStartup = server.Startup(4, new Span<SocketDescriptor>(ref serverSocket));
|
||||
if (serverStartup != StartupResult.RaknetStarted)
|
||||
throw new InvalidOperationException($"Server failed to start on port {PORT}: {serverStartup}");
|
||||
server.SetMaximumIncomingConnections(4);
|
||||
|
||||
var clientSocket = new SocketDescriptor(0); // Any free port.
|
||||
client.Startup(1, new Span<SocketDescriptor>(ref clientSocket));
|
||||
|
||||
unsafe
|
||||
{
|
||||
client.Connect("127.0.0.1"u8, PORT, default, null);
|
||||
|
||||
// Everything is local, so give up if the exchange takes more than a few seconds.
|
||||
var deadline = DateTime.UtcNow + TimeSpan.FromSeconds(5);
|
||||
var running = true;
|
||||
while (running)
|
||||
{
|
||||
if (DateTime.UtcNow > deadline)
|
||||
throw new TimeoutException("Timed out connecting and exchanging a message over localhost");
|
||||
|
||||
for (var packet = client.Receive(); packet != null; packet = client.Receive())
|
||||
{
|
||||
if ((DefaultMessageIDTypes)packet->Data[0] == DefaultMessageIDTypes.ConnectionRequestAccepted)
|
||||
{
|
||||
Console.WriteLine("Client: connected to server");
|
||||
|
||||
byte[] message = [(byte)DefaultMessageIDTypes.UserPacketEnum, .. "Hello, RakNet!"u8];
|
||||
client.Send(
|
||||
message,
|
||||
PacketPriority.HighPriority,
|
||||
PacketReliability.ReliableOrdered,
|
||||
0,
|
||||
new AddressOrGUID(packet->SystemAddress),
|
||||
false
|
||||
);
|
||||
}
|
||||
client.DeallocatePacket(packet);
|
||||
}
|
||||
|
||||
for (var packet = server.Receive(); packet != null; packet = server.Receive())
|
||||
{
|
||||
if ((DefaultMessageIDTypes)packet->Data[0] == DefaultMessageIDTypes.UserPacketEnum)
|
||||
{
|
||||
var text = Utf8.GetString(packet->Data + 1, (int)packet->Length - 1);
|
||||
Console.WriteLine($"Server: received \"{text}\"");
|
||||
running = false;
|
||||
}
|
||||
server.DeallocatePacket(packet);
|
||||
}
|
||||
|
||||
Thread.Sleep(10);
|
||||
}
|
||||
}
|
||||
|
||||
client.Shutdown(300);
|
||||
server.Shutdown(300);
|
||||
}
|
||||
Loading…
Reference in a new issue