Client: Improve disconnection detection

This commit is contained in:
Tal Aloni 2023-10-27 14:30:20 +03:00
parent 3a7e36b6d1
commit 660442e778
4 changed files with 18 additions and 6 deletions

View file

@ -688,14 +688,14 @@ namespace SMBLibrary.Client
}
}
public static void TrySendMessage(Socket socket, SMB1Message message)
private void TrySendMessage(Socket socket, SMB1Message message)
{
SessionMessagePacket packet = new SessionMessagePacket();
packet.Trailer = message.GetBytes();
TrySendPacket(socket, packet);
}
public static void TrySendPacket(Socket socket, SessionPacket packet)
private void TrySendPacket(Socket socket, SessionPacket packet)
{
try
{
@ -704,9 +704,11 @@ namespace SMBLibrary.Client
}
catch (SocketException)
{
m_isConnected = false;
}
catch (ObjectDisposedException)
{
m_isConnected = false;
}
}
}

View file

@ -1,4 +1,4 @@
/* Copyright (C) 2014-2019 Tal Aloni <tal.aloni.il@gmail.com>. All rights reserved.
/* Copyright (C) 2014-2023 Tal Aloni <tal.aloni.il@gmail.com>. All rights reserved.
*
* You can redistribute this program and/or modify it under the terms of
* the GNU Lesser Public License as published by the Free Software Foundation,
@ -560,6 +560,10 @@ namespace SMBLibrary.Client
private void TrySendMessage(SMB1Command request)
{
if (!m_client.IsConnected)
{
throw new InvalidOperationException("The client is no longer connected");
}
m_client.TrySendMessage(request, m_treeID);
}

View file

@ -640,7 +640,7 @@ namespace SMBLibrary.Client
}
}
public static void TrySendCommand(Socket socket, SMB2Command request, byte[] encryptionKey)
private void TrySendCommand(Socket socket, SMB2Command request, byte[] encryptionKey)
{
SessionMessagePacket packet = new SessionMessagePacket();
if (encryptionKey != null)
@ -655,7 +655,7 @@ namespace SMBLibrary.Client
TrySendPacket(socket, packet);
}
public static void TrySendPacket(Socket socket, SessionPacket packet)
private void TrySendPacket(Socket socket, SessionPacket packet)
{
try
{
@ -664,9 +664,11 @@ namespace SMBLibrary.Client
}
catch (SocketException)
{
m_isConnected = false;
}
catch (ObjectDisposedException)
{
m_isConnected = false;
}
}
}

View file

@ -1,4 +1,4 @@
/* Copyright (C) 2017-2021 Tal Aloni <tal.aloni.il@gmail.com>. All rights reserved.
/* Copyright (C) 2017-2023 Tal Aloni <tal.aloni.il@gmail.com>. All rights reserved.
*
* You can redistribute this program and/or modify it under the terms of
* the GNU Lesser Public License as published by the Free Software Foundation,
@ -335,6 +335,10 @@ namespace SMBLibrary.Client
private void TrySendCommand(SMB2Command request)
{
request.Header.TreeID = m_treeID;
if (!m_client.IsConnected)
{
throw new InvalidOperationException("The client is no longer connected");
}
m_client.TrySendCommand(request, m_encryptShareData);
}