diff options
author | Andreas Kling <awesomekling@gmail.com> | 2019-03-18 14:09:58 +0100 |
---|---|---|
committer | Andreas Kling <awesomekling@gmail.com> | 2019-03-18 14:09:58 +0100 |
commit | 8e3d0a23d555b2464aba5a07a90092535cec19e1 (patch) | |
tree | 551456726750a407e82f76d9b626fa1d78775e70 /LibGUI/GSocket.cpp | |
parent | d466f2634d94df44402c5779395a8f3798151d45 (diff) | |
download | serenity-8e3d0a23d555b2464aba5a07a90092535cec19e1.zip |
LibGUI: Add GTCPSocket and base class GSocket (inherits from GIODevice.)
And use these to do the line-by-line reading automagically instead of having
that logic in IRCClient. This will definitely come in handy.
Diffstat (limited to 'LibGUI/GSocket.cpp')
-rw-r--r-- | LibGUI/GSocket.cpp | 64 |
1 files changed, 64 insertions, 0 deletions
diff --git a/LibGUI/GSocket.cpp b/LibGUI/GSocket.cpp new file mode 100644 index 0000000000..78343f2225 --- /dev/null +++ b/LibGUI/GSocket.cpp @@ -0,0 +1,64 @@ +#include <LibGUI/GSocket.h> +#include <sys/socket.h> +#include <netinet/in.h> +#include <arpa/inet.h> +#include <stdio.h> + +GSocket::GSocket(Type type, GObject* parent) + : GIODevice(parent) + , m_type(type) +{ +} + +GSocket::~GSocket() +{ +} + +bool GSocket::connect(const GSocketAddress& address, int port) +{ + ASSERT(!is_connected()); + ASSERT(address.type() == GSocketAddress::Type::IPv4); + 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); + + printf("Connecting to %s...", address.to_string().characters()); + fflush(stdout); + int rc = ::connect(fd(), (struct sockaddr*)&addr, sizeof(addr)); + if (rc < 0) { + perror("connect"); + exit(1); + } + printf("ok!\n"); + + m_destination_address = address; + m_destination_port = port; + m_connected = true; + return true; +} + +ByteBuffer GSocket::receive(int max_size) +{ + auto buffer = read(max_size); + if (eof()) { + dbgprintf("GSocket{%p}: Connection appears to have closed in receive().\n", this); + m_connected = false; + } + return buffer; +} + +bool GSocket::send(const ByteBuffer& data) +{ + int nsent = ::send(fd(), data.pointer(), data.size(), 0); + if (nsent < 0) { + set_error(nsent); + return false; + } + ASSERT(nsent == data.size()); + return true; +} |