Stream Context. Immutable properties.

This commit is contained in:
lganzzzo 2019-12-10 15:39:33 +02:00
parent de1ab64920
commit b456f043cc
8 changed files with 132 additions and 149 deletions

View File

@ -26,30 +26,12 @@
namespace oatpp { namespace data{ namespace stream {
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// FileStreamContext
FileStreamContext FileStreamContext::DEFAULT_CONTEXT;
void FileStreamContext::init() {
// DO NOTHING
}
async::CoroutineStarter FileStreamContext::initAsync() {
return nullptr;
}
bool FileStreamContext::isInitialized() const {
return true;
}
StreamType FileStreamContext::getStreamType() const {
return StreamType::STREAM_FINITE;
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// FileInputStream
oatpp::data::stream::DefaultInitializedContext FileInputStream::DEFAULT_CONTEXT(data::stream::StreamType::STREAM_FINITE);
FileInputStream::FileInputStream(std::FILE* file, bool ownsFile)
: m_file(file)
, m_ownsFile(ownsFile)
@ -104,12 +86,14 @@ IOMode FileInputStream::getInputStreamIOMode() {
}
Context* FileInputStream::getInputStreamContext() {
return &FileStreamContext::DEFAULT_CONTEXT;
return &DEFAULT_CONTEXT;
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// FileOutputStream
oatpp::data::stream::DefaultInitializedContext FileOutputStream::DEFAULT_CONTEXT(data::stream::StreamType::STREAM_FINITE);
FileOutputStream::FileOutputStream(std::FILE* file, bool ownsFile)
: m_file(file)
, m_ownsFile(ownsFile)
@ -165,7 +149,7 @@ IOMode FileOutputStream::getOutputStreamIOMode() {
}
Context* FileOutputStream::getOutputStreamContext() {
return &FileStreamContext::DEFAULT_CONTEXT;
return &DEFAULT_CONTEXT;
}
}}}

View File

@ -31,46 +31,12 @@
namespace oatpp { namespace data{ namespace stream {
/**
* File stream context.
*/
class FileStreamContext : public Context {
public:
static FileStreamContext DEFAULT_CONTEXT;
public:
/**
* Initialize stream context. <br>
* *This particular implementation does nothing.*
*/
void init() override;
/**
* Initialize stream context in an async manner.
* *This particular implementation does nothing.*
* @return - &id:oatpp::async::CoroutineStarter;.
*/
async::CoroutineStarter initAsync() override;
/**
* Check if the stream context is initialized.
* *This particular implementation always returns `true`.*
* @return - `bool`.
*/
bool isInitialized() const override;
/**
* Get stream type.
* @return - &id:oatpp::data::stream::StreamType::STREAM_FINITE;.
*/
StreamType getStreamType() const override;
};
/**
* Wrapper over `std::FILE`.
*/
class FileInputStream : public InputStream {
public:
static oatpp::data::stream::DefaultInitializedContext DEFAULT_CONTEXT;
private:
std::FILE* m_file;
bool m_ownsFile;
@ -143,6 +109,8 @@ public:
* Wrapper over `std::FILE`.
*/
class FileOutputStream : public OutputStream {
public:
static oatpp::data::stream::DefaultInitializedContext DEFAULT_CONTEXT;
private:
std::FILE* m_file;
bool m_ownsFile;

View File

@ -30,6 +30,10 @@ namespace oatpp { namespace data{ namespace stream {
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Context
Context::Context(Properties&& properties)
: m_properties(std::forward<Properties>(properties))
{}
const Context::Properties& Context::getProperties() const {
return m_properties;
}
@ -38,6 +42,34 @@ Context::Properties& Context::getMutableProperties() {
return m_properties;
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// DefaultInitializedContext
DefaultInitializedContext::DefaultInitializedContext(StreamType streamType)
: m_streamType(streamType)
{}
DefaultInitializedContext::DefaultInitializedContext(StreamType streamType, Properties&& properties)
: Context(std::forward<Properties>(properties))
, m_streamType(streamType)
{}
void DefaultInitializedContext::init() {
// DO NOTHING
}
async::CoroutineStarter DefaultInitializedContext::initAsync() {
return nullptr;
}
bool DefaultInitializedContext::isInitialized() const {
return true;
}
StreamType DefaultInitializedContext::getStreamType() const {
return m_streamType;
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// IOStream

View File

@ -56,11 +56,31 @@ enum StreamType : v_int32 {
*/
class Context {
public:
/**
* Convenience typedef for &id:oatpp::data::share::LazyStringMap;.
*/
typedef oatpp::data::share::LazyStringMap<oatpp::data::share::StringKeyLabel> Properties;
protected:
private:
Properties m_properties;
protected:
/**
* `protected`. Get mutable additional optional context specific properties.
* @return - &l:Context::Properties;.
*/
Properties& getMutableProperties();
public:
/**
* Default constructor.
*/
Context() = default;
/**
* Constructor.
* @param properties - &l:Context::Properties;.
*/
Context(Properties&& properties);
/**
* Initialize stream context.
*/
@ -68,13 +88,13 @@ public:
/**
* Initialize stream context in an async manner.
* @return
* @return - &id:oatpp::async::CoroutineStarter;.
*/
virtual async::CoroutineStarter initAsync() = 0;
/**
* Check if the stream context is initialized.
* @return
* @return - `bool`.
*/
virtual bool isInitialized() const = 0;
@ -86,15 +106,58 @@ public:
/**
* Additional optional context specific properties.
* @return
* @return - &l:Context::Properties;.
*/
const Properties& getProperties() const;
};
/**
* Get mutable additional optional context specific properties.
* @return
* The default implementation for context with no initialization.
*/
Properties& getMutableProperties();
class DefaultInitializedContext : public oatpp::data::stream::Context {
private:
StreamType m_streamType;
public:
/**
* Constructor.
* @param streamType - &l:StreamType;.
*/
DefaultInitializedContext(StreamType streamType);
/**
* Constructor.
* @param streamType - &l:StreamType;.
* @param properties - &l:Context::Properties;.
*/
DefaultInitializedContext(StreamType streamType, Properties&& properties);
/**
* Initialize stream context. <br>
* *This particular implementation does nothing.*
*/
void init() override;
/**
* Initialize stream context in an async manner.
* *This particular implementation does nothing.*
* @return - &id:oatpp::async::CoroutineStarter;.
*/
async::CoroutineStarter initAsync() override;
/**
* Check if the stream context is initialized.
* *This particular implementation always returns `true`.*
* @return - `bool`.
*/
bool isInitialized() const override;
/**
* Get stream type.
* @return - &l:StreamType;.
*/
StreamType getStreamType() const override;
};

View File

@ -38,30 +38,11 @@
namespace oatpp { namespace network {
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// ConnectionStreamContext
ConnectionStreamContext ConnectionStreamContext::DEFAULT_CONTEXT;
void ConnectionStreamContext::init() {
// DO NOTHING
}
async::CoroutineStarter ConnectionStreamContext::initAsync() {
return nullptr;
}
bool ConnectionStreamContext::isInitialized() const {
return true;
}
oatpp::data::stream::StreamType ConnectionStreamContext::getStreamType() const {
return oatpp::data::stream::StreamType::STREAM_INFINITE;
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Connection
oatpp::data::stream::DefaultInitializedContext Connection::DEFAULT_CONTEXT(data::stream::StreamType::STREAM_INFINITE);
Connection::Connection(data::v_io_handle handle)
: m_handle(handle)
{
@ -290,7 +271,7 @@ oatpp::data::stream::IOMode Connection::getOutputStreamIOMode() {
}
oatpp::data::stream::Context* Connection::getOutputStreamContext() {
return &ConnectionStreamContext::DEFAULT_CONTEXT;
return &DEFAULT_CONTEXT;
}
void Connection::setInputStreamIOMode(oatpp::data::stream::IOMode ioMode) {
@ -302,7 +283,7 @@ oatpp::data::stream::IOMode Connection::getInputStreamIOMode() {
}
oatpp::data::stream::Context* Connection::getInputStreamContext() {
return &ConnectionStreamContext::DEFAULT_CONTEXT;
return &DEFAULT_CONTEXT;
}
void Connection::close(){

View File

@ -30,43 +30,12 @@
namespace oatpp { namespace network {
class ConnectionStreamContext : public oatpp::data::stream::Context {
public:
static ConnectionStreamContext DEFAULT_CONTEXT;
public:
/**
* Initialize stream context. <br>
* *This particular implementation does nothing.*
*/
void init() override;
/**
* Initialize stream context in an async manner.
* *This particular implementation does nothing.*
* @return - &id:oatpp::async::CoroutineStarter;.
*/
async::CoroutineStarter initAsync() override;
/**
* Check if the stream context is initialized.
* *This particular implementation always returns `true`.*
* @return - `bool`.
*/
bool isInitialized() const override;
/**
* Get stream type.
* @return - &id:oatpp::data::stream::StreamType::STREAM_INFINITE;.
*/
oatpp::data::stream::StreamType getStreamType() const override;
};
/**
* TCP Connection implementation. Extends &id:oatpp::base::Countable; and &id:oatpp::data::stream::IOStream;.
*/
class Connection : public oatpp::base::Countable, public oatpp::data::stream::IOStream {
private:
static oatpp::data::stream::DefaultInitializedContext DEFAULT_CONTEXT;
private:
data::v_io_handle m_handle;
#if defined(WIN32) || defined(_WIN32)

View File

@ -49,8 +49,9 @@ const char* const SimpleTCPConnectionProvider::ExtendedConnection::PROPERTY_PEER
const char* const SimpleTCPConnectionProvider::ExtendedConnection::PROPERTY_PEER_ADDRESS_FORMAT = "peer_address_format";
const char* const SimpleTCPConnectionProvider::ExtendedConnection::PROPERTY_PEER_PORT = "peer_port";
SimpleTCPConnectionProvider::ExtendedConnection::ExtendedConnection(data::v_io_handle handle)
SimpleTCPConnectionProvider::ExtendedConnection::ExtendedConnection(data::v_io_handle handle, data::stream::Context::Properties&& properties)
: Connection(handle)
, m_context(data::stream::StreamType::STREAM_INFINITE, std::forward<data::stream::Context::Properties>(properties))
{}
oatpp::data::stream::Context* SimpleTCPConnectionProvider::ExtendedConnection::getOutputStreamContext() {
@ -229,12 +230,11 @@ std::shared_ptr<oatpp::data::stream::IOStream> SimpleTCPConnectionProvider::getD
std::shared_ptr<oatpp::data::stream::IOStream> SimpleTCPConnectionProvider::getExtendedConnection() {
oatpp::String ipAddress;
int port;
const char* addrFormat;
struct sockaddr_storage clientAddress;
socklen_t clientAddressSize = sizeof(clientAddress);
data::stream::Context::Properties properties;
oatpp::data::v_io_handle handle = accept(m_serverHandle, (struct sockaddr*) &clientAddress, &clientAddressSize);
if (clientAddress.ss_family == AF_INET) {
@ -243,9 +243,9 @@ std::shared_ptr<oatpp::data::stream::IOStream> SimpleTCPConnectionProvider::getE
struct sockaddr_in* sockAddress = (struct sockaddr_in*) &clientAddress;
inet_ntop(AF_INET, &sockAddress->sin_addr, strIp, INET_ADDRSTRLEN);
ipAddress = (const char*) strIp;
addrFormat = "ipv4";
port = ntohs(sockAddress->sin_port);
properties.put(ExtendedConnection::PROPERTY_PEER_ADDRESS, oatpp::String((const char*) strIp));
properties.put(ExtendedConnection::PROPERTY_PEER_ADDRESS_FORMAT, "ipv4");
properties.put(ExtendedConnection::PROPERTY_PEER_PORT, oatpp::utils::conversion::int32ToStr(sockAddress->sin_port));
} else if (clientAddress.ss_family == AF_INET6) {
@ -253,9 +253,9 @@ std::shared_ptr<oatpp::data::stream::IOStream> SimpleTCPConnectionProvider::getE
struct sockaddr_in6* sockAddress = (struct sockaddr_in6*) &clientAddress;
inet_ntop(AF_INET6, &sockAddress->sin6_addr, strIp, INET6_ADDRSTRLEN);
ipAddress = (const char*) strIp;
addrFormat = "ipv6";
port = ntohs(sockAddress->sin6_port);
properties.put(ExtendedConnection::PROPERTY_PEER_ADDRESS, oatpp::String((const char*) strIp));
properties.put(ExtendedConnection::PROPERTY_PEER_ADDRESS_FORMAT, "ipv6");
properties.put(ExtendedConnection::PROPERTY_PEER_PORT, oatpp::utils::conversion::int32ToStr(sockAddress->sin6_port));
} else {
@ -271,16 +271,7 @@ std::shared_ptr<oatpp::data::stream::IOStream> SimpleTCPConnectionProvider::getE
}
if(prepareConnectionHandle(handle)) {
auto connection = std::make_shared<ExtendedConnection>(handle);
auto& properties = connection->getInputStreamContext()->getMutableProperties();
properties.put(ExtendedConnection::PROPERTY_PEER_ADDRESS, ipAddress);
properties.put(ExtendedConnection::PROPERTY_PEER_ADDRESS_FORMAT, addrFormat);
properties.put(ExtendedConnection::PROPERTY_PEER_PORT, oatpp::utils::conversion::int32ToStr(port));
return connection;
return std::make_shared<ExtendedConnection>(handle, std::move(properties));
}
return nullptr;

View File

@ -38,12 +38,6 @@ namespace oatpp { namespace network { namespace server {
class SimpleTCPConnectionProvider : public base::Countable, public ServerConnectionProvider {
public:
/**
* Connection stream context with additional data.
*/
class ExtendedConnectionContext : public oatpp::network::ConnectionStreamContext {
};
/**
* Connection with extra data - ex.: peer address.
*/
@ -55,14 +49,15 @@ public:
static const char* const PROPERTY_PEER_PORT;
protected:
ExtendedConnectionContext m_context;
data::stream::DefaultInitializedContext m_context;
public:
/**
* Constructor.
* @param handle - &id:oatpp::data::v_io_handle;.
* @param properties - &id:oatpp::data::stream::Context::Properties;.
*/
ExtendedConnection(data::v_io_handle handle);
ExtendedConnection(data::v_io_handle handle, data::stream::Context::Properties&& properties);
/**
* Get output stream context.