diff --git a/src/rudp/partials/RUDP.Client.cs b/src/rudp/partials/RUDP.Client.cs index e9ac7f6..429c4bd 100644 --- a/src/rudp/partials/RUDP.Client.cs +++ b/src/rudp/partials/RUDP.Client.cs @@ -1,4 +1,5 @@ using System; +using System.Net.Sockets; using Netly.Interfaces; namespace Netly @@ -10,6 +11,12 @@ public partial class Client : IRUDP.Client internal readonly ClientOn _on; private readonly ClientTo _to; + public bool IsOpened => _to.IsOpened; + public Host Host => _to.Host; + public IRUDP.ClientTo To => _to; + public IRUDP.ClientOn On => _on; + public string Id { get; } + public Client() { Id = Guid.NewGuid().ToString(); @@ -17,7 +24,10 @@ public Client() _to = new ClientTo(this); } - public bool IsOpened => _to.IsOpened; + public Client(Host host, Socket socket) : this() + { + _to = new ClientTo(this, host, socket); + } public int OpenTimeout { @@ -25,10 +35,8 @@ public int OpenTimeout set => _to.SetOpenTimeout(value); } - public Host Host => _to.Host; - public IRUDP.ClientTo To => _to; - public IRUDP.ClientOn On => _on; - public string Id { get; } + internal void InjectBuffer(ref byte[] bytes) => _to.InjectBuffer(ref bytes); + internal void StartServerSideConnection(Action callback) => _to.StartServerSideConnection(ref callback); } } } \ No newline at end of file diff --git a/src/rudp/partials/RUDP.ClientTo.cs b/src/rudp/partials/RUDP.ClientTo.cs index e6d010a..acc80a5 100644 --- a/src/rudp/partials/RUDP.ClientTo.cs +++ b/src/rudp/partials/RUDP.ClientTo.cs @@ -21,7 +21,7 @@ private class ClientTo : IRUDP.ClientTo private int _openTimeout; private ClientOn On => _client._on; - public ClientTo() + private ClientTo() { Host = Host.Default; _client = null; @@ -37,8 +37,9 @@ public ClientTo(Client client) : this() _isServer = false; } - public ClientTo(Host host, Socket socket) : this() + public ClientTo(Client client, Host host, Socket socket) : this() { + _client = client; _socket = socket; _isServer = true; InitConnection(ref host); @@ -230,7 +231,7 @@ private void InitConnection(ref Host host) _connection = null; On.OnClose?.Invoke(null, null); }, - OnOpenFail = (message) => + OnOpenFail = message => { // error on open connection _connection = null; @@ -249,10 +250,23 @@ private void InitConnection(ref Host host) }; } - public void StartConnection() + private void StartConnection() { _connection.Open(_openTimeout).Wait(); } + + public void InjectBuffer(ref byte[] bytes) + { + if (IsOpened) + { + _connection?.InjectBuffer(bytes); + } + } + + public void StartServerSideConnection(ref Action callback) + { + throw new NotImplementedException(); + } } } } \ No newline at end of file diff --git a/src/rudp/partials/RUDP.ServerTo.cs b/src/rudp/partials/RUDP.ServerTo.cs index 463ab11..d54ac74 100644 --- a/src/rudp/partials/RUDP.ServerTo.cs +++ b/src/rudp/partials/RUDP.ServerTo.cs @@ -41,7 +41,7 @@ public IRUDP.Client[] GetClients() { lock (_clientsLocker) { - return _clients.Select(x => (IRUDP.Client)x).ToArray(); + return _clients.Where(x => x.IsOpened).Select(x => (IRUDP.Client)x).ToArray(); } } @@ -268,6 +268,7 @@ private void ContentUpdate() if (_contents.Count <= 0) continue; (Host host, byte[] data) value; + Client client; lock (_contentsLooker) { @@ -283,8 +284,62 @@ private void ContentUpdate() try { - // TODO: implement this - _ = value; + lock (_clientsLocker) + { + // find client + client = _clients.FirstOrDefault(x => value.host.Equals(x.Host)); + } + + byte[] buffer = value.data; + + // use existent context + if (client != null) + { + client.InjectBuffer(ref buffer); + return; + } + + // create new context + client = new Client(value.host, _socket); + // TODO: client.OpenTimeout = _acceptTimeout; + + // save context + lock (_clientsLocker) + { + _clients.Add(client); + } + + client.StartServerSideConnection(isError => + { + if (isError) + { + // remove client from client list + lock (_clientsLocker) + { + _clients.Remove(client); + } + + client = null; + } + else // connected successful + { + client.On.Close(() => + { + // connection closed + lock (_clientsLocker) + { + _clients.Remove(client); + } + + client = null; + }); + + // invoke new client + On.OnAccept?.Invoke(null, client); + } + }); + + client.InjectBuffer(ref buffer); } catch (Exception e) { diff --git a/src/rudp/partials/utils/RUDP.Connection.cs b/src/rudp/partials/utils/RUDP.Connection.cs index b5f9c9b..69dc402 100644 --- a/src/rudp/partials/utils/RUDP.Connection.cs +++ b/src/rudp/partials/utils/RUDP.Connection.cs @@ -81,7 +81,7 @@ public void Send(ref byte[] bytes, MessageType messageType) { } - public void Receive(byte[] buffer) + public void InjectBuffer(byte[] buffer) { }