Qore Programming Language 2.1.1
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 - 2024 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);
43DLLLOCAL TypedHashDecl* init_hashdecl_SseMessageInfo(QoreNamespace& ns);
44
45#include <qore/QoreSocket.h>
46#include <qore/AbstractPrivateData.h>
47#include <qore/QoreThreadLock.h>
48#include <qore/QoreSocketObject.h>
49#include "qore/intern/QC_SSLCertificate.h"
50#include "qore/intern/QC_SSLPrivateKey.h"
51
52class my_socket_priv {
53public:
54 QoreSocket* socket;
55 QoreSSLCertificate* cert = nullptr;
56 QoreSSLPrivateKey* pk = nullptr;
57 mutable QoreThreadLock m;
58 bool in_non_block = false;
59 bool valid = true;
60
61 DLLLOCAL my_socket_priv(QoreSocket* s, QoreSSLCertificate* c = nullptr, QoreSSLPrivateKey* p = nullptr)
62 : socket(s), cert(c), pk(p) {
63 }
64
65 DLLLOCAL my_socket_priv() : socket(new QoreSocket) {
66 }
67
68 DLLLOCAL ~my_socket_priv() {
69 if (cert) {
70 cert->deref();
71 }
72 if (pk) {
73 pk->deref();
74 }
75
76 delete socket;
77 }
78
80 DLLLOCAL void invalidate() {
81 // must be called with the lock held
82 assert(m.trylock());
83
84 if (valid) {
85 valid = false;
86 }
87 }
88
90 DLLLOCAL int checkValid(ExceptionSink* xsink) {
91 // must be called with the lock held
92 assert(m.trylock());
93
94 if (!valid) {
95 xsink->raiseException("OBJECT-ALREADY-DELETED", "the underlying socket object has already been deleted "
96 "and can no longer be used");
97 return -1;
98 }
99 return 0;
100 }
101
103 DLLLOCAL int checkNonBlock(ExceptionSink* xsink) {
104 // must be called with the lock held
105 assert(m.trylock());
106
107 if (in_non_block) {
108 xsink->raiseException("SOCKET-NON-BLOCK-ERROR", "a non-blocking operation is currently in progress");
109 return -1;
110 }
111
112 return checkValid(xsink);
113 }
114
116 DLLLOCAL int checkOpen(ExceptionSink* xsink);
117
119 DLLLOCAL int checkOpenAndNotSsl(ExceptionSink* xsink);
120
122 DLLLOCAL void setNonBlock() {
123 // must be called with the lock held
124 assert(m.trylock());
125
126 assert(!in_non_block);
127 in_non_block = true;
128 }
129
131 DLLLOCAL int setNonBlock(ExceptionSink* xsink) {
132 // must be called with the lock held
133 assert(m.trylock());
134
135 if (!checkNonBlock(xsink)) {
136 setNonBlock();
137 return 0;
138 }
139 return -1;
140 }
141
143 DLLLOCAL void clearNonBlock() {
144 // must be called with the lock held
145 assert(m.trylock());
146 if (in_non_block) {
147 in_non_block = false;
148 }
149 }
150
152 DLLLOCAL void setAccept(QoreObject* o) {
153 socket->setAccept(o);
154 }
155
156 DLLLOCAL static void setAccept(QoreSocketObject& sock, QoreObject* o) {
157 sock.priv->setAccept(o);
158 }
159};
160
161#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:310
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:61
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:129
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:85