-
Notifications
You must be signed in to change notification settings - Fork 0
Add UnixFileChannel #51
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
dac60f5
947722f
19803eb
3110649
34e9f19
dbada73
ad02372
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,61 @@ | ||
| #pragma once | ||
|
|
||
| #include <vector> | ||
| #include <cstdint> | ||
|
|
||
| #include "data.h" | ||
| #include "Channel.h" | ||
|
|
||
| namespace styxlib | ||
| { | ||
| using FileDescriptor = int; | ||
| constexpr FileDescriptor InvalidFileDescriptor = -1; | ||
|
|
||
| struct ReadBuffer | ||
| { | ||
| std::vector<uint8_t> buffer; | ||
| Size currentSize{0}; | ||
| bool isDirty{false}; | ||
| }; | ||
|
|
||
| class ChannelUnixFile : public ChannelTx, public ChannelRx | ||
| { | ||
| public: | ||
| struct Configuration | ||
| { | ||
| PacketHeaderSize packetSizeHeader{PacketHeaderSize::Size2Bytes}; | ||
| uint16_t iounit{8192}; | ||
| DeserializerL4Ptr deserializer{nullptr}; | ||
| Configuration( | ||
| PacketHeaderSize packetSizeHeader, | ||
| uint16_t iounit, | ||
| DeserializerL4Ptr deserializer) | ||
| : packetSizeHeader(packetSizeHeader), | ||
| iounit(iounit), | ||
| deserializer(deserializer) {} | ||
| }; | ||
| class FileDescriptorPair | ||
| { | ||
| public: | ||
| FileDescriptor readFd; | ||
| FileDescriptor writeFd; | ||
| FileDescriptorPair(FileDescriptor readFd, FileDescriptor writeFd) | ||
| : readFd(readFd), writeFd(writeFd) {} | ||
| }; | ||
|
|
||
| protected: | ||
| FileDescriptorPair fds; | ||
| Configuration config; | ||
| ReadBuffer readBufferData; | ||
|
|
||
| virtual void closeDescriptors(); | ||
| public: | ||
| ChannelUnixFile(const Configuration &config); | ||
| ~ChannelUnixFile() override; | ||
| SizeResult sendBuffer( | ||
| ClientId clientId, | ||
| const StyxBuffer buffer, | ||
| Size size) override; | ||
| void readBufferBlocking(); | ||
| }; | ||
| } // namespace styxlib |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,38 @@ | ||
| #pragma once | ||
|
|
||
| #include <expected> | ||
| #include <future> | ||
| #include <atomic> | ||
| #include <memory> | ||
| #include <thread> | ||
|
|
||
| #include "ChannelUnixFile.h" | ||
|
|
||
| namespace styxlib | ||
| { | ||
| class ChannelUnixPipeImpl : public ChannelUnixFile | ||
| { | ||
| public: | ||
| using StartResult = std::expected<ChannelUnixFile::FileDescriptorPair, ErrorCode>; | ||
| private: | ||
| std::thread serverThread; | ||
| std::atomic<bool> running{false}; | ||
| std::atomic<bool> stopRequested{false}; | ||
| std::unique_ptr<std::promise<StartResult>> startPromise; | ||
| FileDescriptorPair rxFds; | ||
| FileDescriptorPair txFds; | ||
| bool isDescriptorOwned{false}; | ||
| private: | ||
| void workThreadFunction(); | ||
| protected: | ||
| void closeDescriptors() override; | ||
| public: | ||
| ChannelUnixPipeImpl(const ChannelUnixFile::Configuration &config); | ||
| std::future<StartResult> start(); | ||
| std::future<ErrorCode> connect(const FileDescriptorPair& fds); | ||
| bool isStarted() const; | ||
| bool isConnected() const; | ||
| std::future<void> stop(); | ||
| FileDescriptorPair getClientFileDescriptors() const; | ||
| }; | ||
| } // namespace styxlib |
| Original file line number | Diff line number | Diff line change | ||||||
|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,39 @@ | ||||||||
| #include "Channel.h" | ||||||||
|
|
||||||||
| namespace styxlib | ||||||||
| { | ||||||||
| std::expected<uint8_t, ErrorCode> setPacketSize(const PacketHeaderSize &headerSize, | ||||||||
| uint8_t* buffer, | ||||||||
| Size bufferSize, | ||||||||
| Size packetSize) { | ||||||||
| if (bufferSize < 4) { | ||||||||
| return std::unexpected(ErrorCode::BufferTooSmall); | ||||||||
| } | ||||||||
| switch (headerSize) | ||||||||
| { | ||||||||
| case PacketHeaderSize::Size1Byte: | ||||||||
| if (packetSize > 0xFF) { | ||||||||
| return std::unexpected(ErrorCode::PacketTooLarge); | ||||||||
| } | ||||||||
| buffer[0] = static_cast<uint8_t>(packetSize); | ||||||||
| break; | ||||||||
| case PacketHeaderSize::Size2Bytes: | ||||||||
| if (packetSize > 0xFFFF) { | ||||||||
| return std::unexpected(ErrorCode::PacketTooLarge); | ||||||||
| } | ||||||||
| buffer[1] = packetSize & 0xFF; | ||||||||
| buffer[0] = (packetSize >> 8) & 0xFF; | ||||||||
| break; | ||||||||
| case PacketHeaderSize::Size4Bytes: | ||||||||
| if (packetSize > 0xFFFFFFFF) { | ||||||||
| return std::unexpected(ErrorCode::PacketTooLarge); | ||||||||
| } | ||||||||
|
Comment on lines
+28
to
+30
|
||||||||
| if (packetSize > 0xFFFFFFFF) { | |
| return std::unexpected(ErrorCode::PacketTooLarge); | |
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Changing
ClientIdfrominttouint16_tis a breaking API change that limits the maximum number of clients from ~2 billion to 65,535. Consider whether this limitation is acceptable for your use case, and ensure this change is properly documented in release notes. If backward compatibility is needed, consider versioning the API.