The easy to use and full featured Irc Bot everyone is talking about!
Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

TCPInterface.cs 3.7KB

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