Socket.hpp 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. /*
  2. * Author: Patrick-Christopher Mattulat
  3. * Company: Lynar Studios
  4. * E-Mail: webmaster@lynarstudios.com
  5. * Created: 2022-11-16
  6. * Changed: 2022-12-16
  7. *
  8. * */
  9. #ifndef LS_STD_SOCKET_HPP
  10. #define LS_STD_SOCKET_HPP
  11. #include <ls_std/os/dynamic_goal.hpp>
  12. #include <ls_std/core/Class.hpp>
  13. #if defined(unix) || defined(__APPLE__)
  14. #include "PosixSocket.hpp"
  15. #endif
  16. #include "SocketParameter.hpp"
  17. #include "SocketAddressMapperParameter.hpp"
  18. #include <memory>
  19. #include <ls_std/core/types/Types.hpp>
  20. #include <ls_std/core/interface/IReader.hpp>
  21. namespace ls::std::network
  22. {
  23. class LS_STD_DYNAMIC_GOAL Socket : public ls::std::core::Class, public ls::std::core::interface_type::IReader
  24. {
  25. public:
  26. explicit Socket(ls::std::network::SocketParameter _parameter);
  27. ~Socket() override;
  28. // implementation
  29. ls::std::core::type::byte_field read() override;
  30. // other functionalities
  31. [[nodiscard]] bool accept();
  32. [[nodiscard]] bool bind();
  33. [[nodiscard]] bool close();
  34. [[nodiscard]] bool connect();
  35. [[nodiscard]] bool isInitialized() const;
  36. [[nodiscard]] bool listen();
  37. private:
  38. bool initialized{};
  39. ls::std::network::SocketParameter parameter{};
  40. ls::std::core::type::byte* readBuffer{};
  41. bool readBufferSet{};
  42. #if defined(unix) || defined(__APPLE__)
  43. int unixDescriptor{};
  44. #endif
  45. #if defined(unix) || defined(__APPLE__)
  46. [[nodiscard]] bool _acceptUnix();
  47. [[nodiscard]] bool _bindUnix();
  48. [[nodiscard]] bool _closeUnix();
  49. [[nodiscard]] bool _connectUnix();
  50. #endif
  51. [[nodiscard]] SocketAddressMapperParameter _createSocketAddressMapperParameter() const;
  52. [[nodiscard]] bool _init();
  53. void _initReadBuffer();
  54. #if defined(unix) || defined(__APPLE__)
  55. [[nodiscard]] bool _initUnix();
  56. [[nodiscard]] bool _listenUnix();
  57. #endif
  58. ls::std::core::type::byte_field _read();
  59. #if defined(unix) || defined(__APPLE__)
  60. ls::std::core::type::byte_field _readUnix();
  61. void _setPosixReaderApi();
  62. void _setUnixSocketApi();
  63. #endif
  64. };
  65. }
  66. #endif