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.

IRCService.cs 2.0KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading;
  6. using System.Threading.Tasks;
  7. using System.Net;
  8. namespace Combot
  9. {
  10. internal partial class IRCService
  11. {
  12. protected Bot _Bot;
  13. protected TCPInterface _TCP;
  14. protected Thread TCPReader;
  15. protected event Action<string> TCPMessageEvent;
  16. internal IRCService(Bot bot)
  17. {
  18. _Bot = bot;
  19. _TCP = new TCPInterface();
  20. TCPMessageEvent += ParseTCPMessage;
  21. }
  22. internal bool Connect(IPAddress IP, int port, int readTimeout, int allowedFailedCount = 0)
  23. {
  24. bool result = false;
  25. if (!_TCP.Connected)
  26. {
  27. result = _TCP.Connect(IP, port, readTimeout, allowedFailedCount);
  28. if (result)
  29. {
  30. TCPReader = new Thread(ReadTCPMessages);
  31. TCPReader.IsBackground = true;
  32. TCPReader.Start();
  33. }
  34. }
  35. return result;
  36. }
  37. internal bool Disconnect()
  38. {
  39. bool result = false;
  40. if (_TCP.Connected)
  41. {
  42. _TCP.Disconnect();
  43. }
  44. return result;
  45. }
  46. protected void ReadTCPMessages()
  47. {
  48. while (_TCP.Connected)
  49. {
  50. string response = ReadTCPMessage();
  51. if (TCPMessageEvent != null && response != null && response != string.Empty)
  52. {
  53. TCPMessageEvent(response);
  54. }
  55. Thread.Sleep(10);
  56. }
  57. }
  58. protected string ReadTCPMessage()
  59. {
  60. if (_TCP.Connected)
  61. {
  62. return _TCP.Read();
  63. }
  64. return null;
  65. }
  66. protected void SendTCPMessage(string message)
  67. {
  68. if (_TCP.Connected)
  69. {
  70. _TCP.Write(message);
  71. }
  72. }
  73. }
  74. }