Advertisement
Vlad3955

TCPserver.h

Oct 28th, 2022
923
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.76 KB | None | 0 0
  1. #pragma once
  2. #include <iostream>
  3. #include <cassert>
  4. #include <algorithm>
  5. #include <cstdlib>
  6. #include <cwctype>
  7. #include <stdexcept>
  8. #include <fstream>
  9. #include <iomanip>
  10. #include <string>
  11. #include <array>
  12. #include <optional>
  13. #include <filesystem>
  14. #include <thread>
  15.  
  16. #include <socket_wrapper/socket_headers.h>
  17. #include <socket_wrapper/socket_wrapper.h>
  18. #include <socket_wrapper/socket_class.h>
  19.  
  20.  
  21.  
  22.  
  23. //const auto clients_count = 10;
  24. const auto buffer_size = 4096;
  25. using namespace std::literals;
  26. namespace fs = std::filesystem;
  27.  
  28. #if defined(_WIN32)
  29. const wchar_t separ = fs::path::preferred_separator;
  30. #else
  31. const wchar_t separ = *reinterpret_cast<const wchar_t*>(&fs::path::preferred_separator);
  32. #endif
  33.  
  34.  
  35. // Trim from end (in place).
  36. static inline std::string& rtrim(std::string& s)
  37. {
  38.     s.erase(std::find_if(s.rbegin(), s.rend(), [](int c) { return !std::isspace(c); }).base());
  39.     return s;
  40. }
  41.  
  42.  
  43. class Connecter
  44. {
  45. public:
  46.     Connecter();
  47.     socket_wrapper::Socket connect_to_client(unsigned short port);
  48.     ~Connecter();
  49. private:
  50.  
  51. };
  52.  
  53.  
  54. class TCPserver
  55. {
  56. public:
  57.     TCPserver(socket_wrapper::Socket&& client_sock);
  58.     TCPserver(const TCPserver&) = delete;
  59.     TCPserver() = delete;
  60.     std::string get_request();
  61.     void server_run();
  62.     bool send_file(fs::path const& file_path);
  63.     bool send_buffer(const std::vector<char>& buffer);
  64.     std::optional<fs::path> recv_file_path();
  65.     bool process();
  66.     ~TCPserver();
  67. private:
  68.     static bool need_to_repeat()
  69.     {
  70.         switch (errno)
  71.         {
  72.         case EINTR:
  73.         case EAGAIN:
  74.             // case EWOULDBLOCK: // EWOULDBLOCK == EINTR.
  75.             return true;
  76.         }
  77.  
  78.         return false;
  79.     };
  80. private:
  81.     socket_wrapper::Socket client_sock_;
  82. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement