Socket.hpp 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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-31
  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. #include <list>
  21. #include <ls_std/core/types/SocketTypes.hpp>
  22. #if LS_STD_UNIX_PLATFORM
  23. #include "UnixSocketDescriptor.hpp"
  24. #endif
  25. namespace ls::std::network
  26. {
  27. 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
  28. {
  29. public:
  30. explicit Socket(ls::std::network::SocketParameter _parameter);
  31. ~Socket() override;
  32. // implementation
  33. [[nodiscard]] ls::std::core::type::byte_field read() override;
  34. [[nodiscard]] bool write(const ls::std::core::type::byte_field &_data) override;
  35. // other functionalities
  36. [[nodiscard]] ls::std::core::type::connection_id accept();
  37. [[nodiscard]] bool bind();
  38. [[nodiscard]] bool close();
  39. [[nodiscard]] bool connect();
  40. [[nodiscard]] bool handle(const ls::std::core::type::connection_id& _connectionId);
  41. [[nodiscard]] bool handle();
  42. [[nodiscard]] bool isInitialized() const;
  43. [[nodiscard]] bool listen();
  44. private:
  45. ls::std::core::type::connection_id currentAcceptedConnection{};
  46. bool initialized{};
  47. ls::std::network::SocketParameter parameter{};
  48. ls::std::core::type::byte* readBuffer{};
  49. bool readBufferSet{};
  50. #if LS_STD_UNIX_PLATFORM
  51. ::std::list<ls::std::network::UnixSocketDescriptor> unixDescriptors{};
  52. ls::std::core::type::connection_id unixUniqueDescriptorId{};
  53. #endif
  54. #if LS_STD_UNIX_PLATFORM
  55. [[nodiscard]] ls::std::core::type::connection_id _acceptUnix();
  56. void _addUnixDescriptor(const int& _descriptor);
  57. [[nodiscard]] bool _bindUnix();
  58. #endif
  59. [[nodiscard]] bool _close();
  60. #if LS_STD_UNIX_PLATFORM
  61. [[nodiscard]] bool _closeUnix(const ls::std::core::type::connection_id& _connectionId);
  62. [[nodiscard]] bool _connectUnix();
  63. #endif
  64. [[nodiscard]] SocketAddressMapperParameter _createSocketAddressMapperParameter() const;
  65. [[nodiscard]] bool _hasAcceptedConnection(const ls::std::core::type::connection_id& _connectionId);
  66. #if LS_STD_UNIX_PLATFORM
  67. [[nodiscard]] bool _hasAcceptedConnectionUnix(const ls::std::core::type::connection_id& _connectionId);
  68. #endif
  69. [[nodiscard]] bool _handle(const ls::std::core::type::connection_id& _connectionId);
  70. void _init();
  71. void _initReadBuffer();
  72. #if LS_STD_UNIX_PLATFORM
  73. [[nodiscard]] bool _initUnix();
  74. [[nodiscard]] bool _listenUnix();
  75. #endif
  76. ls::std::core::type::byte_field _read();
  77. #if LS_STD_UNIX_PLATFORM
  78. ls::std::core::type::byte_field _readUnix(const int& _descriptor);
  79. [[nodiscard]] bool _removeUnix(const ls::std::core::type::connection_id& _connectionId);
  80. void _setPosixReaderApi();
  81. void _setPosixSocketApi();
  82. void _setPosixWriterApi();
  83. #endif
  84. bool _write(const ls::std::core::type::byte_field &_data);
  85. #if LS_STD_UNIX_PLATFORM
  86. bool _writeUnix(const int& _descriptor, const ls::std::core::type::byte_field &_data);
  87. #endif
  88. };
  89. }
  90. #endif