Skip to content
This repository has been archived by the owner on Dec 20, 2019. It is now read-only.

Commit

Permalink
Removing connection relaying; client auto-reconnect; auto-object disp…
Browse files Browse the repository at this point in the history
…osition on connection close.
  • Loading branch information
dajuric committed Oct 13, 2017
1 parent 3c9b8cc commit c2604b4
Show file tree
Hide file tree
Showing 18 changed files with 46 additions and 358 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -41,3 +41,4 @@ bld/



/legacy-materials
4 changes: 2 additions & 2 deletions Deploy/Nuget/Build.cmd
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
:: timeout /T 5

:: settings
set nugetPath=%cd%\..\.nuget
set version=0.5.0
set nugetPath=%cd%\..\..\.nuget
set version=0.5.1
set output=%cd%\bin

:: Create output directory
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ There is only one relevant method: **Bind** for binding object/interface onto co

## Sample

To scratch the surface... *(RPC in both directions, connection relaying, multi-service, .NET clients)*
To scratch the surface... *(RPC in both directions, multi-service, .NET clients)*

**Server**
``` csharp
Expand Down

This file was deleted.

29 changes: 0 additions & 29 deletions Samples/RelayConnectionSample/BackgroundService/Program.cs

This file was deleted.

This file was deleted.

40 changes: 0 additions & 40 deletions Samples/RelayConnectionSample/FrontendService/Program.cs

This file was deleted.

35 changes: 0 additions & 35 deletions Samples/RelayConnectionSample/FrontendService/Site/Index.html

This file was deleted.

18 changes: 0 additions & 18 deletions Samples/RelayConnectionSample/Run.bat

This file was deleted.

2 changes: 1 addition & 1 deletion Samples/ServerClientSample/Client/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ static void Main(string[] args)
})
.Wait(0);

Console.Write("Running: '{0}'. Press [Enter] to exit.", nameof(TestClient));
Console.Write("Running: '{0}'. Press [Enter] to exit.\n", nameof(TestClient));
Console.ReadLine();
cts.Cancel();
}
Expand Down
32 changes: 31 additions & 1 deletion Source/WebsocketRPC/ClientServer/Client.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,37 @@ public static class Client
/// <param name="token">Cancellation token.</param>
/// <param name="onConnection">Action executed when connection is established.</param>
/// <param name="setOptions">Websocket option set method.</param>
/// <param name="reconnectOnError">True to reconnect on error, false otherwise.</param>
/// <param name="secondsBetweenReconnect">The number of seconds between two reconnect attempts.</param>
/// <returns>Client task.</returns>
public static async Task ConnectAsync(string uri, CancellationToken token, Action<Connection> onConnection, Action<ClientWebSocketOptions> setOptions = null)
/// <exception cref="Exception">Socket connection exception thrown in case when <paramref name="secondsBetweenReconnect"/> is set to false.</exception>
public static async Task ConnectAsync(string uri, CancellationToken token, Action<Connection> onConnection, Action<ClientWebSocketOptions> setOptions = null,
bool reconnectOnError = true, int secondsBetweenReconnect = 0)
{
var isClosedSuccessfully = true;

do
{
try
{
isClosedSuccessfully = await connectAsync(uri, token, onConnection, setOptions);
}
catch (Exception ex)
{
isClosedSuccessfully = false;
if (!reconnectOnError) throw ex;
}

if (!isClosedSuccessfully && reconnectOnError)
await Task.Delay(TimeSpan.FromSeconds(secondsBetweenReconnect));
}
while (!isClosedSuccessfully && reconnectOnError);
}

static async Task<bool> connectAsync(string uri, CancellationToken token, Action<Connection> onConnection, Action<ClientWebSocketOptions> setOptions = null)
{
ClientWebSocket webSocket = null;
var isClosedSuccessfully = true;

try
{
Expand All @@ -68,8 +95,11 @@ public static async Task ConnectAsync(string uri, CancellationToken token, Actio
}
finally
{
isClosedSuccessfully = webSocket.State != WebSocketState.Aborted;
webSocket?.Dispose();
}

return isClosedSuccessfully;
}
}
}
1 change: 1 addition & 0 deletions Source/WebsocketRPC/ClientServer/Connection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,7 @@ internal static async Task ListenReceiveAsync(Connection connection, Cancellatio
{
while (ex.InnerException != null) ex = ex.InnerException;
connection.OnError?.Invoke(ex);
//socket will be aborted -> no need to close manually
}
}
}
Expand Down
6 changes: 6 additions & 0 deletions Source/WebsocketRPC/ConnectionBinders/LocalBinder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
// OTHER DEALINGS IN THE SOFTWARE.
#endregion

using System;
using static WebsocketRPC.RPCSettings;

namespace WebsocketRPC
Expand All @@ -48,6 +49,11 @@ public LocalBinder(Connection connection, TObj obj)
var result = await lInvoker.InvokeAsync(Object, msg);
await Connection.SendAsync(result.ToJson(), Encoding);
};

Connection.OnClose += () =>
{
(Object as IDisposable)?.Dispose();
};
}

public TObj Object { get; private set; }
Expand Down
Loading

0 comments on commit c2604b4

Please sign in to comment.