diff options
author | monster <monster@ydb.tech> | 2022-07-07 14:41:37 +0300 |
---|---|---|
committer | monster <monster@ydb.tech> | 2022-07-07 14:41:37 +0300 |
commit | 06e5c21a835c0e923506c4ff27929f34e00761c2 (patch) | |
tree | 75efcbc6854ef9bd476eb8bf00cc5c900da436a2 /library/cpp/netliba/socket/udp_recv_packet.h | |
parent | 03f024c4412e3aa613bb543cf1660176320ba8f4 (diff) | |
download | ydb-06e5c21a835c0e923506c4ff27929f34e00761c2.tar.gz |
fix ya.make
Diffstat (limited to 'library/cpp/netliba/socket/udp_recv_packet.h')
-rw-r--r-- | library/cpp/netliba/socket/udp_recv_packet.h | 79 |
1 files changed, 79 insertions, 0 deletions
diff --git a/library/cpp/netliba/socket/udp_recv_packet.h b/library/cpp/netliba/socket/udp_recv_packet.h new file mode 100644 index 0000000000..a2777fbcbf --- /dev/null +++ b/library/cpp/netliba/socket/udp_recv_packet.h @@ -0,0 +1,79 @@ +#pragma once + +#include <util/generic/noncopyable.h> +#include <util/system/defaults.h> + +#include <memory> +#include "allocator.h" + +namespace NNetlibaSocket { + enum { UDP_MAX_PACKET_SIZE = 8900 }; + + class TUdpHostRecvBufAlloc; + struct TUdpRecvPacket: public TWithCustomAllocator { + friend class TUdpHostRecvBufAlloc; + int DataStart = 0, DataSize = 0; + std::shared_ptr<char> Data; + + private: + int ArraySize_ = 0; + }; + + /////////////////////////////////////////////////////////////////////////////// + + class TUdpHostRecvBufAlloc: public TNonCopyable { + private: + mutable TUdpRecvPacket* RecvPktBuf; + + static TUdpRecvPacket* Alloc() { + return new TUdpRecvPacket(); + } + + public: + static TUdpRecvPacket* Create(const int dataSize) { + TUdpRecvPacket* result = Alloc(); + result->Data.reset(TCustomAllocator<char>().allocate(dataSize), [=](char* p) { TCustomAllocator<char>().deallocate(p, dataSize); }, TCustomAllocator<char>()); + result->ArraySize_ = dataSize; + return result; + } + void SetNewPacket() const { + RecvPktBuf = CreateNewPacket(); + } + + public: + static TUdpRecvPacket* CreateNewSmallPacket(int dataSize) { + return Create(dataSize); + } + static TUdpRecvPacket* CreateNewPacket() { + return Create(UDP_MAX_PACKET_SIZE); + } + static TUdpRecvPacket* Clone(const TUdpRecvPacket* pkt) { + TUdpRecvPacket* result = Alloc(); + result->DataStart = pkt->DataStart; + result->DataSize = pkt->DataSize; + result->Data = pkt->Data; + result->ArraySize_ = pkt->ArraySize_; + return result; + } + + TUdpHostRecvBufAlloc() { + SetNewPacket(); + } + ~TUdpHostRecvBufAlloc() { + delete RecvPktBuf; + } + + TUdpRecvPacket* ExtractPacket() { + TUdpRecvPacket* res = RecvPktBuf; + SetNewPacket(); + return res; + } + + int GetBufSize() const { + return RecvPktBuf->ArraySize_; + } + char* GetDataPtr() const { + return RecvPktBuf->Data.get(); + } + }; +} |