Qore Programming Language 1.19.2
Loading...
Searching...
No Matches
QC_Socket.h
1/* -*- mode: c++; indent-tabs-mode: nil -*- */
2/*
3 QC_Socket.h
4
5 Qore Programming Language
6
7 Copyright (C) 2003 - 2023 Qore Technologies, s.r.o.
8
9 provides a thread-safe interface to the QoreSocket object
10
11 Permission is hereby granted, free of charge, to any person obtaining a
12 copy of this software and associated documentation files (the "Software"),
13 to deal in the Software without restriction, including without limitation
14 the rights to use, copy, modify, merge, publish, distribute, sublicense,
15 and/or sell copies of the Software, and to permit persons to whom the
16 Software is furnished to do so, subject to the following conditions:
17
18 The above copyright notice and this permission notice shall be included in
19 all copies or substantial portions of the Software.
20
21 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
22 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
23 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
24 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
25 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
26 FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
27 DEALINGS IN THE SOFTWARE.
28
29 Note that the Qore library is released under a choice of three open-source
30 licenses: MIT (as above), LGPL 2+, or GPL 2+; see README-LICENSE for more
31 information.
32*/
33
34#ifndef _QORE_CLASS_SOCKET_H
35
36#define _QORE_CLASS_SOCKET_H
37
38DLLLOCAL QoreClass* initSocketClass(QoreNamespace& qorens);
39DLLEXPORT extern qore_classid_t CID_SOCKET;
40DLLEXPORT extern QoreClass* QC_SOCKET;
41
42DLLLOCAL TypedHashDecl* init_hashdecl_SocketPollInfo(QoreNamespace& ns);
43
44#include <qore/QoreSocket.h>
45#include <qore/AbstractPrivateData.h>
46#include <qore/QoreThreadLock.h>
47#include <qore/QoreSocketObject.h>
48#include "qore/intern/QC_SSLCertificate.h"
49#include "qore/intern/QC_SSLPrivateKey.h"
50
51class my_socket_priv {
52public:
53 QoreSocket* socket;
54 QoreSSLCertificate* cert = nullptr;
55 QoreSSLPrivateKey* pk = nullptr;
56 mutable QoreThreadLock m;
57 bool in_non_block = false;
58 bool valid = true;
59
60 DLLLOCAL my_socket_priv(QoreSocket* s, QoreSSLCertificate* c = nullptr, QoreSSLPrivateKey* p = nullptr)
61 : socket(s), cert(c), pk(p) {
62 }
63
64 DLLLOCAL my_socket_priv() : socket(new QoreSocket) {
65 }
66
67 DLLLOCAL ~my_socket_priv() {
68 if (cert) {
69 cert->deref();
70 }
71 if (pk) {
72 pk->deref();
73 }
74
75 delete socket;
76 }
77
79 DLLLOCAL void invalidate() {
80 // must be called with the lock held
81 assert(m.trylock());
82
83 if (valid) {
84 valid = false;
85 }
86 }
87
89 DLLLOCAL int checkValid(ExceptionSink* xsink) {
90 // must be called with the lock held
91 assert(m.trylock());
92
93 if (!valid) {
94 xsink->raiseException("OBJECT-ALREADY-DELETED", "the underlying socket object has already been deleted "
95 "and can no longer be used");
96 return -1;
97 }
98 return 0;
99 }
100
102 DLLLOCAL int checkNonBlock(ExceptionSink* xsink) {
103 // must be called with the lock held
104 assert(m.trylock());
105
106 if (in_non_block) {
107 xsink->raiseException("SOCKET-NON-BLOCK-ERROR", "a non-blocking operation is currently in progress");
108 return -1;
109 }
110
111 return checkValid(xsink);
112 }
113
115 DLLLOCAL int checkOpen(ExceptionSink* xsink);
116
118 DLLLOCAL int checkOpenAndNotSsl(ExceptionSink* xsink);
119
121 DLLLOCAL void setNonBlock() {
122 // must be called with the lock held
123 assert(m.trylock());
124
125 assert(!in_non_block);
126 in_non_block = true;
127 }
128
130 DLLLOCAL int setNonBlock(ExceptionSink* xsink) {
131 // must be called with the lock held
132 assert(m.trylock());
133
134 if (!checkNonBlock(xsink)) {
135 setNonBlock();
136 return 0;
137 }
138 return -1;
139 }
140
142 DLLLOCAL void clearNonBlock() {
143 // must be called with the lock held
144 assert(m.trylock());
145 if (in_non_block) {
146 in_non_block = false;
147 }
148 }
149
151 DLLLOCAL void setAccept(QoreObject* o) {
152 socket->setAccept(o);
153 }
154
155 DLLLOCAL static void setAccept(QoreSocketObject& sock, QoreObject* o) {
156 sock.priv->setAccept(o);
157 }
158};
159
160#endif // _QORE_CLASS_QORESOCKET_H
virtual DLLLOCAL void deref(ExceptionSink *xsink)
decrements the reference count of the object
Definition: AbstractPrivateData.h:59
container for holding Qore-language exception information and also for registering a "thread_exit" ca...
Definition: ExceptionSink.h:50
DLLEXPORT AbstractQoreNode * raiseException(const char *err, const char *fmt,...)
appends a Qore-language exception to the list
defines a Qore-language class
Definition: QoreClass.h:253
contains constants, classes, and subnamespaces in QoreProgram objects
Definition: QoreNamespace.h:65
the implementation of Qore's object data type, reference counted, dynamically-allocated only
Definition: QoreObject.h:60
represents an X509 certificate, reference-counted, dynamically-allocated only
Definition: QoreSSLCertificate.h:42
provides access to a private key data structure for SSL connections
Definition: QoreSSLPrivateKey.h:40
provides access to sockets using Qore data structures
Definition: QoreSocket.h:127
DLLLOCAL void setAccept(QoreObject *o)
sets backwards-compatible members on accept in a new object - will be removed in a future version of ...
provides a mutually-exclusive thread lock
Definition: QoreThreadLock.h:49
DLLLOCAL int trylock()
attempts to acquire the mutex and returns the status immediately; does not block
Definition: QoreThreadLock.h:101
typed hash declaration
Definition: TypedHashDecl.h:44
unsigned qore_classid_t
used for the unique class ID for QoreClass objects
Definition: common.h:79