2020-01-18 09:38:21 +01:00
|
|
|
/*
|
|
|
|
|
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
|
|
|
|
|
* All rights reserved.
|
|
|
|
|
*
|
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
|
* modification, are permitted provided that the following conditions are met:
|
|
|
|
|
*
|
|
|
|
|
* 1. Redistributions of source code must retain the above copyright notice, this
|
|
|
|
|
* list of conditions and the following disclaimer.
|
|
|
|
|
*
|
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright notice,
|
|
|
|
|
* this list of conditions and the following disclaimer in the documentation
|
|
|
|
|
* and/or other materials provided with the distribution.
|
|
|
|
|
*
|
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
|
|
|
|
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
|
|
|
|
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
|
|
|
|
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
|
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
|
|
|
|
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
|
|
|
|
|
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
|
|
|
|
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
|
|
|
|
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
|
*/
|
|
|
|
|
|
2019-03-18 14:09:58 +01:00
|
|
|
#pragma once
|
|
|
|
|
|
2020-02-06 15:04:03 +01:00
|
|
|
#include <LibCore/IODevice.h>
|
|
|
|
|
#include <LibCore/SocketAddress.h>
|
2019-03-18 14:09:58 +01:00
|
|
|
|
2020-02-02 12:34:39 +01:00
|
|
|
namespace Core {
|
2019-04-08 04:53:45 +02:00
|
|
|
|
2020-02-02 12:34:39 +01:00
|
|
|
class Notifier;
|
|
|
|
|
|
|
|
|
|
class Socket : public IODevice {
|
|
|
|
|
C_OBJECT(Socket)
|
2019-03-18 14:09:58 +01:00
|
|
|
public:
|
2019-06-07 17:13:23 +02:00
|
|
|
enum class Type {
|
2019-05-28 11:53:16 +02:00
|
|
|
Invalid,
|
|
|
|
|
TCP,
|
2019-07-13 19:42:03 +02:00
|
|
|
UDP,
|
|
|
|
|
Local,
|
2019-05-28 11:53:16 +02:00
|
|
|
};
|
2020-02-02 12:34:39 +01:00
|
|
|
virtual ~Socket() override;
|
2019-03-18 14:09:58 +01:00
|
|
|
|
2019-07-25 11:48:21 +02:00
|
|
|
Type type() const { return m_type; }
|
|
|
|
|
|
2019-04-02 20:40:10 +02:00
|
|
|
bool connect(const String& hostname, int port);
|
2020-02-02 12:34:39 +01:00
|
|
|
bool connect(const SocketAddress&, int port);
|
|
|
|
|
bool connect(const SocketAddress&);
|
2019-03-18 14:09:58 +01:00
|
|
|
|
|
|
|
|
ByteBuffer receive(int max_size);
|
|
|
|
|
bool send(const ByteBuffer&);
|
|
|
|
|
|
|
|
|
|
bool is_connected() const { return m_connected; }
|
2019-07-16 18:00:08 +02:00
|
|
|
void set_blocking(bool blocking);
|
2019-03-18 14:09:58 +01:00
|
|
|
|
2020-02-02 12:34:39 +01:00
|
|
|
SocketAddress source_address() const { return m_source_address; }
|
2019-03-18 14:09:58 +01:00
|
|
|
int source_port() const { return m_source_port; }
|
|
|
|
|
|
2020-02-02 12:34:39 +01:00
|
|
|
SocketAddress destination_address() const { return m_source_address; }
|
2019-03-18 14:09:58 +01:00
|
|
|
int destination_port() const { return m_destination_port; }
|
|
|
|
|
|
2019-04-08 04:53:45 +02:00
|
|
|
Function<void()> on_connected;
|
2019-07-27 10:48:43 +02:00
|
|
|
Function<void()> on_ready_to_read;
|
2019-04-08 04:53:45 +02:00
|
|
|
|
2019-03-18 14:09:58 +01:00
|
|
|
protected:
|
2020-02-02 12:34:39 +01:00
|
|
|
Socket(Type, Object* parent);
|
2019-03-18 14:09:58 +01:00
|
|
|
|
2020-02-02 12:34:39 +01:00
|
|
|
SocketAddress m_source_address;
|
|
|
|
|
SocketAddress m_destination_address;
|
2019-03-18 14:09:58 +01:00
|
|
|
int m_source_port { -1 };
|
|
|
|
|
int m_destination_port { -1 };
|
|
|
|
|
bool m_connected { false };
|
|
|
|
|
|
2019-07-27 10:48:43 +02:00
|
|
|
virtual void did_update_fd(int) override;
|
|
|
|
|
|
2019-03-18 14:09:58 +01:00
|
|
|
private:
|
2020-02-02 12:34:39 +01:00
|
|
|
virtual bool open(IODevice::OpenMode) override { ASSERT_NOT_REACHED(); }
|
2019-09-11 19:44:15 +02:00
|
|
|
bool common_connect(const struct sockaddr*, socklen_t);
|
2019-09-22 21:46:46 +02:00
|
|
|
void ensure_read_notifier();
|
2019-09-11 19:44:15 +02:00
|
|
|
|
2019-03-18 14:09:58 +01:00
|
|
|
Type m_type { Type::Invalid };
|
2020-02-02 12:34:39 +01:00
|
|
|
RefPtr<Notifier> m_notifier;
|
|
|
|
|
RefPtr<Notifier> m_read_notifier;
|
2019-03-18 14:09:58 +01:00
|
|
|
};
|
2020-02-02 12:34:39 +01:00
|
|
|
|
|
|
|
|
}
|