Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Issue 26 - Fix and catch channel and reconnect exceptions #33

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 43 additions & 16 deletions ScClient/Socket.cs
Original file line number Diff line number Diff line change
Expand Up @@ -66,26 +66,38 @@ public void SetSslCertVerification(bool value)

public Channel CreateChannel(string name)
{
var channel = new Channel(this, name);
_channels.Add(channel);
return channel;
lock (_channels)
{
var channel = new Channel(this, name);
_channels.Add(channel);
return channel;
}
}

public List<Channel> GetChannels()
{
return _channels;
lock (_channels)
{
return _channels;
}
}

public Channel GetChannelByName(string name)
{
return _channels.FirstOrDefault(channel => channel.GetChannelName().Equals(name));
lock (_channels)
{
return _channels.FirstOrDefault(channel => channel.GetChannelName().Equals(name));
}
}

private void SubscribeChannels()
{
foreach (var channel in _channels)
lock (_channels)
{
channel.Subscribe();
foreach (var channel in _channels)
{
channel.Subscribe();
}
}
}

Expand Down Expand Up @@ -127,7 +139,7 @@ private void OnWebsocketClosed(object sender, EventArgs e)
_listener.OnDisconnected(this);
if (!_strategy.AreAttemptsComplete())
{
new Thread(Reconnect).Start();
new Thread(ReconnectThread).Start();
}
}

Expand Down Expand Up @@ -230,11 +242,20 @@ public void Connect()
_socket.Open();
}

private void Reconnect()
private void ReconnectThread()
{
_strategy.ProcessValues();
Thread.Sleep(_strategy.GetReconnectInterval());
Connect();
// Catch any exception, we can't have a thread that we have no control over
// crashing our applications
try
{
_strategy.ProcessValues();
Thread.Sleep(_strategy.GetReconnectInterval());
Connect();
}
catch (Exception ex)
{
//System.Diagnostics.Trace.WriteLine(ex);
}
}

public void Disconnect()
Expand Down Expand Up @@ -402,14 +423,20 @@ public void Publish(object data, Ackcall ack)

public void Unsubscribe()
{
_socket.Unsubscribe(_channelname);
_socket._channels.Remove(this);
lock (_socket._channels)
{
_socket.Unsubscribe(_channelname);
_socket._channels.Remove(this);
}
}

public void Unsubscribe(Ackcall ack)
{
_socket.Unsubscribe(_channelname, ack);
_socket._channels.Remove(this);
lock (_socket._channels)
{
_socket.Unsubscribe(_channelname, ack);
_socket._channels.Remove(this);
}
}

public string GetChannelName()
Expand Down