Server.h 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. #ifndef Server_H
  2. #define Server_H
  3. #include <ReferenceCounter.h>
  4. #include "Network.h"
  5. #ifndef HEADER_OPENSSL_TYPES_H
  6. struct ssl_ctx_st;
  7. struct ssl_st;
  8. #endif
  9. namespace Framework
  10. {
  11. namespace Encryption
  12. {
  13. class Key;
  14. }
  15. class Text;
  16. } // namespace Framework
  17. using namespace Framework;
  18. namespace Network
  19. {
  20. class Server; // from this file
  21. class SClient; // from this file
  22. class Server : public virtual ReferenceCounter
  23. {
  24. private:
  25. SOCKET sock;
  26. SOCKADDR_IN address;
  27. int clients;
  28. public:
  29. // Constructor
  30. __declspec(dllexport) Server();
  31. // Destructor
  32. __declspec(dllexport) ~Server();
  33. // non-constant
  34. __declspec(dllexport) bool connect(
  35. unsigned short port, int queueLength); // Opens the socket
  36. __declspec(dllexport) SClient* getClient(); // accepts a client
  37. __declspec(dllexport) int getClients(
  38. bool reset); // returns the number of clients
  39. __declspec(dllexport) bool disconnect(); // stops the server
  40. // constant
  41. __declspec(
  42. dllexport) unsigned short getPort() const; // returns the port
  43. __declspec(dllexport) bool isConnected()
  44. const; // returns 1 if the server is connected
  45. };
  46. class SClient : public EncryptedConnection,
  47. public virtual ReferenceCounter
  48. {
  49. private:
  50. SOCKET sock;
  51. sockaddr_in clientAddr;
  52. Encryption::Key* sendKey;
  53. Encryption::Key* receiveKey;
  54. int downStreamBytes;
  55. int upStreamBytes;
  56. public:
  57. // Constructor
  58. __declspec(dllexport) SClient(sockaddr_in client, SOCKET sock);
  59. // Destructor
  60. __declspec(dllexport) ~SClient();
  61. // non-constant
  62. __declspec(dllexport) void setReceiveTimeout(
  63. int miliseconds); // Sets a timeout for receiving data
  64. __declspec(dllexport) void setSendKeyZ(
  65. Encryption::Key* key); // Sets the key for sending
  66. __declspec(dllexport) void setReceiveKeyZ(
  67. Encryption::Key* key); // Sets the key for receiving
  68. __declspec(dllexport) void setSendKey(
  69. const char* key, int len); // Sets the key for sending
  70. __declspec(dllexport) void setReceiveKey(
  71. const char* key, int len); // Sets the key for receiving
  72. __declspec(dllexport) bool send(
  73. const char* message, int len) override; // sends to client
  74. __declspec(dllexport) bool getMessage(
  75. char* message, int len) override; // receives message from client
  76. __declspec(dllexport) bool sendEncrypted(
  77. const char* message, int len) override; // sends to server
  78. __declspec(dllexport) bool getMessageEncrypted(
  79. char* message, int len) override; // receives message
  80. __declspec(dllexport) int getDownloadBytes(
  81. bool reset); // returns the number of received bytes
  82. __declspec(dllexport) int getUploadBytes(
  83. bool reset); // returns the number of sent bytes
  84. __declspec(dllexport) bool disconnect(); // disconnects from client
  85. // constant
  86. __declspec(dllexport) bool hasMessage(
  87. int time) const; // Waits for a message for a given time
  88. __declspec(
  89. dllexport) unsigned short getPort() const; // returns the port
  90. __declspec(dllexport) const
  91. char* getIp() const; // returns the client's IP
  92. __declspec(dllexport) bool waitForNextMessage()
  93. const; // waits until there is something to receive
  94. };
  95. class SSLSClient;
  96. class SSLServer : public virtual ReferenceCounter
  97. {
  98. private:
  99. SOCKET s;
  100. SOCKADDR_IN addr;
  101. ssl_ctx_st* ctx;
  102. Text* password;
  103. int clients;
  104. public:
  105. // Constructor
  106. __declspec(dllexport) SSLServer();
  107. // Destructor
  108. __declspec(dllexport) ~SSLServer();
  109. // non-constant
  110. // Sets the path to the file where the certificate is stored
  111. __declspec(dllexport) bool setCertificateFile(const char* file);
  112. // Sets the path to the file where the private key is stored
  113. __declspec(dllexport) bool setPrivateKeyFile(const char* file);
  114. // sets the password of the private key (must be called before
  115. // setPrivateKeyFile)
  116. __declspec(dllexport) void setPrivateKeyPassword(const char* password);
  117. // Opens the socket
  118. __declspec(dllexport) bool connect(
  119. unsigned short port, int queueLength);
  120. // accepts a client
  121. __declspec(dllexport) SSLSClient* getClient();
  122. // returns the number of clients
  123. __declspec(dllexport) int getClients(bool reset);
  124. // stops the server
  125. __declspec(dllexport) bool disconnect();
  126. // constant
  127. // returns the port
  128. __declspec(dllexport) unsigned short getPort() const;
  129. // returns 1 if the server is connected
  130. __declspec(dllexport) bool isConnected() const;
  131. };
  132. class SSLSClient : public Connection,
  133. public virtual ReferenceCounter
  134. {
  135. private:
  136. SOCKET s;
  137. ssl_st* ssl;
  138. sockaddr_in clientAddr;
  139. int downStreamBytes;
  140. int upStreamBytes;
  141. public:
  142. // Constructor
  143. __declspec(dllexport)
  144. SSLSClient(sockaddr_in client, ssl_st* ssl, SOCKET s);
  145. // Destructor
  146. __declspec(dllexport) ~SSLSClient();
  147. // non-constant
  148. __declspec(dllexport) void setReceiveTimeout(
  149. int miliseconds); // Sets a timeout for receiving data
  150. __declspec(dllexport) bool send(
  151. const char* message, int len) override; // sends to client
  152. __declspec(dllexport) bool getMessage(
  153. char* message, int len) override; // receives message from client
  154. __declspec(dllexport) int getDownloadBytes(
  155. bool reset); // returns the number of received bytes
  156. __declspec(dllexport) int getUploadBytes(
  157. bool reset); // returns the number of sent bytes
  158. __declspec(dllexport) bool disconnect(); // disconnects from client
  159. // constant
  160. __declspec(dllexport) bool hasMessage(
  161. int time) const; // Waits for a message for a given time
  162. __declspec(
  163. dllexport) unsigned short getPort() const; // returns the port
  164. __declspec(dllexport) const
  165. char* getIp() const; // returns the client's IP
  166. };
  167. } // namespace Network
  168. #endif