Server handling client disconnects - c#

Below this post, is my ChatApp's client disconnect handler, where it removed the disconnected client from the ListView, it perfectly but the CPU usage suddenly goes up to 50% when the client disconnects. How can I fix it?
Task.Factory.StartNew(() =>
{
while (true)
{
try
{
if (!client.Client.Connected)
{
session.Stop();
session.tcpclient = null;
Clients.Remove(session);
listView1.Invoke((MethodInvoker)(() =>
{
ListViewItem data = new ListViewItem(session.listItems);
listView1.Items.RemoveAt((session.index));
listView1.Refresh();
}));
}
Thread.Sleep(500);
}
catch (Exception)
{
}
}
});
I tried putting it in a Thread but didn't work.

Related

ClientWebSocket.ConnectAsync closing immediately

I'm implementing a small ClientWebSocket to send messages to the IPAddress.Loopback address of the computer so that a browser utility page can pick them up. however as soon as I connect, the client is immediately closed without message, reason, or exception.
ClientWebSocket cws;
Uri address = new Uri("ws://" + IPAddress.Loopback.ToString() + ":80/"
async void Start(){
ServicePointManager.SecurityProtocol = SecurityProtocolType.Ssl3 | SecurityProtocolType.Tls |
SecurityProtocolType.Tls11 | SecurityProtocolType.Tls12;
using (cws = new ClientWebSocket()) {
cws.Options.KeepAliveInterval = new TimeSpan(1, 0, 0);
//Start response listener loop
_ = Task.Run(() => async_ListenerLoop(receiveCancelTokenSrc.Token).ConfigureAwait(false));
try {
//Start the connection
cws.ConnectAsync(address, sendCancelTokenSrc.Token);
} catch(Exception ex) {
MessageBox.Show("Unable to connect.\n\r\n\r" + ex.Message);
}
}
}
async Task async_ListenerLoop(CancellationToken cancelToken) {
ArraySegment<byte> buffer = WebSocket.CreateServerBuffer(RECIEVE_BUFFER);
List<byte> rawData = new List<byte>();
try {
while (state != WebSocketState.Closed && state != WebSocketState.Aborted && !cancelToken.IsCancellationRequested) {
if(state == WebSocketState.Connecting) {
//Sometimes it waits 2-3 cycles, other times it closes immediately
await Task.Delay(500);
continue;
}
WebSocketReceiveResult receiveResult = await cws.ReceiveAsync(buffer, cancelToken);
if (!cancelToken.IsCancellationRequested) {
if (state == WebSocketState.CloseReceived && receiveResult.MessageType == WebSocketMessageType.Close) {
sendCancelTokenSrc.Cancel();
await cws.CloseOutputAsync(WebSocketCloseStatus.NormalClosure, "Acknowledge Close frame", CancellationToken.None);
//The socket state changes to closed at this point
}
if (state == WebSocketState.Open) {
if (receiveResult.MessageType == WebSocketMessageType.Text) {
rawData.AddRange(buffer.Array);
if (receiveResult.EndOfMessage) {
string strData = Encoding.UTF8.GetString(rawData.ToArray(), 0, rawData.Count);
ProcessData(JsonConvert.DeserializeObject<RequestDigest>(strData));
rawData.Clear();
}
} else if (receiveResult.MessageType == WebSocketMessageType.Binary) {
rawData.AddRange(buffer.Array);
if (receiveResult.EndOfMessage) {
ProcessData(rawData.ToArray());
rawData.Clear();
}
}
}
}
}
//Descripion is always null
MessageBox.Show("Connection was closed because '"+cws.CloseStatusDescription+"'");
} catch (OperationCanceledException ex) {
// normal upon task/token cancellation, disregard
MessageBox.Show("Connection to server cancelled.\n\r\n\r" + ex.Message);
} catch (JsonReaderException ex) {
MessageBox.Show("Unable to read incoming message.\n\r\n\r" + ex.Message);
} catch (Exception ex) {
MessageBox.Show("Unable recieve incoming message.\n\r\n\r" + ex.Message);
} finally {
sendCancelTokenSrc.Cancel();
AbortConnection();
}
}
void AbortConnection() {
// don't leave the socket in any potentially connected state
if (state != WebSocketState.Closed) {
cws.Abort();
}
cws.Dispose();
}
There may be other problems with your code (wiring up sockets and clients takes a bit of messing about before you get it right) but there are two glaring problems:
You need to await the result of ConnectAsync(...), to stop the code of Start() continuing synchronously without it:
await cws.ConnectAsync(address, sendCancelTokenSrc.Token);
And you need to make sure that cws does not get disposed until you've finished with it. The using(cws) { ... } block will dispose cws as soon as the code leaves the squiggly braces (which happens straight away currently), even though your listener is still running, and needs it to remain alive.
You can either do that by keeping the code within the using block by awaiting an asynchronous result (such as hint! a final result from your listener) - or by not using using at all and manually disposing cws yourself at the right time.
Also make sure to only start your listener after the connection call has completed, to ensure cws is in a good state.
Something like this:
using(cws = new ClientWebSocket())
{
await cws.ConnectAsync(...);
await DoSomeListeningForABit();
} //cws gets implicitly disposed here
And... your listener will doubtlessly require more work too, but the above will get you past your immediate problem (hopefully!)

Signal R reconnect seems to create a new thread

We have a siglnalR hub hosted in IIS, and a WPF .net core application that connects. Everything is working perfectly on first run. However, when IIS recycles the application pool, the WPF client re-reconnects successfully, but, (so it seems) on another thread, as when the user attempts to perform an action (open a new WPF window) - the following error is thrown when creating a new instance of the window to open :-
"The calling thread must be STA, because many UI components require this"
This is how we connect to the hub :-
private async void Connect()
{
try
{
_signalRConnection.On<Notification>(NotificationMessageStr, (message) =>
{
if (message != null && _signalRConnection != null)
{
OnProcessMessage(message);
}
}
);
_signalRConnection.Reconnecting += error =>
{
OnReconnecting("Connection lost - Attempting to reconnect.");
return Task.CompletedTask;
};
_signalRConnection.Reconnected += connectionId =>
{
OnReconnected("Reconnected");
return Task.CompletedTask;
};
_signalRConnection.Closed += error =>
{
OnLostConnection("Failed to connect");
// Notify users the connection has been closed or manually try to restart the connection.
return Task.CompletedTask;
};
try
{
//Connect to the server
await _signalRConnection.StartAsync();
}
catch (Exception ex)
{
}
}
catch (Exception ex)
{
}
}
When a message is received from the hub, we call :-
private void SubscriveToNewNotification()
{
vm.NewNotification += (sender, e) => {
ShowNotificationAlert(e.NotificationMessage); };
}
private void ShowNotificationAlert(Notification notification) {
NotificationAlert notificationAlert = new NotificationAlert();
notificationAlert.notification = notification;
notificationAlert.Show();
}
And it is this:-
NotificationAlert notificationAlert = new NotificationAlert();
That is failing.
This is how the connection is built up :-
private void InitializeViewModel()
{
try
{
string serviceAddress = "xxxx/notificationHub";
connectHub = NotificationHubManager.CreateNotificationHub(serviceAddress, userInfo);
}
catch (System.Exception ex)
{
System.Windows.MessageBox.Show(ex.Message + "--");
}
connectHub.ProcessMessage += (sender, e) =>
{
// THIS IS WHERE IT FALLS OVER
NotificationAlert n = new NotificationAlert();
OnNotificationReceived(e.NotificationMessage);
};
-- This is the notification hub
public static NotificationHubConnect CreateNotificationHub(string address, ISwiftUser userInfo = null)
{
HubConnection hubConnection = new HubConnectionBuilder()
.WithUrl(address)
.WithAutomaticReconnect()
.Build();
try
{
var result = new NotificationHubConnect(hubConnection, userInfo);
return result;
}
catch (Exception ex)
{
throw ex;
}
}
Is there a way to have the reconnect run on the same thread?

C# - Signalr client connection is causing performance issue Windows Service

Signalr client code is causing performance issue is Windows Service. Windows service is acting as signalr client.
What we are trying to do:
Windows service is having one timer, which executes the method(ConnectToSignalRServer). If somehow the connection gets closed, there is one event (Connection.Closed += Connection_Closed), which will again try to establish the connection by calling the method(ConnectToSignalRServer). A while loop is being used in the event (Connection.Closed += Connection_Closed) to try reconnecting.
Please find the sample code below and let me know if any issues with the code.
private static HubConnection Connection = null;
//When the service starts, this method would be called.
public static bool ConnectToSignalRServer()
{
try
{
string Url = "http://www.samplesignalrserver.com";
Connection = new HubConnection(Url);
var myHub = Connection.CreateHubProxy("SignalHub");
Connection.Start().ContinueWith(task =>
{
if (task.IsFaulted)
{
}
else
{
Connection.Closed += Connection_Closed;
}
}).Wait();
//Method(RequestData) would be called upon receiveng message from server
myHub.On<string>("GetMessgeFromServer", type =>
{
Task.Run(() => RequestData(type));
});
//Method(GetHostName) would be called in server
myHub.Invoke<string>("GetHostName", BLConstants.strHostName);
return true;
}
catch (Exception ex)
{
//capturing Stacktrace and Message from ex object
}
return false;
}
//Establish the connection, if the the connection would be closed
private static void Connection_Closed()
{
try
{
while (true)
{
Thread.Sleep(10000);
bool connected = ConnectToSignalRServer();
if (connected)
{
break;
}
}
}
catch (Exception ex)
{
//capturing Stacktrace and Message from ex object
}
}

Implement UDP communication without blocking UI

I currently have a partly synchronous (poor) implementation of UDP communication between my android App and a hardware which is broadcasting UDP packets. The App continuously polls the hardware for status information which then is used to update the UI. The App also has various screens, each requesting (only when user switches screens, not continuous) a different set of configuration information. The user can also make changes to the configurations and load them to the hardware. All this while, the status updates keeps running in the background. I am looking for a solution best suited to my scenario.
Here is what I have done so far (simplified to make it more readable)
void InitializeUDP()
{
udpClient = new UdpClient(15001);
sender = default(IPEndPoint);
ThreadPool.QueueUserWorkItem(o => UDP_StatusCommunicator());
udpClient.EnableBroadcast = true;
udpClient.Client.ReceiveTimeout = 500;
}
void UDP_StatusCommunicator()
{
while (true)
{
if (update_flag)
{
try
{
sent_packet = FrameGenerator(frame_Queue[screen], true); //Creates UDP Packet
//CheckQuery(sent_packet);
udpClient.Send(sent_packet, sent_packet.Length,"192.168.4.255", 15000);
received_packet = udpClient.Receive(ref sender);
//CheckResponse(received_packet);
RunOnUiThread(() =>
{
Update_UI(received_packet);
});
}
catch (SocketException e)
{
Console.Writeline("Socket Timeout: " + e);
}
}
Thread.Sleep(update_delay);
}
}
void UDPReadWrite(int screen, bool reading)
{
SelectFunctionQueue(screen); //Select the frames according to the screen selected
//CheckQueue(frame_Queue);
for (int i = 0; i < frame_Queue.Length; i++)
{
try
{
sent_packet = FrameGenerator(frame_Queue[i], reading);
//CheckQuery(sent_packet);
udpClient.Send(sent_packet, sent_packet.Length, "192.168.4.255", 15000);
received_packet = udpClient.Receive(ref sender);
//CheckResponse(received_packet);
if (sent_packet[2] == received_packet[2]) //Verify correct packet received
{
Update_UI(received_packet);
}
else
{
i--; //retry
}
}
catch (SocketException e)
{
Console.WriteLine("Socket Timeout: " e);
i--;
}
}
}
}
void Switch_Screen(int new_screen)
{
update_flag = false;
UDPReadWrite(new_screen, true)
update_flag = true;
}
void User_Config_Write(int screen, byte[] data)
{
update_flag = false;
Update_Payload(data);
UDPReadWrite(screen, false)
update_flag = true;
}
As you would have clearly noticed, this is a very flawed implementation. I keep running into issues like UI freeze, same socket usage being attempted by two threads simultaneously, stuck while waiting for packets. I have tried to use 'async await' but I am not implementing it correctly resulting in race conditions and what not. Any help would be appreciated
Update : After some research and testing I have found the below to be working satisfactorily. However, I would appreciate if someone could just verify whether it has been done correctly
UdpClient udpClient = new UdpClient();
UdpClient r_UdpClient = new UdpClient(15001);
IPEndPoint sender = default(IPEndPoint);
ManualResetEventSlim receive = new ManualResetEventSlim(true);
Task.Run(() => UDP_Transmit());
async void UDP_Transmit()
{
byte[] frame;
SelectFrameQueue(selector);
udpClient = new UdpClient(15001);
udpClient.EnableBroadcast = true;
udpClient.BeginReceive(new AsyncCallback(UDP_Receive), udpClient);
while (true)
{
for (int i = 0; i < frame_Queue.Length; i++)
{
frame = FrameGenerator(frame_Queue[i]); //Generates Frames
try
{
udpClient.Send(frame, frame.Length, "192.168.4.255", 15000);
}
catch (SocketException)
{
Log.Debug("Error", "Socket Exception");
}
if(!receive.Wait(10000)) //Receive Timeout
{
RunOnUiThread(() =>
{
ShowToast("Connection Timeout. Please check device");
});
};
await Task.Delay(update_delay); //To release pressure from H/W
receive.Reset();
}
}
}
void UDP_Receive(IAsyncResult result)
{
receive.Set();
r_UdpClient = result.AsyncState as UdpClient;
data = r_UdpClient.EndReceive(result, ref sender);
RunOnUiThread(() =>
{
Update_UI(data);
});
r_UdpClient.BeginReceive(new AsyncCallback(UDP_Receive), r_UdpClient);
}
I don't know what the intent of this code is:
void InitializeUDP()
{
udpClient = new UdpClient(15001);
sender = default(IPEndPoint);
ThreadPool.QueueUserWorkItem(o => UDP_StatusCommunicator());
udpClient.EnableBroadcast = true;
udpClient.Client.ReceiveTimeout = 500;
}
but it is not guaranteed that
udpClient.EnableBroadcast = true;
udpClient.Client.ReceiveTimeout = 500;
is executed before UDP_StatusCommunicator().
For client UIs like Xamarin Task.Run can be a good option over ThreadPool.QueueUserWorkItem.
You might want to take a look at Dataflow (Task Parallel Library), in particular to the ActionBlock to replace your queue.
You might also want to consider using Progress to report updates to the UI or using Reactive Extensions (Rx) to subscribe to updates from the UI.

C# Sockets: 2nd (and more) users can't connect

I connect to my (C#) server and from an App built in Corona SDK but for the second person can never connect.
I have tried using different IP's i.e. two cellphones with external IP's with no difference.
This is how my server listener works:
server.cs
void Listener()
{
while (isRunning)
{
try
{
Socket socket = listener.AcceptSocket();
foreach (var worker in workers)
if (worker.IsAvailable)
{
worker.ProcessNewSocket(socket);
break;
}
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
}
}
}
public void Run()
{
Console.WriteLine("Server.Run()");
listener.Start();
isRunning = true;
foreach (var worker in workers)
{
Thread t = new Thread(worker.Run);
t.Start();
}
Listener();
}
ServerWorker.cs
public void ProcessNewSocket(Socket socket)
{
var pc = new PlayerClient(this);
sockets.Add(socket, pc);
}
// this method will be called in cycle
public void Run()
{
while (server.IsRunning)
{
List<Socket> readList = sockets.Keys.ToList(); //List of sockets that have some data from client available for reading.
List<Socket> writeList = sockets.Keys.ToList(); //List of sockets that are ready to write (send) to the client. An action was made to a table and the change was sent to PlayerClient.Write and is now stored in the queue (MemoreStream)
List<Socket> errorList = sockets.Keys.ToList();
if (readList.Count() != 0 || writeList.Count() != 0 || errorList.Count() != 0)
{
// for providing security you can use System.Net.Security.SslStream here when read/write data,
// see http://msdn.microsoft.com/ru-ru/library/system.net.security.sslstream(v=vs.110).aspx
Socket currentSocket = null;
// foreach socket with events
try
{
foreach (var s in readList)
{
currentSocket = s;
//TODO: Get the actual length of the message.
byte[] data = new byte[2048];
s.Receive(data);
sockets[s].OnData(data);
}
foreach (var s in writeList)
{
currentSocket = s;
if (sockets[s].IsWriteDataAvailable())
{
s.Send(sockets[s].GetWriteBuffer());
sockets[s].ClearWriteBuffer();
}
}
foreach (var s in errorList)
{
//OnError
}
}
// we got exception, depending on the type...
catch (SocketException ex)
{
//Console.WriteLine(ex.ToString());
// send client error message, this is not always possible(for example network failure)
// maybe we would like to notify his opponent about connection failure
// terminate connection
if (ex.ErrorCode == (int)SocketError.ConnectionAborted || ex.ErrorCode == (int)SocketError.ConnectionReset)
RemoveSocket(currentSocket);
else
Console.WriteLine("Other problem .. " + ex.ErrorCode.ToString());
}
}
}
}
I'm new in network programming so I'm not really sure what to do. I have read about using ASync but first I would like to know if there is something I can do with this code and/or if I should change it completely?
I think the "BREAK" statement in your Listener() Block;
foreach (var worker in workers)
if (worker.IsAvailable)
{
worker.ProcessNewSocket(socket);
break; // this BREAK WILL END YOUR loop AFTER first CLIENT FOUND.
}
So, try removing break as;
foreach (var worker in workers)
{
if (worker.IsAvailable)
{
worker.ProcessNewSocket(socket);
}
}

Categories

Resources