SOLA
Loading...
Searching...
No Matches
server.h
1// Copyright 2023 The SOLA authors
2//
3// This file is part of DAISI.
4//
5// DAISI is free software: you can redistribute it and/or modify it under the terms of the GNU
6// General Public License as published by the Free Software Foundation; version 2.
7//
8// DAISI is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even
9// the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
10// Public License for more details.
11//
12// You should have received a copy of the GNU General Public License along with DAISI. If not, see
13// <https://www.gnu.org/licenses/>.
14//
15// SPDX-License-Identifier: GPL-2.0-only
16
17#ifndef DAISI_NETWORK_TCP_SERVER_H_
18#define DAISI_NETWORK_TCP_SERVER_H_
19
20#include <cstdint>
21#include <functional>
22#include <string>
23
24#include "network_tcp/definitions.h"
25#include "network_tcp/framing_manager.h"
26#include "ns3/socket.h"
27
28namespace daisi::network_tcp {
29
31 std::function<void(TcpSocketHandle, const std::string &msg)> new_msg_cb;
32 std::function<void(TcpSocketHandle, const Ipv4 &ip, uint16_t port)> client_connected_cb;
33 std::function<void(TcpSocketHandle)> client_disconnected_cb;
34};
35
39class Server {
40public:
41 explicit Server(ServerCallbacks callbacks);
42
43 Server(const Ipv4 &ip, ServerCallbacks callbacks);
44
45 ~Server();
46
47 // Forbid copy/move operations
48 Server(const Server &) = delete;
49 Server &operator=(const Server &) = delete;
50 Server(const Server &&) = delete;
51 Server &operator=(Server &&) = delete;
52
58 void send(TcpSocketHandle receiver, const std::string &msg);
59
60 uint16_t getPort() const;
61
62 Ipv4 getIP() const;
63
64private:
65 ns3::Ptr<ns3::Socket> listening_socket_;
66
67 std::string ip_;
68 uint16_t port_;
69
70 const ServerCallbacks callbacks_;
71
72 struct TcpConnection {
73 ns3::Ptr<ns3::Socket> socket;
74 FramingManager manager;
75 };
76
77 // Handle number for next incoming connection
78 TcpSocketHandle next_handle_ = 1;
79
80 // Map of all currently open connections
81 std::unordered_map<TcpSocketHandle, TcpConnection> connections_;
82
83 void readFromSocket(TcpSocketHandle handle, ns3::Ptr<ns3::Socket> socket);
84 void processPacket(ns3::Ptr<ns3::Packet> packet, TcpSocketHandle sender);
85
86 void handleClose(TcpSocketHandle handle, ns3::Ptr<ns3::Socket> socket);
87
88 bool connectionRequest(ns3::Ptr<ns3::Socket>, const ns3::Address &);
89 void newConnectionCreated(ns3::Ptr<ns3::Socket> socket, const ns3::Address &addr);
90
91 void successfulConnect(ns3::Ptr<ns3::Packet>);
92 void failedConnect(ns3::Ptr<ns3::Packet>);
93};
94} // namespace daisi::network_tcp
95
96#endif // DAISI_NETWORK_TCP_SERVER_H_
Definition framing_manager.h:27
Definition server.h:39
void send(TcpSocketHandle receiver, const std::string &msg)
Definition server.cpp:64