add try catch

This commit is contained in:
Sem van der Hoeven
2020-10-21 22:41:39 +02:00
parent 754c1c5db9
commit e8a72e164f
2 changed files with 51 additions and 36 deletions

View File

@@ -6,6 +6,7 @@ using SharedClientServer;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Net.Sockets;
using System.Text;
using static SharedClientServer.JSONConvert;
@@ -45,7 +46,8 @@ namespace Server.Models
if (ar == null || (!ar.IsCompleted) || (!this.stream.CanRead) || !this.tcpClient.Client.Connected)
return;
try
{
int bytesReceived = this.stream.EndRead(ar);
if (totalBufferReceived + bytesReceived > 1024)
@@ -87,6 +89,13 @@ namespace Server.Models
// start reading for a new message
stream.BeginRead(buffer, 0, buffer.Length, new AsyncCallback(OnRead), null);
}
catch (IOException e)
{
tcpClient.Close();
ServerCommunication.INSTANCE.ServerClientDisconnect(this);
}
}

View File

@@ -97,6 +97,12 @@ namespace Server.Models
}
}
public void ServerClientDisconnect(ServerClient serverClient)
{
// remove from serverclientsinlobbies
// send leave message
}
public void SendToAllExcept(string username, byte[] message)
{
foreach (ServerClient sc in serverClients)