Network.h 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. #ifndef Network_H
  2. #define Network_H
  3. #ifdef WIN32
  4. # ifdef _DEBUG
  5. # ifndef _LTMDB
  6. # define _CRTDBG_MAP_ALLOC
  7. # include <crtdbg.h>
  8. # include <stdlib.h>
  9. # define DEBUG_CLIENTBLOCK new (_CLIENT_BLOCK, __FILE__, __LINE__)
  10. # define new DEBUG_CLIENTBLOCK
  11. # define _LTMDB
  12. # endif
  13. # endif
  14. # define _WINSOCK_DEPRECATED_NO_WARNINGS
  15. # include <WinSock2.h>
  16. #else
  17. # include <arpa/inet.h>
  18. # include <fcntl.h>
  19. # include <iostream>
  20. # include <netinet/in.h>
  21. # include <sys/socket.h>
  22. # include <sys/time.h>
  23. # include <unistd.h>
  24. # define __declspec(x)
  25. # define __int64 long long
  26. # ifndef SOCKET
  27. # define SOCKET int
  28. # define SOCKADDR_IN sockaddr_in
  29. # define ADDR_ANY INADDR_ANY
  30. # define closesocket close
  31. # define PHOSTENT hostent*
  32. # endif
  33. #endif
  34. #include <Reader.h>
  35. #include <Writer.h>
  36. namespace Framework
  37. {
  38. class Text;
  39. }
  40. namespace Network
  41. {
  42. __declspec(dllexport) void Start(int maxClients);
  43. __declspec(dllexport) void getHostName(char* name, int bufferLen);
  44. __declspec(dllexport) char* getHostAddress();
  45. __declspec(dllexport) void Exit();
  46. class Connection
  47. {
  48. public:
  49. virtual bool send(const char* message, int len)
  50. = 0; // sends message
  51. virtual bool getMessage(char* message, int len)
  52. = 0; // receives message
  53. };
  54. class EncryptedConnection : public Connection
  55. {
  56. public:
  57. virtual bool sendEncrypted(const char* message, int len)
  58. = 0; // sends message
  59. virtual bool getMessageEncrypted(char* message, int len)
  60. = 0; // receives message
  61. };
  62. class NetworkReader : public Framework::StreamReader
  63. {
  64. private:
  65. Connection* connection;
  66. public:
  67. __declspec(dllexport) NetworkReader(Connection* v);
  68. __declspec(dllexport) virtual ~NetworkReader();
  69. //! Reads from the resource
  70. //! \param bytes An array to be filled with bytes from the resource
  71. //! \param len How many bytes to read from the resource
  72. __declspec(dllexport) void read(char* bytes, int len) override;
  73. //! Reads the next line from the resource
  74. //! \return The read line as text with line break
  75. __declspec(dllexport) Framework::Text* readLine() override;
  76. //! Checks whether the resource has been fully read
  77. //! return 1 if the resource has been fully read. 0 otherwise
  78. __declspec(dllexport) bool isEnd() const override;
  79. };
  80. class NetworkWriter : public Framework::StreamWriter
  81. {
  82. private:
  83. Connection* connection;
  84. public:
  85. __declspec(dllexport) NetworkWriter(Connection* v);
  86. __declspec(dllexport) virtual ~NetworkWriter();
  87. //! Writes to the resource
  88. //! \param bytes An array containing the bytes to write to the resource
  89. //! \param len How many bytes to write to the resource
  90. __declspec(dllexport) void write(const char* bytes, int len) override;
  91. //! Checks whether the resource has been fully written
  92. //! return 1 if the resource has been fully written. 0 otherwise
  93. __declspec(dllexport) bool isEnd() const override;
  94. };
  95. class EncryptedNetworkReader : public Framework::StreamReader
  96. {
  97. private:
  98. EncryptedConnection* connection;
  99. public:
  100. __declspec(dllexport) EncryptedNetworkReader(EncryptedConnection* v);
  101. //! Reads from the resource
  102. //! \param bytes An array to be filled with bytes from the resource
  103. //! \param len How many bytes to read from the resource
  104. __declspec(dllexport) void read(char* bytes, int len) override;
  105. //! Reads the next line from the resource
  106. //! \return The read line as text with line break
  107. __declspec(dllexport) Framework::Text* readLine() override;
  108. //! Checks whether the resource has been fully read
  109. //! return 1 if the resource has been fully read. 0 otherwise
  110. __declspec(dllexport) bool isEnd() const override;
  111. };
  112. class EncryptedNetworkWriter : public Framework::StreamWriter
  113. {
  114. private:
  115. EncryptedConnection* connection;
  116. public:
  117. __declspec(dllexport) EncryptedNetworkWriter(EncryptedConnection* v);
  118. //! Writes to the resource
  119. //! \param bytes An array containing the bytes to write to the resource
  120. //! \param len How many bytes to write to the resource
  121. __declspec(dllexport) void write(const char* bytes, int len) override;
  122. //! Checks whether the resource has been fully written
  123. //! return 1 if the resource has been fully written. 0 otherwise
  124. __declspec(dllexport) bool isEnd() const override;
  125. };
  126. } // namespace Network
  127. #endif