Socket.hpp 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  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-25
  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. #include "SocketParameter.hpp"
  14. #include "SocketAddressMapperParameter.hpp"
  15. #include <memory>
  16. #include <ls_std/core/types/Types.hpp>
  17. #include <ls_std/core/interface/IReader.hpp>
  18. #include <ls_std/core/interface/IWriter.hpp>
  19. namespace ls::std::network
  20. {
  21. class LS_STD_DYNAMIC_GOAL Socket : public ls::std::core::Class, public ls::std::core::interface_type::IReader, public ls::std::core::interface_type::IWriter
  22. {
  23. public:
  24. explicit Socket(ls::std::network::SocketParameter _parameter);
  25. ~Socket() override;
  26. // implementation
  27. [[nodiscard]] ls::std::core::type::byte_field read() override;
  28. [[nodiscard]] bool write(const ls::std::core::type::byte_field &_data) override;
  29. // other functionalities
  30. [[nodiscard]] bool accept();
  31. [[nodiscard]] bool bind();
  32. [[nodiscard]] bool close();
  33. [[nodiscard]] bool connect();
  34. [[nodiscard]] bool isInitialized() const;
  35. [[nodiscard]] bool listen();
  36. private:
  37. bool initialized{};
  38. ls::std::network::SocketParameter parameter{};
  39. ls::std::core::type::byte* readBuffer{};
  40. bool readBufferSet{};
  41. #if defined(unix) || defined(__APPLE__)
  42. int unixDescriptor{};
  43. #endif
  44. #if defined(unix) || defined(__APPLE__)
  45. [[nodiscard]] bool _acceptUnix();
  46. [[nodiscard]] bool _bindUnix();
  47. [[nodiscard]] bool _closeUnix();
  48. [[nodiscard]] bool _connectUnix();
  49. #endif
  50. [[nodiscard]] SocketAddressMapperParameter _createSocketAddressMapperParameter() const;
  51. [[nodiscard]] bool _init();
  52. void _initReadBuffer();
  53. #if defined(unix) || defined(__APPLE__)
  54. [[nodiscard]] bool _initUnix();
  55. [[nodiscard]] bool _listenUnix();
  56. #endif
  57. ls::std::core::type::byte_field _read();
  58. #if defined(unix) || defined(__APPLE__)
  59. ls::std::core::type::byte_field _readUnix();
  60. void _setPosixReaderApi();
  61. void _setPosixSocketApi();
  62. void _setPosixWriterApi();
  63. #endif
  64. bool _write(const ls::std::core::type::byte_field &_data);
  65. #if defined(unix) || defined(__APPLE__)
  66. bool _writeUnix(const ls::std::core::type::byte_field &_data);
  67. #endif
  68. };
  69. }
  70. #endif