summaryrefslogtreecommitdiff
path: root/Userland/Libraries/LibCore/TCPSocket.cpp
blob: 7eab7f7834f7a087d602ceb5da7afc4e3d1b61cc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
/*
 * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
 *
 * SPDX-License-Identifier: BSD-2-Clause
 */

#include <LibCore/TCPSocket.h>
#include <errno.h>
#include <sys/socket.h>

#ifndef SOCK_NONBLOCK
#    include <sys/ioctl.h>
#endif

namespace Core {

TCPSocket::TCPSocket(int fd, Object* parent)
    : Socket(Socket::Type::TCP, parent)
{
    // NOTE: This constructor is used by TCPServer::accept(), so the socket is already connected.
    m_connected = true;
    set_fd(fd);
    set_mode(IODevice::ReadWrite);
    set_error(0);
}

TCPSocket::TCPSocket(Object* parent)
    : Socket(Socket::Type::TCP, parent)
{
#ifdef SOCK_NONBLOCK
    int fd = socket(AF_INET, SOCK_STREAM | SOCK_NONBLOCK, 0);
#else
    int fd = socket(AF_INET, SOCK_STREAM, 0);
    int option = 1;
    ioctl(fd, FIONBIO, &option);
#endif
    if (fd < 0) {
        set_error(errno);
    } else {
        set_fd(fd);
        set_mode(IODevice::ReadWrite);
        set_error(0);
    }
}

TCPSocket::~TCPSocket()
{
}

}