The easy to use and full featured Irc Bot everyone is talking about!
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

TCPInterface.cs 3.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.IO;
  6. using System.Net;
  7. using System.Net.Sockets;
  8. using System.Threading;
  9. namespace Combot
  10. {
  11. class TCPInterface
  12. {
  13. internal event Action<TCPError> TCPErrorEvent;
  14. internal event Action<int> TCPConnectionEvent;
  15. internal bool Connected = false;
  16. private IPEndPoint _serverIP = null;
  17. private int _readTimeout = 250;
  18. private Socket _tcpClient;
  19. private NetworkStream _tcpStream;
  20. private int _allowedFailedCount;
  21. private int _currentFailedCount;
  22. internal TCPInterface()
  23. {
  24. }
  25. internal bool Connect(IPAddress IP, int port, int readTimeout, int allowedFailedCount = 0)
  26. {
  27. _serverIP = new IPEndPoint(IP, port);
  28. _readTimeout = readTimeout;
  29. _allowedFailedCount = allowedFailedCount;
  30. _currentFailedCount = 0;
  31. try
  32. {
  33. _tcpClient = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
  34. _tcpClient.Connect(_serverIP);
  35. _tcpClient.ReceiveTimeout = _readTimeout;
  36. _tcpStream = new NetworkStream(_tcpClient);
  37. Connected = true;
  38. return true;
  39. }
  40. catch
  41. {
  42. if (TCPErrorEvent != null)
  43. {
  44. TCPError error = new TCPError();
  45. error.Message = string.Format("Unable to connect to {0} on port {1}", _serverIP.Address, _serverIP.Port);
  46. TCPErrorEvent(error);
  47. }
  48. }
  49. return false;
  50. }
  51. internal void Disconnect()
  52. {
  53. Connected = false;
  54. if (_tcpStream != null)
  55. {
  56. _tcpStream.Close();
  57. }
  58. if (_tcpClient != null)
  59. {
  60. _tcpClient.Close();
  61. }
  62. }
  63. internal void Write(string data)
  64. {
  65. if (_tcpStream.CanWrite && Connected)
  66. {
  67. byte[] message = System.Text.Encoding.UTF8.GetBytes(data + Environment.NewLine);
  68. _tcpStream.Write(message, 0, message.Length);
  69. }
  70. }
  71. internal string Read()
  72. {
  73. try
  74. {
  75. if (_tcpStream.CanRead && Connected)
  76. {
  77. byte[] readBytes = new byte[100000];
  78. _tcpStream.Read(readBytes, 0, readBytes.Length);
  79. string result = Encoding.UTF8.GetString(readBytes, 0, readBytes.Length);
  80. // Reset Failed Counter
  81. _currentFailedCount = 0;
  82. return result.TrimEnd('\0');
  83. }
  84. }
  85. catch (IOException)
  86. {
  87. _currentFailedCount++;
  88. if (TCPErrorEvent != null && _tcpStream.CanRead)
  89. {
  90. TCPError error = new TCPError();
  91. error.Message = string.Format("Read Timeout, No Response from Server in {0}ms", _readTimeout);
  92. TCPErrorEvent(error);
  93. }
  94. }
  95. catch (Exception ex)
  96. {
  97. _currentFailedCount++;
  98. if (TCPErrorEvent != null)
  99. {
  100. TCPError error = new TCPError();
  101. error.Message = ex.Message;
  102. TCPErrorEvent(error);
  103. }
  104. }
  105. if (_currentFailedCount > _allowedFailedCount)
  106. {
  107. if (TCPConnectionEvent != null)
  108. {
  109. TCPConnectionEvent(_currentFailedCount);
  110. }
  111. Disconnect();
  112. _currentFailedCount = 0;
  113. }
  114. return null;
  115. }
  116. }
  117. }