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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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. if (_tcpStream.CanWrite && Connected)
  67. {
  68. byte[] message = System.Text.Encoding.UTF8.GetBytes(data + Environment.NewLine);
  69. _tcpStream.Write(message, 0, message.Length);
  70. }
  71. }
  72. internal string Read()
  73. {
  74. try
  75. {
  76. if (_tcpStream.CanRead && Connected)
  77. {
  78. byte[] readBytes = new byte[100000];
  79. _tcpStream.Read(readBytes, 0, readBytes.Length);
  80. string result = Encoding.UTF8.GetString(readBytes, 0, readBytes.Length);
  81. // Reset Failed Counter
  82. _currentFailedCount = 0;
  83. return result.TrimEnd('\0');
  84. }
  85. }
  86. catch (IOException)
  87. {
  88. /*
  89. _currentFailedCount++;
  90. Action<TCPError> localEvent = TCPErrorEvent;
  91. if (localEvent != null && _tcpStream.CanRead)
  92. {
  93. TCPError error = new TCPError();
  94. error.Message = string.Format("Read Timeout, No Response from Server in {0}ms", _readTimeout);
  95. localEvent(error);
  96. }
  97. */
  98. }
  99. catch (Exception ex)
  100. {
  101. _currentFailedCount++;
  102. Action<TCPError> localEvent = TCPErrorEvent;
  103. if (localEvent != null)
  104. {
  105. TCPError error = new TCPError();
  106. error.Message = ex.Message;
  107. localEvent(error);
  108. }
  109. }
  110. if (_currentFailedCount > _allowedFailedCount)
  111. {
  112. Action<int> localEvent = TCPConnectionEvent;
  113. if (localEvent != null)
  114. {
  115. localEvent(_currentFailedCount);
  116. }
  117. Disconnect();
  118. _currentFailedCount = 0;
  119. }
  120. return null;
  121. }
  122. }
  123. }