2019-04-10 15:35:43 +00:00
|
|
|
#include <LibCore/CNotifier.h>
|
2019-05-28 09:53:16 +00:00
|
|
|
#include <LibCore/CSocket.h>
|
2019-03-18 13:09:58 +00:00
|
|
|
#include <arpa/inet.h>
|
2019-04-08 02:53:45 +00:00
|
|
|
#include <errno.h>
|
2019-05-28 09:53:16 +00:00
|
|
|
#include <netdb.h>
|
|
|
|
#include <netinet/in.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <sys/socket.h>
|
2019-03-18 13:09:58 +00:00
|
|
|
|
2019-04-10 18:22:23 +00:00
|
|
|
CSocket::CSocket(Type type, CObject* parent)
|
|
|
|
: CIODevice(parent)
|
2019-03-18 13:09:58 +00:00
|
|
|
, m_type(type)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2019-04-10 18:22:23 +00:00
|
|
|
CSocket::~CSocket()
|
2019-03-18 13:09:58 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2019-04-10 18:22:23 +00:00
|
|
|
bool CSocket::connect(const String& hostname, int port)
|
2019-04-02 18:40:10 +00:00
|
|
|
{
|
|
|
|
auto* hostent = gethostbyname(hostname.characters());
|
|
|
|
if (!hostent) {
|
2019-04-10 18:22:23 +00:00
|
|
|
dbgprintf("CSocket::connect: Unable to resolve '%s'\n", hostname.characters());
|
2019-04-02 18:40:10 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
IPv4Address host_address((const byte*)hostent->h_addr_list[0]);
|
2019-04-10 18:22:23 +00:00
|
|
|
dbgprintf("CSocket::connect: Resolved '%s' to %s\n", hostname.characters(), host_address.to_string().characters());
|
2019-04-02 18:40:10 +00:00
|
|
|
return connect(host_address, port);
|
|
|
|
}
|
|
|
|
|
2019-04-10 18:22:23 +00:00
|
|
|
bool CSocket::connect(const CSocketAddress& address, int port)
|
2019-03-18 13:09:58 +00:00
|
|
|
{
|
|
|
|
ASSERT(!is_connected());
|
2019-04-10 18:22:23 +00:00
|
|
|
ASSERT(address.type() == CSocketAddress::Type::IPv4);
|
2019-03-18 13:09:58 +00:00
|
|
|
ASSERT(port > 0 && port <= 65535);
|
|
|
|
|
|
|
|
struct sockaddr_in addr;
|
|
|
|
memset(&addr, 0, sizeof(addr));
|
|
|
|
auto ipv4_address = address.ipv4_address();
|
|
|
|
memcpy(&addr.sin_addr.s_addr, &ipv4_address, sizeof(IPv4Address));
|
|
|
|
addr.sin_family = AF_INET;
|
|
|
|
addr.sin_port = htons(port);
|
|
|
|
|
2019-04-08 02:53:45 +00:00
|
|
|
m_destination_address = address;
|
|
|
|
m_destination_port = port;
|
|
|
|
|
2019-05-10 16:20:54 +00:00
|
|
|
dbgprintf("Connecting to %s...", address.to_string().characters());
|
2019-03-18 13:09:58 +00:00
|
|
|
fflush(stdout);
|
|
|
|
int rc = ::connect(fd(), (struct sockaddr*)&addr, sizeof(addr));
|
|
|
|
if (rc < 0) {
|
2019-04-08 02:53:45 +00:00
|
|
|
if (errno == EINPROGRESS) {
|
2019-05-10 16:20:54 +00:00
|
|
|
dbgprintf("in progress.\n");
|
2019-04-10 15:35:43 +00:00
|
|
|
m_notifier = make<CNotifier>(fd(), CNotifier::Event::Write);
|
|
|
|
m_notifier->on_ready_to_write = [this] {
|
2019-05-10 16:20:54 +00:00
|
|
|
dbgprintf("%s{%p} connected!\n", class_name(), this);
|
2019-04-08 02:53:45 +00:00
|
|
|
m_connected = true;
|
2019-04-10 15:35:43 +00:00
|
|
|
m_notifier->set_event_mask(CNotifier::Event::None);
|
2019-04-08 02:53:45 +00:00
|
|
|
if (on_connected)
|
|
|
|
on_connected();
|
|
|
|
};
|
|
|
|
return true;
|
|
|
|
}
|
2019-03-18 13:09:58 +00:00
|
|
|
perror("connect");
|
|
|
|
exit(1);
|
|
|
|
}
|
2019-05-10 16:20:54 +00:00
|
|
|
dbgprintf("ok!\n");
|
2019-03-18 13:09:58 +00:00
|
|
|
m_connected = true;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2019-04-10 18:22:23 +00:00
|
|
|
ByteBuffer CSocket::receive(int max_size)
|
2019-03-18 13:09:58 +00:00
|
|
|
{
|
|
|
|
auto buffer = read(max_size);
|
|
|
|
if (eof()) {
|
2019-04-10 18:22:23 +00:00
|
|
|
dbgprintf("CSocket{%p}: Connection appears to have closed in receive().\n", this);
|
2019-03-18 13:09:58 +00:00
|
|
|
m_connected = false;
|
|
|
|
}
|
|
|
|
return buffer;
|
|
|
|
}
|
|
|
|
|
2019-04-10 18:22:23 +00:00
|
|
|
bool CSocket::send(const ByteBuffer& data)
|
2019-03-18 13:09:58 +00:00
|
|
|
{
|
|
|
|
int nsent = ::send(fd(), data.pointer(), data.size(), 0);
|
|
|
|
if (nsent < 0) {
|
|
|
|
set_error(nsent);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
ASSERT(nsent == data.size());
|
|
|
|
return true;
|
|
|
|
}
|