Socket.hpp 3.4 KB

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