-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Prototype testing * (Broken) client * Disconnection rework * Uses fd_set for multiplexing * Separate handling functions from the run function * Implement fd_set for client
- Loading branch information
Showing
11 changed files
with
399 additions
and
50 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,125 @@ | ||
#include "client.hpp" | ||
|
||
#include <unistd.h> | ||
|
||
#include <cstring> | ||
#include <iostream> | ||
#include <string_view> | ||
#include <utility> | ||
|
||
#include "jalsock/jalsock.hpp" | ||
#include "types/addr_info.hpp" | ||
|
||
TCPClient::TCPClient(const std::string_view host, const std::string_view port) | ||
: m_host{host}, m_port{port} {} | ||
|
||
TCPClient::~TCPClient() { | ||
if (m_server_fd != -1) { | ||
jalsock::send(m_server_fd, "/quit", 0); | ||
close(); | ||
} | ||
} | ||
|
||
TCPClient& TCPClient::setHost(const std::string_view host) { | ||
m_host = host; | ||
return *this; | ||
} | ||
|
||
TCPClient& TCPClient::setPort(const std::string_view port) { | ||
m_port = port; | ||
return *this; | ||
} | ||
|
||
void TCPClient::init() { | ||
AddrInfo hints; | ||
hints.setFamily(AIFamily::Unspec).setSocket(AISockType::Stream); | ||
|
||
const auto& [error, addresses] = | ||
jalsock::getAddressInfo(m_host, m_port, hints); | ||
|
||
if (error != ErrAI::Success) { | ||
std::cerr << "Can't get address info: " | ||
<< gai_strerror(static_cast<int>(error)) << std::endl; | ||
throw std::runtime_error{gai_strerror(static_cast<int>(error))}; | ||
} | ||
|
||
for (const auto& address : addresses) { | ||
m_server_fd = jalsock::socket(address.family(), address.sockType(), | ||
address.protocol()); | ||
|
||
if (m_server_fd == -1) { | ||
std::cerr << "Can't create socket: " << std::strerror(errno) | ||
<< std::endl; | ||
continue; | ||
} | ||
|
||
if (jalsock::connect(m_server_fd, address) == -1) { | ||
jalsock::close(m_server_fd); | ||
std::cerr << "Can't connect socket: " << std::strerror(errno) | ||
<< std::endl; | ||
continue; | ||
} | ||
|
||
break; | ||
} | ||
|
||
if (m_server_fd == -1) { | ||
throw std::runtime_error{"Can't connect socket"}; | ||
} | ||
} | ||
|
||
void TCPClient::run() { | ||
init(); | ||
|
||
std::cerr << "Connected to " << m_host << ":" << m_port << std::endl; | ||
|
||
FileDescSet _; | ||
|
||
while (true) { | ||
m_fd_set.zero(); | ||
m_fd_set.set(m_server_fd); | ||
m_fd_set.set(STDIN_FILENO); | ||
|
||
if (jalsock::select(m_server_fd + 1, m_fd_set, _, _, nullptr) == -1) { | ||
std::cerr << "Can't select: " << std::strerror(errno) << std::endl; | ||
break; | ||
} | ||
|
||
if (m_fd_set.isSet(m_server_fd)) { | ||
const auto& [len, message] = jalsock::recv(m_server_fd, 0); | ||
|
||
if (len == -1) { | ||
std::cerr << "Can't receive message: " << std::strerror(errno) | ||
<< std::endl; | ||
break; | ||
} else if (len == 0) { | ||
std::cerr << "Server closed connection" << std::endl; | ||
break; | ||
} | ||
|
||
std::cout << message << std::endl; | ||
} | ||
|
||
if (m_fd_set.isSet(STDIN_FILENO)) { | ||
std::string message; | ||
std::getline(std::cin, message); | ||
|
||
if (jalsock::send(m_server_fd, message, 0) == -1) { | ||
std::cerr << "Can't send message: " << std::strerror(errno) | ||
<< std::endl; | ||
break; | ||
} | ||
|
||
if (message == "/quit") { | ||
break; | ||
} | ||
} | ||
} | ||
|
||
close(); | ||
} | ||
|
||
void TCPClient::close() { | ||
jalsock::close(m_server_fd); | ||
m_server_fd = -1; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
#pragma once | ||
|
||
#include <string_view> | ||
|
||
#include "types/aliases.hpp" | ||
#include "types/fd_set.hpp" | ||
|
||
class TCPClient { | ||
public: | ||
TCPClient(const std::string_view host, const std::string_view port); | ||
|
||
TCPClient(const TCPClient&) = delete; | ||
TCPClient(TCPClient&&) = delete; | ||
TCPClient& operator=(const TCPClient&) = delete; | ||
TCPClient& operator=(TCPClient&&) = delete; | ||
|
||
~TCPClient(); | ||
|
||
TCPClient& setHost(const std::string_view host); | ||
TCPClient& setPort(const std::string_view port); | ||
|
||
void run(); | ||
|
||
private: | ||
std::string_view m_host; | ||
std::string_view m_port; | ||
|
||
FileDesc m_server_fd{-1}; | ||
FileDescSet m_fd_set{}; | ||
|
||
void init(); | ||
void close(); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
#include <iostream> | ||
|
||
#include "client/client.hpp" | ||
|
||
int main(int argc, char** argv) { | ||
if (argc != 3) { | ||
std::cerr << "Usage: ./jaltext_client <ip> <port>" << std::endl; | ||
return 1; | ||
} | ||
|
||
try { | ||
TCPClient client{argv[1], argv[2]}; | ||
client.run(); | ||
} catch (const std::exception& e) { | ||
std::cerr << e.what() << std::endl; | ||
return 1; | ||
} | ||
} |
File renamed without changes.
Oops, something went wrong.