AsyncFw 1.2
Async Framework is c++ runtime with timers, poll notifiers, sockets, coroutines, etc.
 
Loading...
Searching...
No Matches
AbstractSocket.h
1/*
2Copyright (c) 2026 Alexandr Kuzmuk
3
4This file is part of the AsyncFw project. Licensed under the MIT License.
5See {Link: LICENSE file https://mit-license.org} in the project root for full license information.
6*/
7
8#pragma once
9
10#include <cstdint>
11#include <string>
12#include "AnyData.h"
13
14namespace AsyncFw {
15class Thread;
16class DataArray;
17class LogStream;
18
20class AbstractSocket : public AnyData {
21 friend Thread;
22 friend LogStream &operator<<(LogStream &, const AbstractSocket &);
23 struct Private;
24
25public:
26 enum State : uint8_t { Unconnected, Listening, Connecting, Connected, Active, Closing, Destroy };
27 enum Error : uint8_t { None, Closed, Refused, Read, Write, Activate };
28
29 virtual void setDescriptor(int);
30 virtual bool connect(const std::string &, uint16_t);
31 virtual void disconnect();
32 virtual void close();
33 virtual void destroy();
34
35 bool listen(const std::string &, uint16_t);
36
37 DataArray &peek();
38 int read(uint8_t *, int);
39 DataArray read(int = 0);
40 int write(const uint8_t *, int);
41 int write(const DataArray &);
42
43 Error error() const;
44 std::string errorString() const;
45
46 Thread *thread() const { return thread_; }
47
48 std::string address() const;
49 uint16_t port() const;
50 std::string peerAddress() const;
51 uint16_t peerPort() const;
52
53protected:
54 AbstractSocket();
55 AbstractSocket(int, int, int);
56 virtual ~AbstractSocket();
57
58 int pendingRead() const;
59 int pendingWrite() const;
60 void setError(Error);
61 void setErrorString(const std::string &) const;
62
63 virtual int read_available_fd() const;
64 virtual int read_fd(void *_p, int _s);
65 virtual int write_fd(const void *_p, int _s);
66
67 virtual void activateEvent();
68
69 virtual void stateEvent() {}
70 virtual void readEvent() {}
71 virtual void writeEvent() {}
72 virtual void incomingEvent() {}
73
74 Thread *thread_;
75 int fd_ = -1;
76 mutable State state_ = State::Unconnected;
77
78private:
79 AbstractSocket(const AbstractSocket &) = delete;
80 AbstractSocket &operator=(const AbstractSocket &) = delete;
81 void pollEvent(int);
82 void changeDescriptor(int);
83 void read_fd();
84 Private *private_;
85};
86} // namespace AsyncFw
The DataArray class.
Definition DataArray.h:20
The LogStream class.
Definition LogStream.h:44
AsyncFw::Thread thread with sockets.
Definition Thread.h:16