add try catch
This commit is contained in:
@@ -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,48 +46,56 @@ namespace Server.Models
|
||||
if (ar == null || (!ar.IsCompleted) || (!this.stream.CanRead) || !this.tcpClient.Client.Connected)
|
||||
return;
|
||||
|
||||
|
||||
int bytesReceived = this.stream.EndRead(ar);
|
||||
|
||||
if (totalBufferReceived + bytesReceived > 1024)
|
||||
try
|
||||
{
|
||||
throw new OutOfMemoryException("buffer is too small!");
|
||||
}
|
||||
int bytesReceived = this.stream.EndRead(ar);
|
||||
|
||||
// copy the received bytes into the buffer
|
||||
Array.Copy(buffer, 0, totalBuffer, totalBufferReceived, bytesReceived);
|
||||
// add the bytes we received to the total amount
|
||||
totalBufferReceived += bytesReceived;
|
||||
|
||||
// calculate the expected length of the message
|
||||
int expectedMessageLength = BitConverter.ToInt32(totalBuffer, 0);
|
||||
|
||||
while (totalBufferReceived >= expectedMessageLength)
|
||||
{
|
||||
// we have received the full packet
|
||||
byte[] message = new byte[expectedMessageLength];
|
||||
// copy the total buffer contents into the message array so we can pass it to the handleIncomingMessage method
|
||||
Array.Copy(totalBuffer, 0, message, 0, expectedMessageLength);
|
||||
HandleIncomingMessage(message);
|
||||
|
||||
// move the contents of the totalbuffer to the start of the array
|
||||
Array.Copy(totalBuffer, expectedMessageLength, totalBuffer, 0, (totalBufferReceived - expectedMessageLength));
|
||||
|
||||
// remove the length of the expected message from the total buffer
|
||||
totalBufferReceived -= expectedMessageLength;
|
||||
// and set the new expected length to the rest that is still in the buffer
|
||||
expectedMessageLength = BitConverter.ToInt32(totalBuffer, 0);
|
||||
|
||||
if (expectedMessageLength == 0)
|
||||
if (totalBufferReceived + bytesReceived > 1024)
|
||||
{
|
||||
break;
|
||||
}
|
||||
throw new OutOfMemoryException("buffer is too small!");
|
||||
}
|
||||
|
||||
// copy the received bytes into the buffer
|
||||
Array.Copy(buffer, 0, totalBuffer, totalBufferReceived, bytesReceived);
|
||||
// add the bytes we received to the total amount
|
||||
totalBufferReceived += bytesReceived;
|
||||
|
||||
// calculate the expected length of the message
|
||||
int expectedMessageLength = BitConverter.ToInt32(totalBuffer, 0);
|
||||
|
||||
while (totalBufferReceived >= expectedMessageLength)
|
||||
{
|
||||
// we have received the full packet
|
||||
byte[] message = new byte[expectedMessageLength];
|
||||
// copy the total buffer contents into the message array so we can pass it to the handleIncomingMessage method
|
||||
Array.Copy(totalBuffer, 0, message, 0, expectedMessageLength);
|
||||
HandleIncomingMessage(message);
|
||||
|
||||
// move the contents of the totalbuffer to the start of the array
|
||||
Array.Copy(totalBuffer, expectedMessageLength, totalBuffer, 0, (totalBufferReceived - expectedMessageLength));
|
||||
|
||||
// remove the length of the expected message from the total buffer
|
||||
totalBufferReceived -= expectedMessageLength;
|
||||
// and set the new expected length to the rest that is still in the buffer
|
||||
expectedMessageLength = BitConverter.ToInt32(totalBuffer, 0);
|
||||
|
||||
if (expectedMessageLength == 0)
|
||||
{
|
||||
break;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
// start reading for a new message
|
||||
stream.BeginRead(buffer, 0, buffer.Length, new AsyncCallback(OnRead), null);
|
||||
|
||||
}
|
||||
// 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);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
@@ -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)
|
||||
|
||||
Reference in New Issue
Block a user