Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Src/HALAL/HALAL.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ void HALAL::start(IPV4 ip, IPV4 subnet_mask, IPV4 gateway,
#else
// Simulator start
void HALAL::start(IPV4 ip, IPV4 subnet_mask, IPV4 gateway, UART::Peripheral& printf_peripheral) {
SharedMemory::start();
Ethernet::inscribe();
Pin::start();
ADC::start();
Expand Down
1 change: 1 addition & 0 deletions Src/HALALMock/Services/Communication/Ethernet/Ethernet.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ void Ethernet::inscribe() {
return;
}
}
is_ready = true;
} else {
LOG_ERROR("Unable to inscribe Ethernet because is already ready!");
}
Expand Down
27 changes: 0 additions & 27 deletions Src/HALALMock/Services/Communication/Ethernet/TCP/ServerSocket.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -292,30 +292,3 @@ void ServerSocket::close_inside_thread() {
listening_sockets[local_port] = this;
state = CLOSED;
}
void ServerSocket::configure_server_socket_and_listen() {
create_server_socket();
if (!configure_server_socket()) {
LOG_ERROR("Can't configure ServerSocket");
close();
return;
}
if (listen(server_socket_fd, SOMAXCONN) < 0) {
LOG_ERROR("Can't listen");
close();
return;
}
state = LISTENING;
listening_sockets[local_port] = this;
// create a thread to listen
listening_thread = std::jthread[&]() {
// solo aceptamos una conexion
struct sockaddr_in client_addr;
socklen_t client_len = sizeof(client_addr);
client_fd = accept(server_socket_fd, (struct sockaddr*)&client_addr,
&client_len);
if (client_fd > 0) {
if (!accept_callback(client_fd, client_addr)) {
LOG_ERROR("Something went wrong in accept_callback");
} else {
OrderProtocol::sockets.push_back(this);
}
12 changes: 1 addition & 11 deletions Src/HALALMock/Services/Communication/Ethernet/TCP/Socket.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ void Socket::close() {
tx_packet_buffer.pop();
}
state = CLOSING;
LOG_INFO("Socket has been closed correctly")
LOG_INFO("Socket has been closed correctly");
}

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

void Socket::reset() {
EthernetNode remote_node(remote_ip, remote_port);
if (!connecting_sockets.contains(remote_node)) {
connecting_sockets[remote_node] = this;
}
state = INACTIVE;
close();
configure_socket_and_connect();
}

void Socket::send() {
std::lock_guard<std::mutex> lock(mutex);
while (!tx_packet_buffer.empty()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,61 +84,6 @@ void DatagramSocket::operator=(DatagramSocket&& other) {
other.is_disconnected = true;
}

DatagramSocket::DatagramSocket(EthernetNode local_node,
EthernetNode remote_node)
: DatagramSocket(local_node.ip, local_node.port, remote_node.ip,
remote_node.port) {}

DatagramSocket::~DatagramSocket() {
if (not is_disconnected) close();
}
void DatagramSocket::create_udp_socket() {
udp_socket = socket(AF_INET, SOCK_DGRAM, 0);
if (udp_socket < 0) {
LOG_ERROR("Unable to create socket");
}
struct sockaddr_in servaddr;
servaddr.sin_family = AF_INET;
servaddr.sin_port = htons(local_port);
servaddr.sin_addr.s_addr = local_ip.address;
if (bind(udp_socket, (struct sockaddr*)&servaddr, sizeof(servaddr)) < 0) {
LOG_ERROR(std::format("Unable to bind to address {} in port {}",
local_ip->string_address, local_port));
close(udp_socket);
is_disconnected = true;
return;
}
is_disconnected = false;
// receiving callback
receiving_udp_thread = std::jthread([&]() {
is_receiving = true;
while (true) {
uint8_t received_data[1024];
struct sockaddr_in src_addr;
socklen_t addr_len = sizeof(src_addr);
ssize_t size =
recvfrom(udp_socket, (uint8_t*)received_data, MAX_SIZE_PACKET,
0, (struct sockaddr*)&src_addr, &addr_len);
if (size < 0) {
LOG_ERROR("Unable to receive data");
is_receiving = false;
return;
}
// receive callback
Packet::parse_data(received_data);
}
});
Ethernet::update();
}
void DatagramSocket::operator=(DatagramSocket&& other) {
udp_socket = move(other.udp_socket);
local_ip = move(other.local_ip);
local_port = move(other.local_port);
remote_ip = other.remote_ip;
remote_port = other.remote_port;
other.is_disconnected = true;
}

void DatagramSocket::close() {
if (!is_disconnected) {
if (::close(udp_socket)) {
Expand All @@ -150,13 +95,4 @@ void DatagramSocket::close() {
}
is_disconnected = true;
}
}

void DatagramSocket::close() {
// check if receiving thread is on and delete it
if (is_receiving) {
receiving_udp_thread.~jthread();
}
::close(udp_socket);
is_disconnected = true;
}
}