Socket.hpp 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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-26
  7. *
  8. * */
  9. #ifndef LS_STD_SOCKET_HPP
  10. #define LS_STD_SOCKET_HPP
  11. #include <ls_std/os/specification.hpp>
  12. #include <ls_std/os/dynamic_goal.hpp>
  13. #include <ls_std/core/Class.hpp>
  14. #include "SocketParameter.hpp"
  15. #include "SocketAddressMapperParameter.hpp"
  16. #include <memory>
  17. #include <ls_std/core/types/Types.hpp>
  18. #include <ls_std/core/interface/IReader.hpp>
  19. #include <ls_std/core/interface/IWriter.hpp>
  20. namespace ls::std::network
  21. {
  22. 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
  23. {
  24. public:
  25. explicit Socket(ls::std::network::SocketParameter _parameter);
  26. ~Socket() override;
  27. // implementation
  28. [[nodiscard]] ls::std::core::type::byte_field read() override;
  29. [[nodiscard]] bool write(const ls::std::core::type::byte_field &_data) 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 LS_STD_UNIX_PLATFORM
  43. int unixDescriptor{};
  44. #endif
  45. #if LS_STD_UNIX_PLATFORM
  46. [[nodiscard]] bool _acceptUnix();
  47. [[nodiscard]] bool _bindUnix();
  48. [[nodiscard]] bool _closeUnix();
  49. [[nodiscard]] bool _connectUnix();
  50. #endif
  51. [[nodiscard]] SocketAddressMapperParameter _createSocketAddressMapperParameter() const;
  52. void _init();
  53. void _initReadBuffer();
  54. #if LS_STD_UNIX_PLATFORM
  55. [[nodiscard]] bool _initUnix();
  56. [[nodiscard]] bool _listenUnix();
  57. #endif
  58. ls::std::core::type::byte_field _read();
  59. #if LS_STD_UNIX_PLATFORM
  60. ls::std::core::type::byte_field _readUnix();
  61. void _setPosixReaderApi();
  62. void _setPosixSocketApi();
  63. void _setPosixWriterApi();
  64. #endif
  65. bool _write(const ls::std::core::type::byte_field &_data);
  66. #if LS_STD_UNIX_PLATFORM
  67. bool _writeUnix(const ls::std::core::type::byte_field &_data);
  68. #endif
  69. };
  70. }
  71. #endif