Qore Programming Language 1.19.2
Loading...
Searching...
No Matches
QC_SocketPollOperation.h
1/* -*- mode: c++; indent-tabs-mode: nil -*- */
2/*
3 QC_SocketPollOperation.h
4
5 Qore Programming Language
6
7 Copyright (C) 2003 - 2023 Qore Technologies, s.r.o.
8
9 Permission is hereby granted, free of charge, to any person obtaining a
10 copy of this software and associated documentation files (the "Software"),
11 to deal in the Software without restriction, including without limitation
12 the rights to use, copy, modify, merge, publish, distribute, sublicense,
13 and/or sell copies of the Software, and to permit persons to whom the
14 Software is furnished to do so, subject to the following conditions:
15
16 The above copyright notice and this permission notice shall be included in
17 all copies or substantial portions of the Software.
18
19 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
24 FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
25 DEALINGS IN THE SOFTWARE.
26
27 Note that the Qore library is released under a choice of three open-source
28 licenses: MIT (as above), LGPL 2+, or GPL 2+; see README-LICENSE for more
29 information.
30*/
31
32#ifndef _QORE_CLASS_SOCKETPOLLOPERATION_H
33
34#define _QORE_CLASS_SOCKETPOLLOPERATION_H
35
36#include "qore/intern/QC_SocketPollOperationBase.h"
37#include "qore/intern/qore_socket_private.h"
38#include "qore/QoreSocketObject.h"
39
40#include <memory>
41
42// goals: connect, connect-ssl
43constexpr int SPG_CONNECT = 1;
44constexpr int SPG_CONNECT_SSL = 2;
45
46// states: none -> connecting -> [connecting-ssl ->] connected
47constexpr int SPS_NONE = 0;
48constexpr int SPS_CONNECTING = 1;
49constexpr int SPS_CONNECTING_SSL = 2;
50constexpr int SPS_CONNECTED = 3;
51
52class SocketConnectPollOperation : public SocketPollOperationBase {
53public:
54 DLLLOCAL SocketConnectPollOperation(ExceptionSink* xsink, bool ssl, const char* target, QoreSocketObject* sock);
55
56 DLLLOCAL void deref(ExceptionSink* xsink) {
57 if (ROdereference()) {
58 if (set_non_block) {
59 sock->clearNonBlock();
60 }
61 sock->deref(xsink);
62 delete this;
63 }
64 }
65
66 DLLLOCAL virtual bool goalReached() const {
67 return state == SPS_CONNECTED;
68 }
69
70 DLLLOCAL virtual QoreHashNode* continuePoll(ExceptionSink* xsink);
71
72protected:
73 QoreSocketObject* sock;
74
76 DLLLOCAL virtual int preVerify(ExceptionSink* xsink) {
77 return 0;
78 }
79
81 DLLLOCAL virtual void connected();
82
84 DLLLOCAL int startSslConnect(ExceptionSink* xsink);
85
86private:
87 std::unique_ptr<AbstractPollState> poll_state;
88 std::string target;
89
90 int sgoal = 0;
91 int state = SPS_NONE;
92
93 bool set_non_block = false;
94
95 DLLLOCAL virtual const char* getStateImpl() const {
96 switch (state) {
97 case SPS_NONE:
98 return "none";
99 case SPS_CONNECTING:
100 return "connecting";
101 case SPS_CONNECTING_SSL:
102 return "connecting-ssl";
103 case SPS_CONNECTED:
104 return "connected";
105 default:
106 assert(false);
107 }
108 return "";
109 }
110
111 DLLLOCAL int checkContinuePoll(ExceptionSink* xsink);
112};
113
114class SocketSendPollOperation : public SocketPollOperationBase {
115public:
116 // "data" must be passed already referenced
117 DLLLOCAL SocketSendPollOperation(ExceptionSink* xsink, QoreStringNode* data, QoreSocketObject* sock);
118
119 // "data" must be passed already referenced
120 DLLLOCAL SocketSendPollOperation(ExceptionSink* xsink, BinaryNode* data, QoreSocketObject* sock);
121
122 DLLLOCAL void deref(ExceptionSink* xsink) {
123 if (ROdereference()) {
124 if (set_non_block) {
125 sock->clearNonBlock();
126 }
127 sock->deref(xsink);
128 delete this;
129 }
130 }
131
132 DLLLOCAL virtual bool goalReached() const {
133 return sent;
134 }
135
136 DLLLOCAL virtual const char* getStateImpl() const {
137 return sent ? "sent" : "sending";
138 }
139
140 DLLLOCAL virtual QoreHashNode* continuePoll(ExceptionSink* xsink);
141
142private:
143 std::unique_ptr<AbstractPollState> poll_state;
145 QoreSocketObject* sock;
146 const char* buf;
147 size_t size;
148 bool set_non_block = false;
149 bool sent = false;
150};
151
152class SocketRecvPollOperationBase : public SocketPollOperationBase {
153public:
154 DLLLOCAL SocketRecvPollOperationBase(QoreSocketObject* sock, bool to_string) : sock(sock),
155 to_string(to_string) {
156 }
157
158 DLLLOCAL virtual void deref(ExceptionSink* xsink) {
159 if (ROdereference()) {
160 if (set_non_block) {
161 sock->clearNonBlock();
162 }
163 sock->deref(xsink);
164 delete this;
165 }
166 }
167
168 DLLLOCAL virtual bool goalReached() const {
169 return received;
170 }
171
172 DLLLOCAL virtual const char* getStateImpl() const {
173 return received ? "received" : "receiving";
174 }
175
176 DLLLOCAL virtual QoreValue getOutput() const {
177 return data ? data->refSelf() : QoreValue();
178 }
179
180 DLLLOCAL virtual QoreHashNode* continuePoll(ExceptionSink* xsink);
181
182protected:
183 std::unique_ptr<AbstractPollState> poll_state;
185 QoreSocketObject* sock;
186 bool to_string;
187 bool set_non_block = false;
188 bool received = false;
189
190 DLLLOCAL int initIntern(ExceptionSink* xsink);
191};
192
193class SocketRecvPollOperation : public SocketRecvPollOperationBase {
194public:
195 // "data" must be passed already referenced
196 DLLLOCAL SocketRecvPollOperation(ExceptionSink* xsink, ssize_t size, QoreSocketObject* sock, bool to_string);
197
198private:
199 size_t size;
200};
201
202class SocketRecvUntilBytesPollOperation : public SocketRecvPollOperationBase {
203public:
204 // "data" must be passed already referenced
205 DLLLOCAL SocketRecvUntilBytesPollOperation(ExceptionSink* xsink, const QoreStringNode* pattern,
206 QoreSocketObject* sock, bool to_string);
207
208private:
210};
211
212class SocketUpgradeClientSslPollOperation : public SocketPollOperationBase {
213public:
214 DLLLOCAL SocketUpgradeClientSslPollOperation(ExceptionSink* xsink, QoreSocketObject* sock);
215
216 DLLLOCAL void deref(ExceptionSink* xsink) {
217 if (ROdereference()) {
218 if (set_non_block) {
219 sock->clearNonBlock();
220 }
221 sock->deref(xsink);
222 delete this;
223 }
224 }
225
226 DLLLOCAL virtual bool goalReached() const {
227 return done;
228 }
229
230 DLLLOCAL virtual QoreHashNode* continuePoll(ExceptionSink* xsink);
231
232 DLLLOCAL virtual const char* getStateImpl() const {
233 return "connecting-ssl";
234 }
235
236private:
237 QoreSocketObject* sock;
238 std::unique_ptr<AbstractPollState> poll_state;
239 bool set_non_block = false;
240 bool done = false;
241};
242
243DLLLOCAL QoreClass* initSocketPollOperationClass(QoreNamespace& qorens);
244
245#endif // _QORE_CLASS_SOCKETPOLLOPERATION_H
virtual DLLLOCAL void deref()
decrements the reference count of the object without the possibility of throwing a Qore-language exce...
Definition: AbstractPrivateData.h:65
holds arbitrary binary data
Definition: BinaryNode.h:41
container for holding Qore-language exception information and also for registering a "thread_exit" ca...
Definition: ExceptionSink.h:50
defines a Qore-language class
Definition: QoreClass.h:253
This is the hash or associative list container type in Qore, dynamically allocated only,...
Definition: QoreHashNode.h:50
contains constants, classes, and subnamespaces in QoreProgram objects
Definition: QoreNamespace.h:65
DLLEXPORT bool ROdereference() const
atomically decrements the reference count
Qore's string value type, reference counted, dynamically-allocated only.
Definition: QoreStringNode.h:50
manages a reference count of a pointer to a class that takes a simple "deref()" call with no argument...
Definition: ReferenceHolder.h:127
The main value class in Qore, designed to be passed by value.
Definition: QoreValue.h:276
DLLEXPORT QoreValue refSelf() const
references the contained value if type == QV_Node, returns itself