Skip to content

Commit d912e84

Browse files
authored
Merge pull request #484 from HyperloopUPV-H8/fix-ethernet-mess
Mess fixed
2 parents 7494baa + 899120c commit d912e84

5 files changed

Lines changed: 4 additions & 103 deletions

File tree

Src/HALAL/HALAL.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@ void HALAL::start(IPV4 ip, IPV4 subnet_mask, IPV4 gateway,
8383
#else
8484
// Simulator start
8585
void HALAL::start(IPV4 ip, IPV4 subnet_mask, IPV4 gateway, UART::Peripheral& printf_peripheral) {
86+
SharedMemory::start();
8687
Ethernet::inscribe();
8788
Pin::start();
8889
ADC::start();

Src/HALALMock/Services/Communication/Ethernet/Ethernet.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ void Ethernet::inscribe() {
5858
return;
5959
}
6060
}
61+
is_ready = true;
6162
} else {
6263
LOG_ERROR("Unable to inscribe Ethernet because is already ready!");
6364
}

Src/HALALMock/Services/Communication/Ethernet/TCP/ServerSocket.cpp

Lines changed: 0 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -292,30 +292,3 @@ void ServerSocket::close_inside_thread() {
292292
listening_sockets[local_port] = this;
293293
state = CLOSED;
294294
}
295-
void ServerSocket::configure_server_socket_and_listen() {
296-
create_server_socket();
297-
if (!configure_server_socket()) {
298-
LOG_ERROR("Can't configure ServerSocket");
299-
close();
300-
return;
301-
}
302-
if (listen(server_socket_fd, SOMAXCONN) < 0) {
303-
LOG_ERROR("Can't listen");
304-
close();
305-
return;
306-
}
307-
state = LISTENING;
308-
listening_sockets[local_port] = this;
309-
// create a thread to listen
310-
listening_thread = std::jthread[&]() {
311-
// solo aceptamos una conexion
312-
struct sockaddr_in client_addr;
313-
socklen_t client_len = sizeof(client_addr);
314-
client_fd = accept(server_socket_fd, (struct sockaddr*)&client_addr,
315-
&client_len);
316-
if (client_fd > 0) {
317-
if (!accept_callback(client_fd, client_addr)) {
318-
LOG_ERROR("Something went wrong in accept_callback");
319-
} else {
320-
OrderProtocol::sockets.push_back(this);
321-
}

Src/HALALMock/Services/Communication/Ethernet/TCP/Socket.cpp

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,7 @@ void Socket::close() {
186186
tx_packet_buffer.pop();
187187
}
188188
state = CLOSING;
189-
LOG_INFO("Socket has been closed correctly")
189+
LOG_INFO("Socket has been closed correctly");
190190
}
191191

192192
void Socket::reconnect() { // I'm going to do in reconnect a total reset due to
@@ -213,16 +213,6 @@ void Socket::reset() {
213213
connect_attempt();
214214
}
215215

216-
void Socket::reset() {
217-
EthernetNode remote_node(remote_ip, remote_port);
218-
if (!connecting_sockets.contains(remote_node)) {
219-
connecting_sockets[remote_node] = this;
220-
}
221-
state = INACTIVE;
222-
close();
223-
configure_socket_and_connect();
224-
}
225-
226216
void Socket::send() {
227217
std::lock_guard<std::mutex> lock(mutex);
228218
while (!tx_packet_buffer.empty()) {

Src/HALALMock/Services/Communication/Ethernet/UDP/DatagramSocket.cpp

Lines changed: 1 addition & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -84,61 +84,6 @@ void DatagramSocket::operator=(DatagramSocket&& other) {
8484
other.is_disconnected = true;
8585
}
8686

87-
DatagramSocket::DatagramSocket(EthernetNode local_node,
88-
EthernetNode remote_node)
89-
: DatagramSocket(local_node.ip, local_node.port, remote_node.ip,
90-
remote_node.port) {}
91-
92-
DatagramSocket::~DatagramSocket() {
93-
if (not is_disconnected) close();
94-
}
95-
void DatagramSocket::create_udp_socket() {
96-
udp_socket = socket(AF_INET, SOCK_DGRAM, 0);
97-
if (udp_socket < 0) {
98-
LOG_ERROR("Unable to create socket");
99-
}
100-
struct sockaddr_in servaddr;
101-
servaddr.sin_family = AF_INET;
102-
servaddr.sin_port = htons(local_port);
103-
servaddr.sin_addr.s_addr = local_ip.address;
104-
if (bind(udp_socket, (struct sockaddr*)&servaddr, sizeof(servaddr)) < 0) {
105-
LOG_ERROR(std::format("Unable to bind to address {} in port {}",
106-
local_ip->string_address, local_port));
107-
close(udp_socket);
108-
is_disconnected = true;
109-
return;
110-
}
111-
is_disconnected = false;
112-
// receiving callback
113-
receiving_udp_thread = std::jthread([&]() {
114-
is_receiving = true;
115-
while (true) {
116-
uint8_t received_data[1024];
117-
struct sockaddr_in src_addr;
118-
socklen_t addr_len = sizeof(src_addr);
119-
ssize_t size =
120-
recvfrom(udp_socket, (uint8_t*)received_data, MAX_SIZE_PACKET,
121-
0, (struct sockaddr*)&src_addr, &addr_len);
122-
if (size < 0) {
123-
LOG_ERROR("Unable to receive data");
124-
is_receiving = false;
125-
return;
126-
}
127-
// receive callback
128-
Packet::parse_data(received_data);
129-
}
130-
});
131-
Ethernet::update();
132-
}
133-
void DatagramSocket::operator=(DatagramSocket&& other) {
134-
udp_socket = move(other.udp_socket);
135-
local_ip = move(other.local_ip);
136-
local_port = move(other.local_port);
137-
remote_ip = other.remote_ip;
138-
remote_port = other.remote_port;
139-
other.is_disconnected = true;
140-
}
141-
14287
void DatagramSocket::close() {
14388
if (!is_disconnected) {
14489
if (::close(udp_socket)) {
@@ -150,13 +95,4 @@ void DatagramSocket::close() {
15095
}
15196
is_disconnected = true;
15297
}
153-
}
154-
155-
void DatagramSocket::close() {
156-
// check if receiving thread is on and delete it
157-
if (is_receiving) {
158-
receiving_udp_thread.~jthread();
159-
}
160-
::close(udp_socket);
161-
is_disconnected = true;
162-
}
98+
}

0 commit comments

Comments
 (0)