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 5.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  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.IRCServices.TCP
  10. {
  11. internal 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. Action<TCPError> localEvent = TCPErrorEvent;
  43. if (localEvent != null)
  44. {
  45. TCPError error = new TCPError();
  46. error.Message = string.Format("Unable to connect to {0} on port {1}", _serverIP.Address, _serverIP.Port);
  47. localEvent(error);
  48. }
  49. }
  50. return false;
  51. }
  52. internal void Disconnect()
  53. {
  54. Connected = false;
  55. if (_tcpStream != null)
  56. {
  57. _tcpStream.Close();
  58. }
  59. if (_tcpClient != null)
  60. {
  61. _tcpClient.Close();
  62. }
  63. }
  64. internal void Write(string data)
  65. {
  66. try
  67. {
  68. if (_tcpStream.CanWrite && Connected)
  69. {
  70. byte[] message = System.Text.Encoding.UTF8.GetBytes(data + Environment.NewLine);
  71. _tcpStream.Write(message, 0, message.Length);
  72. }
  73. }
  74. catch (IOException)
  75. {
  76. /*
  77. _currentFailedCount++;
  78. Action<TCPError> localEvent = TCPErrorEvent;
  79. if (localEvent != null && _tcpStream.CanRead)
  80. {
  81. TCPError error = new TCPError();
  82. error.Message = string.Format("Read Timeout, No Response from Server in {0}ms", _readTimeout);
  83. localEvent(error);
  84. }
  85. */
  86. }
  87. catch (Exception ex)
  88. {
  89. Action<TCPError> localEvent = TCPErrorEvent;
  90. if (localEvent != null)
  91. {
  92. TCPError error = new TCPError();
  93. error.Message = ex.Message;
  94. localEvent(error);
  95. }
  96. }
  97. }
  98. internal string Read()
  99. {
  100. try
  101. {
  102. if (_tcpStream.CanRead && Connected)
  103. {
  104. byte[] readBytes = new byte[100000];
  105. _tcpStream.Read(readBytes, 0, readBytes.Length);
  106. string result = Encoding.UTF8.GetString(readBytes, 0, readBytes.Length);
  107. // Reset Failed Counter
  108. _currentFailedCount = 0;
  109. return result.TrimEnd('\0');
  110. }
  111. }
  112. catch (IOException)
  113. {
  114. /*
  115. _currentFailedCount++;
  116. Action<TCPError> localEvent = TCPErrorEvent;
  117. if (localEvent != null && _tcpStream.CanRead)
  118. {
  119. TCPError error = new TCPError();
  120. error.Message = string.Format("Read Timeout, No Response from Server in {0}ms", _readTimeout);
  121. localEvent(error);
  122. }
  123. */
  124. }
  125. catch (Exception ex)
  126. {
  127. _currentFailedCount++;
  128. Action<TCPError> localEvent = TCPErrorEvent;
  129. if (localEvent != null)
  130. {
  131. TCPError error = new TCPError();
  132. error.Message = ex.Message;
  133. localEvent(error);
  134. }
  135. }
  136. if (_currentFailedCount > _allowedFailedCount)
  137. {
  138. Action<int> localEvent = TCPConnectionEvent;
  139. if (localEvent != null)
  140. {
  141. localEvent(_currentFailedCount);
  142. }
  143. Disconnect();
  144. _currentFailedCount = 0;
  145. }
  146. return null;
  147. }
  148. }
  149. }