-
Notifications
You must be signed in to change notification settings - Fork 4
/
main.cpp
126 lines (100 loc) · 4.74 KB
/
main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
/********************************************************************************
* Copyright (c) 2024 Continental Corporation
*
* This program and the accompanying materials are made available under the
* terms of the Apache License, Version 2.0 which is available at
* https://www.apache.org/licenses/LICENSE-2.0.
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations
* under the License.
*
* SPDX-License-Identifier: Apache-2.0
********************************************************************************/
#include <array>
#include <chrono>
#include <iostream>
#include <memory>
#include <string>
#include <asio.hpp>
#include <ecaludp/socket.h>
#include <ecaludp/socket_npcap.h>
std::shared_ptr<asio::io_context> io_context_;
std::shared_ptr<ecaludp::Socket> send_socket_;
std::shared_ptr<ecaludp::SocketNpcap> receive_socket_;
std::shared_ptr<asio::steady_timer> send_timer_;
void send_package()
{
auto douglas_adams_buffer_1 = std::make_shared<std::string>("In the beginning the Universe was created.");
auto douglas_adams_buffer_2 = std::make_shared<std::string>(" ");
auto douglas_adams_buffer_3 = std::make_shared<std::string>("This had made many people very angry and has been widely regarded as a bad move.");
send_socket_->async_send_to({ asio::buffer(*douglas_adams_buffer_1), asio::buffer(*douglas_adams_buffer_2), asio::buffer(*douglas_adams_buffer_3) }
, asio::ip::udp::endpoint(asio::ip::address_v4::loopback()
, 14000)
, [douglas_adams_buffer_1, douglas_adams_buffer_2, douglas_adams_buffer_3](asio::error_code ec)
{
if (ec)
{
std::cout << "Error sending: " << ec.message() << std::endl;
return;
}
send_timer_->expires_from_now(std::chrono::milliseconds(500));
send_timer_->async_wait([](asio::error_code ec)
{
if (ec)
{
std::cout << "Error waiting: " << ec.message() << std::endl;
return;
}
send_package();
});
});
}
void receive_package()
{
auto sender_endpoint = std::make_shared<asio::ip::udp::endpoint>();
receive_socket_->async_receive_from(*sender_endpoint
, [sender_endpoint](const std::shared_ptr<ecaludp::OwningBuffer>& buffer, const ecaludp::Error& error)
{
if (error)
{
std::cout << "Error receiving: " << error.ToString() << std::endl;
return;
}
std::string received_string(static_cast<const char*>(buffer->data()), buffer->size());
std::cout << "Received " << buffer->size() << " bytes from " << sender_endpoint->address().to_string() << ":" << sender_endpoint->port() << ": " << received_string << std::endl;
receive_package();
});
}
int main(int argc, char** argv)
{
std::cout << "Starting...\n";
io_context_ = std::make_shared<asio::io_context>();
send_socket_ = std::make_shared<ecaludp::Socket> (*io_context_, std::array<char, 4>{'E', 'C', 'A', 'L'});
receive_socket_ = std::make_shared<ecaludp::SocketNpcap>(std::array<char, 4>{'E', 'C', 'A', 'L'});
{
asio::error_code ec;
send_socket_->open(asio::ip::udp::v4(), ec);
if (ec)
{
std::cout << "Error opening socket: " << ec.message() << std::endl;
return -1;
}
}
{
bool bind_success = receive_socket_->bind(asio::ip::udp::endpoint(asio::ip::address_v4::loopback(), 14000));
if (!bind_success)
{
std::cout << "Error binding socket" << std::endl;
return -1;
}
}
send_timer_ = std::make_shared<asio::steady_timer>(*io_context_);
asio::io_context::work work(*io_context_);
receive_package();
send_package();
io_context_->run();
return 0;
}