Skip to content

Commit

Permalink
RUDP.Server implemented.
Browse files Browse the repository at this point in the history
  • Loading branch information
alec1o committed Jul 3, 2024
1 parent f915f30 commit 1f97bf5
Show file tree
Hide file tree
Showing 4 changed files with 90 additions and 13 deletions.
18 changes: 13 additions & 5 deletions src/rudp/partials/RUDP.Client.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Net.Sockets;
using Netly.Interfaces;

namespace Netly
Expand All @@ -10,25 +11,32 @@ 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();
_on = new ClientOn();
_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
{
get => _to.GetOpenTimeout();
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<bool> callback) => _to.StartServerSideConnection(ref callback);
}
}
}
22 changes: 18 additions & 4 deletions src/rudp/partials/RUDP.ClientTo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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);
Expand Down Expand Up @@ -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;
Expand All @@ -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<bool> callback)
{
throw new NotImplementedException();
}
}
}
}
61 changes: 58 additions & 3 deletions src/rudp/partials/RUDP.ServerTo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
}

Expand Down Expand Up @@ -268,6 +268,7 @@ private void ContentUpdate()
if (_contents.Count <= 0) continue;

(Host host, byte[] data) value;
Client client;

lock (_contentsLooker)
{
Expand All @@ -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)
{
Expand Down
2 changes: 1 addition & 1 deletion src/rudp/partials/utils/RUDP.Connection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ public void Send(ref byte[] bytes, MessageType messageType)
{
}

public void Receive(byte[] buffer)
public void InjectBuffer(byte[] buffer)
{
}

Expand Down

0 comments on commit 1f97bf5

Please sign in to comment.