Skip to content

Commit a1edff9

Browse files
committed
Issue #22: Get source IP from TCP packages
1 parent 8914015 commit a1edff9

File tree

10 files changed

+78
-28
lines changed

10 files changed

+78
-28
lines changed
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
#ifndef ICLIENT_H
2+
#define ICLIENT_H
3+
4+
#include <cstdint>
5+
#include <string>
6+
#include <vector>
7+
8+
class IClient {
9+
protected:
10+
IClient() = default;
11+
12+
public:
13+
virtual bool Send(const std::vector<std::uint8_t>& data) = 0;
14+
virtual bool Receive(std::vector<std::uint8_t>& data) = 0;
15+
virtual int Init(std::string ip_address, int port) = 0;
16+
virtual void Destroy() = 0;
17+
virtual ~IClient() = default;
18+
19+
IClient(const IClient&) = delete;
20+
IClient& operator=(const IClient&) = delete;
21+
IClient(IClient&&) = delete;
22+
IClient& operator=(IClient&&) = delete;
23+
};
24+
25+
#endif

src/kpi_rover_ecu/include/IMUController.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ class IMUController {
1515
int Init();
1616
void SetEnable();
1717
void SetDisable();
18-
bool GetEnable();
1918
void Stop();
2019
std::vector<float> GetData();
2120
uint8_t GetId();

src/kpi_rover_ecu/include/KPIRoverECU.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
static constexpr std::uint32_t kTimerPrecision = 100000; // 100 milliseconds in microsecond (for timer)
1515
static constexpr std::uint32_t kOneSecondMicro = 1000000; // 1 s in microseconds
1616
static constexpr std::uint32_t kOneSecondMilli = 1000; // 1 s in milliseconds
17-
static constexpr std::uint32_t kTimeStop = 5; // 5 seconds
17+
static constexpr std::uint32_t kTimeStop = 1; // 5 seconds
1818
static constexpr std::uint32_t k16MaxCount = 65535;
1919

2020
class KPIRoverECU {

src/kpi_rover_ecu/include/TCPTransport.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#ifndef TCPTRANSPORT_H
22
#define TCPTRANSPORT_H
33

4+
#include <arpa/inet.h>
45
#include <atomic>
56
#include <cstdint>
67
#include <cstring>
@@ -28,9 +29,14 @@ class TCPTransport : public ITransport {
2829
int Init() override;
2930
void Destroy() override;
3031

32+
std::string GetSourceIp();
33+
int GetSourcePort();
34+
3135
private:
3236
int sockfd_, client_sockfd_;
3337
char* server_address_;
38+
char source_address_[INET_ADDRSTRLEN];
39+
int source_port_;
3440
int server_portnum_;
3541
std::atomic<bool> running_;
3642
std::thread acceptThread_;

src/kpi_rover_ecu/include/UDPClient.h

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,31 +5,31 @@
55
#include <netinet/in.h>
66

77
#include <cstdint>
8+
#include <string>
89
#include <vector>
910

10-
#include "ITransport.h"
11+
#include "IClient.h"
1112

12-
class UDPClient : public ITransport {
13+
class UDPClient : public IClient {
1314
public:
14-
UDPClient(const char* ip_address, int port);
15+
UDPClient();
1516

1617
UDPClient(const UDPClient&) = delete;
1718
UDPClient& operator=(const UDPClient&) = delete;
1819
UDPClient(UDPClient&&) = delete;
1920
UDPClient& operator=(UDPClient&&) = delete;
2021

21-
int Init() override;
22+
int Init(std::string ip_address, int port) override;
2223
bool Send(const std::vector<std::uint8_t>& data) override;
2324
bool Receive(std::vector<std::uint8_t>& data) override;
24-
void Start() override;
2525
void Destroy() override;
2626
~UDPClient() override;
2727

2828
private:
2929
int sockfd_;
3030
sockaddr_in serverStruct_;
31-
char* server_address_;
32-
int client_portnum_;
31+
char server_address_[INET_ADDRSTRLEN];
32+
int server_portnum_;
3333
};
3434

3535
#endif

src/kpi_rover_ecu/src/IMUController.cpp

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,6 @@ void IMUController::SetEnable() { isStarted_ = true; }
2828

2929
void IMUController::SetDisable() { isStarted_ = false; }
3030

31-
bool IMUController::GetEnable() { return isStarted_; }
32-
3331
std::vector<float> IMUController::GetData() {
3432
if (!isStarted_) {
3533
return {};

src/kpi_rover_ecu/src/KPIRoverECU.cpp

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -40,14 +40,25 @@ bool KPIRoverECU::Start() {
4040
void KPIRoverECU::IMUThreadFucntion(IMUController *workClass) {
4141
uint16_t packet_number = 0;
4242

43+
std::string destination_address = "";
44+
int destination_port = 0;
45+
46+
while (destination_address.empty()) {
47+
destination_address = tcp_transport_->GetSourceIp();
48+
destination_port = tcp_transport_->GetSourcePort();
49+
}
50+
51+
udp_client_->Init(destination_address, destination_port);
52+
4353
while (runningProcess_) {
44-
if (workClass->GetEnable()) {
54+
const std::vector<float> kImuData = workClass->GetData();
55+
if (!kImuData.empty()) {
4556
if (packet_number == k16MaxCount) {
4657
packet_number = 0;
4758
}
4859
packet_number += 1;
4960

50-
const std::vector<float> kImuData = workClass->GetData();
61+
//std::cout << "Client connected " << tcp_transport_->GetSourceIp() << ":" << tcp_transport_->GetSourcePort() << '!' << '\n';
5162
std::vector<uint8_t> send_val;
5263
send_val.push_back(workClass->GetId());
5364

@@ -102,9 +113,7 @@ void KPIRoverECU::ProcessingThreadFunction() {
102113
while (runningProcess_) {
103114
std::vector<uint8_t> message;
104115
if (tcp_transport_->Receive(message)) {
105-
if (!imu_controller_->GetEnable()) {
106-
imu_controller_->SetEnable();
107-
}
116+
imu_controller_->SetEnable();
108117

109118
counter_.store(GetCounter());
110119
const std::vector<uint8_t> kReturnMessage = protocol_handler_->HandleMessage(message);

src/kpi_rover_ecu/src/TCPTransport.cpp

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,8 @@ TCPTransport::TCPTransport(const char *ip_address, int port)
2020
client_sockfd_(-1),
2121
server_portnum_(port),
2222
running_(true),
23-
server_address_(new char[strlen(ip_address) + 1]) {
23+
server_address_(new char[strlen(ip_address) + 1]),
24+
source_port_(-1) {
2425
strncpy(server_address_, ip_address, strlen(ip_address) + 1);
2526
}
2627

@@ -38,6 +39,7 @@ TCPTransport::~TCPTransport() {
3839
int TCPTransport::Init() {
3940
struct sockaddr_in serv_addr = {};
4041
int reuseaddr = 1;
42+
source_address_[0] = '\0';
4143

4244
sockfd_ = socket(AF_INET, SOCK_STREAM, 0);
4345

@@ -73,11 +75,16 @@ int TCPTransport::Init() {
7375

7476
void TCPTransport::Start() {
7577
acceptThread_ = std::thread([this]() {
78+
struct sockaddr_in client_addr;
79+
socklen_t client_add_size = sizeof(client_addr);
80+
7681
while (running_) {
7782
std::cout << "Waiting for connection..." << '\n';
78-
client_sockfd_ = accept(sockfd_, nullptr, nullptr);
83+
client_sockfd_ = accept(sockfd_, reinterpret_cast<sockaddr*>(&client_addr), &client_add_size);
7984
if (client_sockfd_ >= 0) {
80-
std::cout << "Client connected." << '\n';
85+
inet_ntop(AF_INET, &client_addr.sin_addr, source_address_, INET_ADDRSTRLEN);
86+
source_port_ = static_cast<int>(ntohs(client_addr.sin_port));
87+
//std::cout << "Client connected " << source_address_ << ":" << ntohs(client_addr.sin_port) << '\n';
8188
while (true) { // Use true instead of 1
8289
std::uint8_t buffer[kBufferSize];
8390
const ssize_t kBytesReceived = recv(client_sockfd_, buffer, sizeof(buffer), 0);
@@ -105,6 +112,10 @@ bool TCPTransport::Send(const std::vector<std::uint8_t> &data) {
105112

106113
bool TCPTransport::Receive(std::vector<std::uint8_t> &data) { return messageQueue_.Pop(data, kTimeoutMs); }
107114

115+
std::string TCPTransport::GetSourceIp() { return std::string(source_address_); }
116+
117+
int TCPTransport::GetSourcePort() { return source_port_; }
118+
108119
void TCPTransport::Destroy() {
109120
running_ = false;
110121
shutdown(sockfd_, SHUT_RDWR);

src/kpi_rover_ecu/src/UDPClient.cpp

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,16 +10,18 @@
1010
#include <cstdio>
1111
#include <cstring>
1212
#include <iostream>
13+
#include <string>
1314
#include <vector>
1415

15-
UDPClient::UDPClient(const char *ip_address, int port)
16-
: sockfd_(-1), client_portnum_(port), server_address_(new char[strlen(ip_address) + 1]) {
17-
strncpy(server_address_, ip_address, strlen(ip_address) + 1);
18-
}
16+
UDPClient::UDPClient()
17+
: sockfd_(-1), server_portnum_(0), server_address_({0}) {}
18+
19+
int UDPClient::Init(std::string ip_address, int port) {
20+
std::strncpy(server_address_, ip_address.c_str(), INET_ADDRSTRLEN);
21+
server_address_[INET_ADDRSTRLEN - 1] = '\0';
1922

20-
void UDPClient::Start() {}
23+
server_portnum_ = port;
2124

22-
int UDPClient::Init() {
2325
std::cout << "initialize socket" << '\n';
2426
sockfd_ = socket(AF_INET, SOCK_DGRAM, 0);
2527
if (sockfd_ == -1) {
@@ -30,7 +32,7 @@ int UDPClient::Init() {
3032
memset(&serverStruct_, 0, sizeof(serverStruct_));
3133
serverStruct_.sin_family = AF_INET;
3234
serverStruct_.sin_addr.s_addr = inet_addr(server_address_); // Server IP address (localhost)
33-
serverStruct_.sin_port = htons(client_portnum_);
35+
serverStruct_.sin_port = htons(server_portnum_);
3436

3537
return 0;
3638
}

src/kpi_rover_ecu/src/main.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -86,9 +86,9 @@ int main(int argc, char* argv[]) {
8686
}
8787

8888
TCPTransport tcp_transport(server_address, server_portnum);
89-
UDPClient udp_client(udp_server_address, udp_server_portnum);
89+
UDPClient udp_client;
9090

91-
if (tcp_transport.Init() == -1 || udp_client.Init() == -1) {
91+
if (tcp_transport.Init() == -1) {
9292
std::cout << "[ERROR] Error creating socket" << '\n';
9393
tcp_transport.Destroy();
9494
udp_client.Destroy();

0 commit comments

Comments
 (0)