Skip to content

Commit cefd36b

Browse files
authored
Merge branch 'dev' into feature/doc
2 parents 0d1dc24 + 24de35d commit cefd36b

18 files changed

+510
-68
lines changed

.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,6 @@
3232
*.app
3333

3434
# Debug files
35-
cmake-build-debug
35+
cmake-build-*
3636
.idea
3737
build

CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ add_executable(STIPTest
4747
tests/testStipSend.cpp
4848
src/protocol/Connection.cpp
4949
src/protocol/Session.cpp
50+
src/protocol/SessionKiller.cpp
5051
)
5152

5253
# add sources dir

conan_provider.cmake

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,28 @@
11
# This file is managed by Conan, contents will be overwritten.
22
# To keep your changes, remove these comment lines, but the plugin won't be able to modify your requirements
33

4+
# The MIT License (MIT)
5+
#
6+
# Copyright (c) 2024 JFrog
7+
#
8+
# Permission is hereby granted, free of charge, to any person obtaining a copy
9+
# of this software and associated documentation files (the "Software"), to deal
10+
# in the Software without restriction, including without limitation the rights
11+
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12+
# copies of the Software, and to permit persons to whom the Software is
13+
# furnished to do so, subject to the following conditions:
14+
#
15+
# The above copyright notice and this permission notice shall be included in all
16+
# copies or substantial portions of the Software.
17+
#
18+
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19+
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20+
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21+
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22+
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23+
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
24+
# SOFTWARE.
25+
426
set(CONAN_MINIMUM_VERSION 2.0.5)
527

628

@@ -628,4 +650,3 @@ if(NOT _cmake_program)
628650
get_filename_component(PATH_TO_CMAKE_BIN "${CMAKE_COMMAND}" DIRECTORY)
629651
set(PATH_TO_CMAKE_BIN "${PATH_TO_CMAKE_BIN}" CACHE INTERNAL "Path where the CMake executable is")
630652
endif()
631-

examples/client.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ int main() {
2626
STIPClient client(socket);
2727
client.startListen();
2828

29-
Connection *connection = client.connect(server_endpoint);
29+
Connection *connection = client.connect(server_endpoint, 1000, 3);
3030
std::cout << "Connection accepted\n\n" << std::endl;
3131
// connection->send("Hello, I'm Ilya");
3232

src/client/STIPClient.cpp

Lines changed: 48 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,13 @@
44

55

66
#include "STIPClient.h"
7+
#include <future>
8+
#include <chrono>
9+
10+
#include "protocol/errors/STIP_errors.h"
11+
#include "protocol/Commands.h"
12+
13+
using namespace std::chrono_literals;
714

815
namespace STIP {
916

@@ -24,39 +31,62 @@ namespace STIP {
2431
return;
2532
// throw boost::system::system_error(error);
2633
}
27-
std::cout << "Получен запрос от " << remote_endpoint.address() << ":" << remote_endpoint.port() << std::endl;
34+
std::cout << "Получен запрос от " << remote_endpoint.address() << ":" << remote_endpoint.port()
35+
<< std::endl;
2836

2937
connectionManager->accept(remote_endpoint, packet[0]);
3038
}
3139
}
3240

3341

34-
Connection *STIPClient::connect(udp::endpoint &targetEndpoint) {
35-
// TODO: Add error handling
42+
Connection *STIPClient::connect(udp::endpoint &targetEndpoint, std::chrono::milliseconds timeout) {
3643

3744
auto *connection = new Connection(targetEndpoint, socket);
3845
this->connectionManager->addConnection(targetEndpoint, connection);
39-
STIP_PACKET packet[1] = {};
40-
packet[0].header.command = 100;
41-
packet[0].header.size = sizeof(int);
4246

43-
this->socket->send_to(boost::asio::buffer(packet, packet[0].header.size), targetEndpoint);
44-
bool result = false;
45-
STIP_PACKET response = connection->getPacket(result);
4647

47-
// TODO: Add better error handling
48-
if (!result) {
49-
throw std::runtime_error("Error while waiting for response");
50-
}
51-
if (response.header.command == 101) {
52-
packet[0].header.command = 102;
53-
packet[0].header.size = sizeof(int);
54-
this->socket->send_to(boost::asio::buffer(packet, packet[0].header.size), targetEndpoint);
48+
STIP_PACKET initPacket[1] = {};
49+
initPacket[0].header.command = Command::CONNECTION_SYN;
50+
initPacket[0].header.size = sizeof(int);
51+
52+
this->socket->send_to(boost::asio::buffer(initPacket, initPacket[0].header.size), targetEndpoint);
53+
std::future<bool> response_future = std::async(std::launch::async, [connection]() {
54+
bool result = false;
55+
STIP_PACKET response{};
56+
do {
57+
response = connection->getPacket(result);
58+
} while (result && response.header.command != Command::CONNECTION_SYN_ACK);
59+
60+
return result;
61+
});
62+
63+
std::future_status status;
64+
do {
65+
switch (status = response_future.wait_for(timeout); status) {
66+
case std::future_status::timeout:
67+
connection->cancelPacketWaiting();
68+
connectionManager->remove(targetEndpoint);
69+
delete connection;
70+
throw STIP::errors::STIPTimeoutException("Timeout while connecting");
71+
break;
72+
}
73+
} while (status == std::future_status::deferred);
74+
75+
if (response_future.get()) {
76+
initPacket[0].header.command = Command::CONNECTION_ACK;
77+
initPacket[0].header.size = sizeof(int);
78+
this->socket->send_to(boost::asio::buffer(initPacket, initPacket[0].header.size), targetEndpoint);
5579
connection->setConnectionStatus(102);
5680
connection->startProcessing();
5781
return connection;
5882
}
59-
throw std::runtime_error("Error while connecting");
83+
}
84+
85+
Connection *STIPClient::connect(udp::endpoint &targetEndpoint) {
86+
return connect(
87+
targetEndpoint,
88+
std::chrono::milliseconds(10000)
89+
);
6090
}
6191

6292
void STIPClient::startListen() {

src/client/STIPClient.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ namespace STIP {
1818

1919
void stopListen();
2020

21+
Connection *connect(udp::endpoint &targetEndpoint, std::chrono::milliseconds timeout);
22+
2123
Connection *connect(udp::endpoint &targetEndpoint);
2224

2325
private:

src/protocol/Commands.h

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
//
2+
// Created by Serge on 12.04.2024.
3+
//
4+
5+
#ifndef RABBIT_COMMANDS_H
6+
#define RABBIT_COMMANDS_H
7+
#include <ostream>
8+
9+
namespace STIP {
10+
enum class Command : int {
11+
MSG_INIT_REQUEST = 0,
12+
MSG_INIT_RESPONSE_SUCCESS = 1,
13+
MSG_INIT_RESPONSE_FAILURE = 2,
14+
15+
MSG_SEND_DATA_PART = 3,
16+
17+
MSG_REQUEST_ALL_RECEIVED = 4,
18+
MSG_RESPONSE_ALL_RECEIVED = 5,
19+
MSG_RESPONSE_RESEND = 6,
20+
21+
MSG_KILLED = 7,
22+
23+
24+
PING_ASK = 10,
25+
PING_ANSWER = 11,
26+
27+
28+
CONNECTION_SYN = 100,
29+
CONNECTION_SYN_ACK = 101,
30+
CONNECTION_ACK = 102,
31+
CONNECTION_FIN = 103,
32+
};
33+
34+
inline std::ostream& operator<<(std::ostream & os, const Command &command) {
35+
switch (command) {
36+
case Command::MSG_INIT_REQUEST:
37+
os << "MSG_INIT_REQUEST";
38+
break;
39+
case Command::MSG_INIT_RESPONSE_SUCCESS:
40+
os << "MSG_INIT_RESPONSE_SUCCESS";
41+
break;
42+
case Command::MSG_INIT_RESPONSE_FAILURE:
43+
os << "MSG_INIT_RESPONSE_FAILURE";
44+
break;
45+
case Command::MSG_SEND_DATA_PART:
46+
os << "MSG_SEND_DATA_PART";
47+
break;
48+
case Command::MSG_REQUEST_ALL_RECEIVED:
49+
os << "MSG_REQUEST_ALL_RECEIVED";
50+
break;
51+
case Command::MSG_RESPONSE_ALL_RECEIVED:
52+
os << "MSG_RESPONSE_ALL_RECEIVED";
53+
break;
54+
case Command::MSG_RESPONSE_RESEND:
55+
os << "MSG_RESPONSE_RESEND";
56+
break;
57+
case Command::MSG_KILLED:
58+
os << "MSG_KILLED";
59+
break;
60+
case Command::PING_ASK:
61+
os << "PING_ASK";
62+
break;
63+
case Command::PING_ANSWER:
64+
os << "PING_ANSWER";
65+
break;
66+
case Command::CONNECTION_SYN:
67+
os << "CONNECTION_SYN";
68+
break;
69+
case Command::CONNECTION_SYN_ACK:
70+
os << "CONNECTION_SYN_ACK";
71+
break;
72+
case Command::CONNECTION_ACK:
73+
os << "CONNECTION_ACK";
74+
break;
75+
case Command::CONNECTION_FIN:
76+
os << "CONNECTION_FIN";
77+
break;
78+
}
79+
os << " (" << static_cast<int>(command) << ")";
80+
return os;
81+
}
82+
}
83+
84+
#endif //RABBIT_COMMANDS_H

0 commit comments

Comments
 (0)