You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

90 lines
1.9 KiB
C

1 month ago
#pragma once
#include <vector>
#include <map>
#include <unordered_map>
#include <memory>
#include <algorithm>
#include "RingBuffer.h"
#include "NativeStream.h"
#include "StreamingTsvStream.h"
#include "SigmaView.h"
/**
* @brief <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
*/
class StreamManager
{
public:
static StreamManager& Instance()
{
static StreamManager instance;
return instance;
}
/// pView Ϊ CSigmaView*<2A><>C# <20><> IntPtr<74><72><EFBFBD><EFBFBD>importType <20><> TableImportType<70><65><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʽ<EFBFBD><CABD>Write ʱ<>߽<EFBFBD><DFBD><EFBFBD><EFBFBD>ߵ<EFBFBD><DFB5><EFBFBD>
int64_t Open(CSigmaView* pView, int importType, StreamError& error)
{
int64_t handle = m_nextId++;
m_streams[handle] = std::make_unique<StreamingTsvStream>(static_cast<CSigmaView*>(pView), importType);
error = StreamError::STREAM_OK;
return handle;
}
StreamError Write(int64_t handle, const void* data, size_t size)
{
auto it = m_streams.find(handle);
if (it == m_streams.end())
{
return HandleNotFound(handle);
}
try
{
it->second->Write(data, size);
return StreamError::STREAM_OK;
}
catch (...)
{
return StreamError::STREAM_WRITE_FAILED;
}
}
StreamError Close(int64_t handle)
{
auto it = m_streams.find(handle);
if (it == m_streams.end())
{
return HandleNotFound(handle);
}
it->second->Complete();
m_streams.erase(it);
m_closedHandles.push(handle);
return StreamError::STREAM_OK;
}
private:
using StreamMap = std::unordered_map<int64_t, std::unique_ptr<INativeStream>>;
static constexpr size_t RING_CAPCITY = 1024; // 2^10<31><30><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>رյ<D8B1> handle <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
/// handle <20><><EFBFBD><EFBFBD> m_streams ʱ<><CAB1><EFBFBD><EFBFBD><EFBFBD>֡<EFBFBD><D6A1>ѹرա<D8B1><D5A1><EFBFBD><EBA1B0>Ч handle<6C><65>
StreamError HandleNotFound(int64_t handle) const
{
if (m_closedHandles.contains(handle))
{
return StreamError::STREAM_ALREADY_CLOSED;
}
return StreamError::STREAM_INVALID_HANDLE;
}
StreamManager() = default;
StreamMap m_streams;
RingBuffer<int64_t, RING_CAPCITY> m_closedHandles;
int64_t m_nextId = 1;
};