Skip to content

Commit 4369378

Browse files
committed
Merge pull request #61 from Code-Sharp/reconnect
Reconnect mechanism
2 parents 74a8c46 + 53c9c43 commit 4369378

File tree

9 files changed

+125
-14
lines changed

9 files changed

+125
-14
lines changed

.travis.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,4 @@ script:
1414

1515
branches:
1616
only:
17-
- develop
17+
- develop

README.md

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
WampSharp
22
=========
3-
3+
[![NuGet Version][NuGetImgMaster]][NuGetLinkMaster]
44

55
A C# implementation of [WAMP (The Web Application Messaging Protocol)][WampLink]
66

@@ -15,6 +15,11 @@ Master | Provider
1515
[![Build Status][WinImgMaster]][WinLinkMaster] | Windows CI Provided By [CodeBetter][] and [JetBrains][]
1616
[![Build Status][MonoImgMaster]][MonoLinkMaster] | Mono CI Provided by [travis-ci][]
1717

18+
19+
## WampSharp v1.2.1.6-beta
20+
21+
WampSharp v1.2.1.6-beta released, see version [release notes](https://github.com/Code-Sharp/WampSharp/wiki/WampSharp-v1.2.1.6-beta-release-notes).
22+
1823
## Get Started
1924

2025
See [Get started tutorial](https://github.com/Code-Sharp/WampSharp/wiki/Getting-started-with-WAMPv2) and
@@ -32,6 +37,12 @@ WAMPv1 support is still available. You can read about it at the [Wiki](https://g
3237

3338
If you're updating from a previous WampSharp version and you're not interested yet in updating your application to WAMPv2, please read the following [notes](https://github.com/Code-Sharp/WampSharp/wiki/Notes-for-WAMPv1-users).
3439

40+
## Donations
41+
42+
If you found WampSharp helpful and want to donate, you are welcome to do so via [PayPal](https://www.paypal.com/cgi-bin/webscr?cmd=_s-xclick&hosted_button_id=UHRAS9KZPNPX4).
43+
44+
Your donations help keep this project development alive.
45+
3546
[WampLink]:http://wamp.ws
3647

3748
[WinImgMaster]:http://teamcity.codebetter.com/app/rest/builds/buildType:\(id:WampSharp_Dev_Build\)/statusIcon

releasenotes.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
WampSharp v1.2.1.0-beta release notes
1+
WampSharp v1.2.1.6-beta release notes
22
=================================
33

44
**Contents**

src/mono/WampSharp/WampSharp.csproj

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -597,6 +597,9 @@
597597
<Compile Include="..\..\net45\WampSharp\Core\Utilities\AsyncDisposable\IAsyncDisposable.cs">
598598
<Link>Core\Utilities\AsyncDisposable\IAsyncDisposable.cs</Link>
599599
</Compile>
600+
<Compile Include="..\..\net45\WampSharp\WAMP2\V2\Api\WampChannelReconnector.cs">
601+
<Link>WAMP2\V2\Api\WampChannelReconnector.cs</Link>
602+
</Compile>
600603
<Compile Include="..\..\net45\WampSharp\WAMP2\V2\Client\Session\WampAuthenticationNotImplementedException.cs">
601604
<Link>WAMP2\V2\Client\Session\WampAuthenticationNotImplementedException.cs</Link>
602605
</Compile>

src/net40/WampSharp/WampSharp.csproj

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -601,6 +601,9 @@
601601
<Compile Include="..\..\net45\WampSharp\Core\Utilities\AsyncDisposable\IAsyncDisposable.cs">
602602
<Link>Core\Utilities\AsyncDisposable\IAsyncDisposable.cs</Link>
603603
</Compile>
604+
<Compile Include="..\..\net45\WampSharp\WAMP2\V2\Api\WampChannelReconnector.cs">
605+
<Link>WAMP2\V2\Api\WampChannelReconnector.cs</Link>
606+
</Compile>
604607
<Compile Include="..\..\net45\WampSharp\WAMP2\V2\Client\Session\WampAuthenticationNotImplementedException.cs">
605608
<Link>WAMP2\V2\Client\Session\WampAuthenticationNotImplementedException.cs</Link>
606609
</Compile>

src/net45/WampSharp.Default/WebSocket4Net/WebSocket4NetConnection.cs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,6 @@ private void WebSocketOnClosed(object sender, EventArgs eventArgs)
6363
private void WebSocketOnError(object sender, ErrorEventArgs e)
6464
{
6565
RaiseConnectionError(e.Exception);
66-
RaiseConnectionClosed();
6766
}
6867

6968
public void Connect()
@@ -76,6 +75,17 @@ public virtual void Dispose()
7675
mWebSocket.Close();
7776
}
7877

78+
void IWampConnection<TMessage>.Send(WampMessage<TMessage> message)
79+
{
80+
try
81+
{
82+
Send(message);
83+
}
84+
catch (Exception ex)
85+
{
86+
}
87+
}
88+
7989
public abstract void Send(WampMessage<TMessage> message);
8090

8191
public event EventHandler ConnectionOpen;
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
using System;
2+
using System.Reactive;
3+
using System.Reactive.Disposables;
4+
using System.Reactive.Linq;
5+
using System.Threading.Tasks;
6+
using WampSharp.V2.Client;
7+
using WampSharp.V2.Realm;
8+
9+
namespace WampSharp.V2
10+
{
11+
/// <summary>
12+
/// An object that reconnects to realm on connection loss.
13+
/// </summary>
14+
public class WampChannelReconnector : IDisposable
15+
{
16+
private IObservable<Unit> mMerged;
17+
private IDisposable mDisposable = Disposable.Empty;
18+
private bool mStarted = false;
19+
private readonly object mLock = new object();
20+
21+
/// <summary>
22+
/// Initializes a new instance of <see cref="WampChannelReconnector"/>.
23+
/// </summary>
24+
/// <param name="channel">The channel used to connect.</param>
25+
/// <param name="connector">The Task to use in order to connect.</param>
26+
public WampChannelReconnector(IWampChannel channel, Func<Task> connector)
27+
{
28+
IWampClientConnectionMonitor monitor = channel.RealmProxy.Monitor;
29+
30+
var connectionBrokenObservable =
31+
Observable.FromEventPattern<WampSessionCloseEventArgs>
32+
(x => monitor.ConnectionBroken += x,
33+
x => monitor.ConnectionBroken -= x)
34+
.Select(x => Unit.Default);
35+
36+
var onceAndConnectionBroken =
37+
Observable.Return(Unit.Default).Concat
38+
(connectionBrokenObservable);
39+
40+
IObservable<IObservable<Unit>> reconnect =
41+
from connectionBroke in onceAndConnectionBroken
42+
let tryReconnect = Observable.FromAsync(connector)
43+
.Catch<Unit, Exception>(x => Observable.Empty<Unit>())
44+
select tryReconnect;
45+
46+
mMerged = reconnect.Concat();
47+
}
48+
49+
/// <summary>
50+
/// Start trying connection establishment to router.
51+
/// </summary>
52+
public void Start()
53+
{
54+
lock (mLock)
55+
{
56+
if (mStarted)
57+
{
58+
throw new Exception("Already started");
59+
}
60+
else
61+
{
62+
if (mMerged != null)
63+
{
64+
mDisposable = mMerged.Subscribe(x => { });
65+
mStarted = true;
66+
}
67+
else
68+
{
69+
throw new ObjectDisposedException(typeof (WampChannelReconnector).Name);
70+
}
71+
}
72+
}
73+
}
74+
75+
public void Dispose()
76+
{
77+
lock (mLock)
78+
{
79+
mMerged = null;
80+
mDisposable.Dispose();
81+
}
82+
}
83+
}
84+
}

src/net45/WampSharp/WAMP2/V2/Client/Session/WampSessionClient.cs

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -110,8 +110,7 @@ public void Challenge(string authMethod, ChallengeDetails extra)
110110
public void Welcome(long session, TMessage details)
111111
{
112112
mSession = session;
113-
mOpenTask.SetResult(true);
114-
mOpenTask = null;
113+
mOpenTask.TrySetResult(true);
115114

116115
OnConnectionEstablished(new WampSessionEventArgs
117116
(session, new SerializedValue<TMessage>(mFormatter, details)));
@@ -173,11 +172,11 @@ public IWampRealmProxy Realm
173172
}
174173

175174
public Task OpenTask
176-
{
177-
get
178-
{
179-
return mOpenTask.Task;
180-
}
175+
{
176+
get
177+
{
178+
return mOpenTask.Task;
179+
}
181180
}
182181

183182
public void Close(string reason, object details)
@@ -242,9 +241,9 @@ private void SetOpenTaskErrorIfNeeded(Exception exception)
242241
mOpenTask = new TaskCompletionSource<bool>();
243242
}
244243

245-
if (openTask != null)
246-
{
247-
openTask.SetException(exception);
244+
if (openTask != null)
245+
{
246+
openTask.TrySetException(exception);
248247
}
249248
}
250249

src/net45/WampSharp/WampSharp.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -243,6 +243,7 @@
243243
<Compile Include="WAMP2\V2\Api\DelegatePubSub\WampSubscriberRegistrar.cs" />
244244
<Compile Include="WAMP2\V2\Api\WampEventContext.cs" />
245245
<Compile Include="Core\Utilities\AsyncDisposable\IAsyncDisposable.cs" />
246+
<Compile Include="WAMP2\V2\Api\WampChannelReconnector.cs" />
246247
<Compile Include="WAMP2\V2\Client\Session\WampAuthenticationNotImplementedException.cs" />
247248
<Compile Include="WAMP2\V2\PubSub\Subscriber\LocalSubscriber.cs" />
248249
<Compile Include="WAMP2\V2\PubSub\Subscriber\MethodInfoSubscriber.cs" />

0 commit comments

Comments
 (0)