|
|
|
@ -12,9 +12,10 @@ namespace ConsoleApp.TelnetSamples |
|
|
|
|
{ |
|
|
|
|
internal class AsyncSocketTelnetClient : IAsyncTelnetClient |
|
|
|
|
{ |
|
|
|
|
private readonly CancellationTokenSource CTS = new CancellationTokenSource(); |
|
|
|
|
private CancellationTokenSource _cts; |
|
|
|
|
|
|
|
|
|
public event EventHandler<string> MessageCallback; |
|
|
|
|
public event EventHandler<Exception> ErrorCallback; |
|
|
|
|
|
|
|
|
|
private Socket _socket; |
|
|
|
|
|
|
|
|
@ -22,7 +23,7 @@ namespace ConsoleApp.TelnetSamples |
|
|
|
|
{ |
|
|
|
|
try |
|
|
|
|
{ |
|
|
|
|
Close(); |
|
|
|
|
//Close(); |
|
|
|
|
|
|
|
|
|
_socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); |
|
|
|
|
_socket.ReceiveTimeout = 1000; |
|
|
|
@ -31,11 +32,13 @@ namespace ConsoleApp.TelnetSamples |
|
|
|
|
IPEndPoint iPEndPoint = new IPEndPoint(ipAddress, port); |
|
|
|
|
await _socket.ConnectAsync(iPEndPoint); |
|
|
|
|
|
|
|
|
|
Task.Run(ReadAsync); |
|
|
|
|
_cts = new CancellationTokenSource(); |
|
|
|
|
Task.Run(ReadAsync, _cts.Token); |
|
|
|
|
} |
|
|
|
|
catch (Exception) |
|
|
|
|
catch (Exception ex) |
|
|
|
|
{ |
|
|
|
|
throw; |
|
|
|
|
if (this.ErrorCallback != null) |
|
|
|
|
this.ErrorCallback(this, ex); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
@ -43,14 +46,21 @@ namespace ConsoleApp.TelnetSamples |
|
|
|
|
{ |
|
|
|
|
try |
|
|
|
|
{ |
|
|
|
|
|
|
|
|
|
StringBuilder sb = new StringBuilder(); |
|
|
|
|
byte[] readBuffer = new byte[1024]; |
|
|
|
|
while (true) |
|
|
|
|
{ |
|
|
|
|
int bytesRead = await _socket.ReceiveAsync(readBuffer); |
|
|
|
|
if (_cts != null && _cts.IsCancellationRequested) |
|
|
|
|
break; |
|
|
|
|
|
|
|
|
|
int bytesRead = _cts != null && _cts.IsCancellationRequested ? 0 : await _socket.ReceiveAsync(readBuffer); |
|
|
|
|
if (bytesRead < 1) |
|
|
|
|
{ |
|
|
|
|
if (sb.Length > 0 && this.MessageCallback != null) |
|
|
|
|
this.MessageCallback(this, sb.ToString()); |
|
|
|
|
|
|
|
|
|
break; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
string data = Encoding.ASCII.GetString(readBuffer, 0, bytesRead); |
|
|
|
|
sb.Append(data); |
|
|
|
@ -69,9 +79,10 @@ namespace ConsoleApp.TelnetSamples |
|
|
|
|
sb.Clear(); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
catch (Exception) |
|
|
|
|
catch (Exception ex) |
|
|
|
|
{ |
|
|
|
|
throw; |
|
|
|
|
if (this.ErrorCallback != null) |
|
|
|
|
this.ErrorCallback(this, ex); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
@ -79,13 +90,18 @@ namespace ConsoleApp.TelnetSamples |
|
|
|
|
{ |
|
|
|
|
try |
|
|
|
|
{ |
|
|
|
|
command = command.Replace(" ", ""); |
|
|
|
|
if (string.IsNullOrEmpty(command)) |
|
|
|
|
return; |
|
|
|
|
|
|
|
|
|
command += "\r\n"; |
|
|
|
|
byte[] sendBytes = Encoding.ASCII.GetBytes(command); |
|
|
|
|
await _socket.SendAsync(sendBytes); |
|
|
|
|
} |
|
|
|
|
catch (Exception) |
|
|
|
|
catch (Exception ex) |
|
|
|
|
{ |
|
|
|
|
throw; |
|
|
|
|
if (this.ErrorCallback != null) |
|
|
|
|
this.ErrorCallback(this, ex); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
@ -96,13 +112,16 @@ namespace ConsoleApp.TelnetSamples |
|
|
|
|
if (_socket == null) |
|
|
|
|
return; |
|
|
|
|
|
|
|
|
|
_cts.Cancel(); |
|
|
|
|
|
|
|
|
|
_socket.Shutdown(SocketShutdown.Both); |
|
|
|
|
_socket.Close(); |
|
|
|
|
_socket.Dispose(); |
|
|
|
|
} |
|
|
|
|
catch (Exception) |
|
|
|
|
catch (Exception ex) |
|
|
|
|
{ |
|
|
|
|
throw; |
|
|
|
|
if (this.ErrorCallback != null) |
|
|
|
|
this.ErrorCallback(this, ex); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|