SNode.C
Loading...
Searching...
No Matches
SocketConnection.cpp
Go to the documentation of this file.
1/*
2 * SNode.C - a slim toolkit for network communication
3 * Copyright (C) Volker Christian <me@vchrist.at>
4 * 2020, 2021, 2022, 2023, 2024, 2025
5 *
6 * This program is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU Lesser General Public License as published
8 * by the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 */
19
20#include "core/socket/stream/SocketConnection.h"
21
22#include "core/socket/stream/SocketContext.h"
23#include "core/socket/stream/SocketContextFactory.h"
24
25#ifndef DOXYGEN_SHOULD_SKIP_THIS
26
27#include "log/Logger.h"
28
29#endif // DOXYGEN_SHOULD_SKIP_THIS
30
31namespace core::socket::stream {
32
33 SocketConnection::SocketConnection(const std::string& instanceName, int fd, const std::string& configuredServer)
35 , connectionName("[" + std::to_string(fd) + "]" + (!instanceName.empty() ? " " : "") + instanceName)
37 }
38
41
42 void SocketConnection::switchSocketContext(SocketContext* newSocketContext) {
43 this->newSocketContext = newSocketContext;
44 }
45
46 void SocketConnection::setSocketContext(SocketContext* socketContext) {
47 if (socketContext != nullptr) { // Perform a pending SocketContextSwitch
48 LOG(DEBUG) << connectionName << ": Connecting SocketContext";
49 this->socketContext = socketContext;
50 socketContext->onConnected();
51 } else {
52 LOG(ERROR) << connectionName << ": Connecting SocketContext failed: no new SocketContext";
53 }
54 }
55
56 void SocketConnection::sendToPeer(const std::string& data) {
57 sendToPeer(data.data(), data.size());
58 }
59
60 void SocketConnection::sentToPeer(const std::vector<uint8_t>& data) {
61 sendToPeer(reinterpret_cast<const char*>(data.data()), data.size());
62 }
63
64 void SocketConnection::sentToPeer(const std::vector<char>& data) {
65 sendToPeer(data.data(), data.size());
66 }
67
69 return instanceName;
70 }
71
73 return connectionName;
74 }
75
77 return configuredServer;
78 }
79
80 void SocketConnection::connectSocketContext(const std::shared_ptr<SocketContextFactory>& socketContextFactory) {
81 SocketContext* socketContext = socketContextFactory->create(this);
82
83 if (socketContext != nullptr) {
84 LOG(DEBUG) << connectionName << ": Creating SocketContext successful";
85 setSocketContext(socketContext);
86 } else {
87 LOG(ERROR) << connectionName << ": Failed creating SocketContext";
88 close();
89 }
90 }
91
93 if (socketContext != nullptr) {
94 socketContext->onDisconnected();
95 delete socketContext;
96
97 LOG(DEBUG) << connectionName << ": SocketContext disconnected";
98 }
99 }
100
101} // namespace core::socket::stream
const std::string & getConfiguredServer() const
void sentToPeer(const std::vector< uint8_t > &data)
const std::string & getConnectionName() const
void sendToPeer(const std::string &data)
SocketConnection(const std::string &instanceName, int fd, const std::string &configuredServer)
const std::string & getInstanceName() const