public override void Send(ulong clientId, ArraySegment <byte> data, string channelName, bool skipQueue) { GetRufflesConnectionDetails(clientId, out ulong connectionId); byte channelId = channelNameToId[channelName]; socket.Send(data, connectionId, channelId, skipQueue); }
public static void Main(string[] args) { RuffleSocket server = new RuffleSocket(ServerConfig); RuffleSocket client = new RuffleSocket(ClientConfig); client.Start(); server.Start(); if (IPv6) { // IPv6 Connect client.Connect(new IPEndPoint(IPAddress.Parse("0:0:0:0:0:0:0:1"), 5674)); } else { // IPv4 Connect client.Connect(new IPEndPoint(IPAddress.Parse("127.0.0.1"), 5674)); } // The server stores the clients id here ulong clientId = 0; Connection clientConnection = null; // The client stores the servers id here ulong serverId = 0; Connection serverConnection = null; // The time when the connection started DateTime started = DateTime.Now; // The time when the last message was sent DateTime lastSent = DateTime.MinValue; // The time the last status was printed DateTime lastStatusPrint = DateTime.MinValue; // The amount of message that has been received int messagesReceived = 0; // The amount of messages that has been sent int messageCounter = 0; while (true) { // Polls server for events NetworkEvent serverEvent = server.Poll(); // Polls client for events NetworkEvent clientEvent = client.Poll(); if (serverEvent.Type != NetworkEventType.Nothing) { if (serverEvent.Type != NetworkEventType.Data) { Console.WriteLine("ServerEvent: " + serverEvent.Type); } if (serverEvent.Type == NetworkEventType.Connect) { clientId = serverEvent.Connection.Id; clientConnection = serverEvent.Connection; } } serverEvent.Recycle(); if (clientEvent.Type != NetworkEventType.Nothing) { if (clientEvent.Type != NetworkEventType.Data) { Console.WriteLine("ClientEvent: " + clientEvent.Type); } if (clientEvent.Type == NetworkEventType.Connect) { serverId = clientEvent.Connection.Id; serverConnection = clientEvent.Connection; } if (clientEvent.Type == NetworkEventType.Data) { messagesReceived++; Console.WriteLine("Got message: \"" + Encoding.ASCII.GetString(clientEvent.Data.Array, clientEvent.Data.Offset, clientEvent.Data.Count) + "\""); } } clientEvent.Recycle(); if ((DateTime.Now - started).TotalSeconds > 10 && (DateTime.Now - lastSent).TotalSeconds >= (1f / 1)) { byte[] helloReliable = Encoding.ASCII.GetBytes("This message was sent over a reliable channel" + messageCounter); server.Send(new ArraySegment <byte>(helloReliable, 0, helloReliable.Length), clientId, 1, false); Console.WriteLine("Sending packet: " + messageCounter); messageCounter++; lastSent = DateTime.Now; } if ((DateTime.Now - started).TotalSeconds > 10 && (DateTime.Now - lastStatusPrint).TotalSeconds >= 5) { Console.WriteLine("Ping: " + serverConnection.SmoothRoundtrip + "ms, " + clientConnection.SmoothRoundtrip + "ms"); lastStatusPrint = DateTime.Now; } } }
private static void NoRufflesManager() { RuffleSocket server = new RuffleSocket(ServerConfig); RuffleSocket client = new RuffleSocket(ClientConfig); // IPv4 Connect //client.Connect(new IPEndPoint(IPAddress.Parse("127.0.0.1"), 5674)); // IPv6 Connect client.Connect(new IPEndPoint(IPAddress.Parse("0:0:0:0:0:0:0:1"), 5674)); // The server stores the clients id here ulong clientId = 0; // The client stores the servers id here ulong serverId = 0; // The time when the connection started DateTime started = DateTime.Now; // The time when the last message was sent DateTime lastSent = DateTime.MinValue; // The amount of message that has been received int messagesReceived = 0; // The amount of messages that has been sent int messageCounter = 0; while (true) { // Runs all the internals client.RunInternalLoop(); // Runs all the internals server.RunInternalLoop(); // Polls server for events NetworkEvent serverEvent = server.Poll(); // Polls client for events NetworkEvent clientEvent = client.Poll(); if (serverEvent.Type != NetworkEventType.Nothing) { if (serverEvent.Type != NetworkEventType.Data) { Console.WriteLine("ServerEvent: " + serverEvent.Type); } if (serverEvent.Type == NetworkEventType.Connect) { clientId = serverEvent.Connection.Id; } if (serverEvent.Type == NetworkEventType.Disconnect || serverEvent.Type == NetworkEventType.Timeout) { serverEvent.Connection.Recycle(); } } if (clientEvent.Type != NetworkEventType.Nothing) { if (clientEvent.Type != NetworkEventType.Data) { Console.WriteLine("ClientEvent: " + clientEvent.Type); } if (clientEvent.Type == NetworkEventType.Connect) { serverId = clientEvent.Connection.Id; } if (clientEvent.Type == NetworkEventType.Data) { messagesReceived++; Console.WriteLine("Got message: \"" + Encoding.ASCII.GetString(clientEvent.Data.Array, clientEvent.Data.Offset, clientEvent.Data.Count) + "\""); clientEvent.Recycle(); } if (clientEvent.Type == NetworkEventType.Disconnect || clientEvent.Type == NetworkEventType.Timeout) { clientEvent.Connection.Recycle(); } } if ((DateTime.Now - started).TotalSeconds > 10 && (DateTime.Now - lastSent).TotalSeconds >= 1) { byte[] helloReliable = Encoding.ASCII.GetBytes("This message was sent over a reliable channel" + messageCounter); byte[] helloReliableSequenced = Encoding.ASCII.GetBytes("This message was sent over a reliable sequenced channel" + messageCounter); server.Send(new ArraySegment <byte>(helloReliableSequenced, 0, helloReliableSequenced.Length), clientId, 0, false); server.Send(new ArraySegment <byte>(helloReliable, 0, helloReliable.Length), clientId, 1, false); messageCounter++; lastSent = DateTime.Now; } } }