Skip to content

NateShoffner/ENetSharp

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

18 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

ENetSharp

C# ENet Wrapper

Build status

Usage

ENet Server

using (var host = new Host())
{
	host.Create(5000, 1);
	var peer = new Peer();

	while (host.Service(1) >= 0)
	{
		Event @event;

		while (host.CheckEvents(out @event) > 0)
		{
			switch (@event.Type)
			{
				case EventType.Connect:
					peer = @event.Peer;
					for (var i = 0; i < 200; i++)
					{
						peer.Send((byte) i, new byte[] {0, 0});
					}
					break;

				case EventType.Receive:
					var data = @event.Packet.GetBytes();
					var value = BitConverter.ToUInt16(data, 0);
					if (value%1000 == 1)
					{
						Console.WriteLine("  Server: Ch={0} Recv={1}", @event.ChannelID, value);
					}
					value++;
					peer.Send(@event.ChannelID, BitConverter.GetBytes(value));
					@event.Packet.Dispose();
					break;
			}
		}
	}
}

ENet Client

using (var host = new Host())
{
	host.Create(null, 1);

	var address = new Address();
	address.SetHost("127.0.0.1");
	address.Port = 5000;

	var peer = host.Connect(address, 200, 1234);
	while (host.Service(1) >= 0)
	{
		Event @event;
		while (host.CheckEvents(out @event) > 0)
		{
			switch (@event.Type)
			{
				case EventType.Receive:
					var data = @event.Packet.GetBytes();
					var value = BitConverter.ToUInt16(data, 0);
					if (value%1000 == 0)
					{
					Console.WriteLine("  Client: Ch={0} Recv={1}", @event.ChannelID, value);
					}
					value++;
					peer.Send(@event.ChannelID, BitConverter.GetBytes(value));
					@event.Packet.Dispose();
					break;
			}
		}
	}
}

About

C# ENet Wrapper

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published