2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
42#include "core/socket/stream/SocketConnection.h"
44#include "core/socket/stream/SocketContext.h"
45#include "core/socket/stream/SocketContextFactory.h"
46#include "net/config/ConfigInstance.h"
48#ifndef DOXYGEN_SHOULD_SKIP_THIS
50#include "log/Logger.h"
60namespace core::socket::
stream {
73 if (socketContext !=
nullptr) {
76 LOG(DEBUG) <<
connectionName <<
": SocketContext switch completed";
80 LOG(ERROR) <<
connectionName <<
": SocketContext switch failed: no new SocketContext";
89 sendToPeer(reinterpret_cast<
const char*>(data.data())
, data.size()
);
123 if (socketContext !=
nullptr) {
124 LOG(DEBUG) <<
connectionName <<
": SocketContext created successful";
127 LOG(ERROR) <<
connectionName <<
": SocketContext failed to create";
133 const std::time_t time = std::chrono::system_clock::to_time_t(timePoint);
134 std::tm* tm_ptr = std::gmtime(&time);
137 std::string onlineSince =
"Formatting error";
140 if (std::strftime(buffer,
sizeof(buffer),
"%Y-%m-%d %H:%M:%S", tm_ptr) > 0) {
141 onlineSince = std::string(buffer) +
" UTC";
148 const std::chrono::time_point<std::chrono::system_clock>& later) {
149 using seconds_duration_type = std::chrono::duration<std::chrono::seconds::rep>::rep;
151 const seconds_duration_type totalSeconds = std::chrono::duration_cast<std::chrono::seconds>(later - bevore).count();
154 const seconds_duration_type days = totalSeconds / 86400;
155 seconds_duration_type remainder = totalSeconds % 86400;
156 const seconds_duration_type hours = remainder / 3600;
157 remainder = remainder % 3600;
158 const seconds_duration_type minutes = remainder / 60;
159 const seconds_duration_type seconds = remainder % 60;
162 std::ostringstream oss;
164 oss << days <<
" day" << (days == 1 ?
"" :
"s") <<
", ";
166 oss << std::setw(2) << std::setfill(
'0') << hours <<
":" << std::setw(2) << std::setfill(
'0') << minutes <<
":" << std::setw(2)
167 << std::setfill(
'0') << seconds;
std::string connectionName
virtual void sendToPeer(const char *chunk, std::size_t chunkLen)=0
SocketConnection(int fd, const net::config::ConfigInstance *config)
core::socket::stream::SocketContext * socketContext
void connectSocketContext(const std::shared_ptr< SocketContextFactory > &socketContextFactory)
static std::string timePointToString(const std::chrono::time_point< std::chrono::system_clock > &timePoint)
virtual ~SocketConnection()
void sentToPeer(const std::vector< char > &data)
void sentToPeer(const std::vector< uint8_t > &data)
static std::string durationToString(const std::chrono::time_point< std::chrono::system_clock > &bevore, const std::chrono::time_point< std::chrono::system_clock > &later=std::chrono::system_clock::now())
const std::string & getConnectionName() const
void setSocketContext(SocketContext *socketContext)
void sendToPeer(const std::string &data)
std::string getOnlineSince() const
std::string getOnlineDuration() const
SocketContext * getSocketContext() const
std::chrono::time_point< std::chrono::system_clock > onlineSinceTimePoint
const net::config::ConfigInstance * getConfig() const
const std::string & getInstanceName() const
virtual core::socket::stream::SocketContext * create(core::socket::stream::SocketConnection *socketConnection)=0
const std::string & getInstanceName() const