Browse Source

Added checks for tcp connection and modified interface class name

pull/1/head
Teknikode 8 years ago
parent
commit
d6e2cc1529
  1. 2
      Combot.csproj
  2. 19
      IRCService.cs
  3. 2
      Methods.cs
  4. 7
      TCP/TCPInterface.cs

2
Combot.csproj

@ -40,7 +40,7 @@ @@ -40,7 +40,7 @@
</ItemGroup>
<ItemGroup>
<Compile Include="IRCService.cs" />
<Compile Include="Interface.cs" />
<Compile Include="Methods.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="TCP\TCPInterface.cs" />
<Compile Include="TCP\TCPTypes.cs" />

19
IRCService.cs

@ -3,6 +3,7 @@ using System.Collections.Generic; @@ -3,6 +3,7 @@ using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Net;
namespace Combot
{
@ -15,6 +16,22 @@ namespace Combot @@ -15,6 +16,22 @@ namespace Combot
_tcp = new TCPInterface();
}
internal bool Connect(IPAddress IP, int port, int readTimeout, int allowedFailedCount = 0)
{
bool result = false;
if (!_tcp.Connected)
{
result = _tcp.Connect(IP, port, readTimeout, allowedFailedCount);
}
return result;
}
internal bool Disconnect()
{
bool result = false;
return result;
}
}
}

2
Interface.cs → Methods.cs

@ -6,7 +6,7 @@ using System.Threading.Tasks; @@ -6,7 +6,7 @@ using System.Threading.Tasks;
namespace Combot
{
public class Interface
public class Methods
{
}
}

7
TCP/TCPInterface.cs

@ -13,6 +13,7 @@ namespace Combot @@ -13,6 +13,7 @@ namespace Combot
{
internal event Action<TCPError> TCPErrorEvent;
internal event Action<int> TCPConnectionEvent;
internal bool Connected = false;
private IPEndPoint _serverIP = null;
private int _readTimeout = 250;
@ -39,6 +40,7 @@ namespace Combot @@ -39,6 +40,7 @@ namespace Combot
_tcpClient.ReceiveTimeout = _readTimeout;
_tcpStream = new NetworkStream(_tcpClient);
Connected = true;
return true;
}
catch
@ -55,6 +57,7 @@ namespace Combot @@ -55,6 +57,7 @@ namespace Combot
internal void Disconnect()
{
Connected = false;
if (_tcpStream != null)
{
_tcpStream.Close();
@ -67,7 +70,7 @@ namespace Combot @@ -67,7 +70,7 @@ namespace Combot
internal void Write(string data)
{
if (_tcpStream.CanWrite)
if (_tcpStream.CanWrite && Connected)
{
byte[] message = System.Text.Encoding.UTF8.GetBytes(data + Environment.NewLine);
_tcpStream.Write(message, 0, message.Length);
@ -78,7 +81,7 @@ namespace Combot @@ -78,7 +81,7 @@ namespace Combot
{
try
{
if (_tcpStream.CanRead)
if (_tcpStream.CanRead && Connected)
{
byte[] readBytes = new byte[100000];
_tcpStream.Read(readBytes, 0, readBytes.Length);

Loading…
Cancel
Save