SOLA
Loading...
Searching...
No Matches
client.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_CLIENT_H_
18#define DAISI_NETWORK_TCP_CLIENT_H_
19
20#include <cstdint>
21#include <functional>
22#include <memory>
23#include <string>
24
25#include "network_tcp/definitions.h"
26#include "network_tcp/framing_manager.h"
27#include "ns3/socket.h"
28
29namespace daisi::network_tcp {
30
32 std::function<void(const std::string &msg)> new_msg_cb;
33 std::function<void()> connected_cb;
34 std::function<void()> disconnected_cb;
35};
36
40class Client {
41public:
42 Client(ClientCallbacks callbacks, const Endpoint &endpoint);
43
44 Client(const Ipv4 &own_ip, ClientCallbacks callbacks, const Endpoint &endpoint);
45
46 ~Client();
47
48 // Forbid copy/move operations
49 Client(const Client &) = delete;
50 Client &operator=(const Client &) = delete;
51 Client(const Client &&) = delete;
52 Client &operator=(Client &&) = delete;
53
58 void send(const std::string &msg);
59
60 uint16_t getPort() const;
61
62 Ipv4 getIP() const;
63
64 bool isConnected() const;
65
66private:
67 void readFromSocket(ns3::Ptr<ns3::Socket> socket);
68 void processPacket(ns3::Ptr<ns3::Packet> packet);
69
70 void closedSocket(ns3::Ptr<ns3::Socket>);
71 void connectedSuccessful(ns3::Ptr<ns3::Socket>);
72 void connectionFailed(ns3::Ptr<ns3::Socket>);
73
74 ns3::Ptr<ns3::Socket> socket_;
75
76 bool connected_ = false;
77
78 FramingManager manager_;
79
80 Ipv4 ip_;
81 uint16_t port_;
82
83 const ClientCallbacks callbacks_;
84};
85} // namespace daisi::network_tcp
86
87#endif // DAISI_NETWORK_TCP_CLIENT_H_
Definition client.h:40
void send(const std::string &msg)
Definition client.cpp:67
Definition framing_manager.h:27
Definition definitions.h:28