Qore Programming Language  0.8.11
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
QoreSocketObject.h
1 /* -*- mode: c++; indent-tabs-mode: nil -*- */
2 /*
3  QoreSocketObject.h
4 
5  Qore Programming Language
6 
7  Copyright (C) 2003 - 2014 David Nichols
8 
9  provides a thread-safe interface to the QoreSocket object
10 
11  This library is free software; you can redistribute it and/or
12  modify it under the terms of the GNU Lesser General Public
13  License as published by the Free Software Foundation; either
14  version 2.1 of the License, or (at your option) any later version.
15 
16  This library is distributed in the hope that it will be useful,
17  but WITHOUT ANY WARRANTY; without even the implied warranty of
18  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19  Lesser General Public License for more details.
20 
21  You should have received a copy of the GNU Lesser General Public
22  License along with this library; if not, write to the Free Software
23  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
24 */
25 
26 #ifndef _QORE_QORE_SOCKET_OBJECT_H
27 
28 #define _QORE_QORE_SOCKET_OBJECT_H
29 
30 #include <qore/QoreSocket.h>
31 #include <qore/AbstractPrivateData.h>
32 #include <qore/QoreThreadLock.h>
33 
34 class QoreSSLCertificate;
35 class QoreSSLPrivateKey;
36 class Queue;
37 class my_socket_priv;
38 
39 class QoreSocketObject : public AbstractPrivateData {
40 private:
41  friend class my_socket_priv;
42  friend struct qore_httpclient_priv;
43 
44  DLLLOCAL QoreSocketObject(QoreSocket* s);
45 
46 protected:
47  my_socket_priv* priv;
48 
49  DLLLOCAL virtual ~QoreSocketObject();
50 
51 public:
52  DLLEXPORT QoreSocketObject();
53 
54  DLLEXPORT virtual void deref(ExceptionSink* xsink);
55  DLLEXPORT virtual void deref();
56 
57  DLLEXPORT int connect(const char* name, int timeout_ms, ExceptionSink* xsink = NULL);
58  DLLEXPORT int connectINET(const char* host, int port, int timeout_ms, ExceptionSink* xsink = NULL);
59  DLLEXPORT int connectINET2(const char* host, const char* service, int family, int sock_type, int protocol, int timeout_ms = -1, ExceptionSink* xsink = NULL);
60  DLLEXPORT int connectUNIX(const char* p, int socktype, int protocol, ExceptionSink* xsink = NULL);
61  DLLEXPORT int connectSSL(const char* name, int timeout_ms, ExceptionSink* xsink);
62  DLLEXPORT int connectINETSSL(const char* host, int port, int timeout_ms, ExceptionSink* xsink);
63  DLLEXPORT int connectINET2SSL(const char* host, const char* service, int family, int sock_type, int protocol, int timeout_ms = -1, ExceptionSink* xsink = NULL);
64  DLLEXPORT int connectUNIXSSL(const char* p, int socktype, int protocol, ExceptionSink* xsink);
65  // to bind to either a UNIX socket or an INET interface:port
66  DLLEXPORT int bind(const char* name, bool reuseaddr = false);
67  // to bind to an INET tcp port on all interfaces
68  DLLEXPORT int bind(int port, bool reuseaddr = false);
69  // to bind an open socket to an INET tcp port on a specific interface
70  DLLEXPORT int bind(const char* iface, int port, bool reuseaddr = false);
71 
72  DLLEXPORT int bindUNIX(const char* name, int socktype, int protocol, ExceptionSink* xsink);
73  DLLEXPORT int bindINET(const char* name, const char* service, bool reuseaddr, int family, int socktype, int protocol, ExceptionSink* xsink);
74 
75  // get port number for INET sockets
76  DLLEXPORT int getPort();
77  DLLEXPORT QoreSocketObject *accept(SocketSource *source, ExceptionSink* xsink);
78  DLLEXPORT QoreSocketObject *acceptSSL(SocketSource *source, ExceptionSink* xsink);
79  DLLEXPORT QoreSocketObject *accept(int timeout_ms, ExceptionSink* xsink);
80  DLLEXPORT QoreSocketObject *acceptSSL(int timeout_ms, ExceptionSink* xsink);
81 
82  DLLEXPORT int listen(int backlog);
83  // send a buffer of a particular size
84  DLLEXPORT int send(const char* buf, int size);
85  DLLEXPORT int send(const char* buf, int size, int timeout_ms, ExceptionSink* xsink);
86  // send a null-terminated string
87  DLLEXPORT int send(const QoreString *msg, int timeout_ms, ExceptionSink* xsink);
88  // send a binary object
89  DLLEXPORT int send(const BinaryNode* b);
90  DLLEXPORT int send(const BinaryNode* b, int timeout_ms, ExceptionSink* xsink);
91  // send from a file descriptor
92  DLLEXPORT int send(int fd, int size = -1);
93  // send bytes and convert to network order
94  DLLEXPORT int sendi1(char b, int timeout_ms, ExceptionSink* xsink);
95  DLLEXPORT int sendi2(short b, int timeout_ms, ExceptionSink* xsink);
96  DLLEXPORT int sendi4(int b, int timeout_ms, ExceptionSink* xsink);
97  DLLEXPORT int sendi8(int64 b, int timeout_ms, ExceptionSink* xsink);
98  DLLEXPORT int sendi2LSB(short b, int timeout_ms, ExceptionSink* xsink);
99  DLLEXPORT int sendi4LSB(int b, int timeout_ms, ExceptionSink* xsink);
100  DLLEXPORT int sendi8LSB(int64 b, int timeout_ms, ExceptionSink* xsink);
101  // receive a message
102  DLLEXPORT QoreStringNode* recv(int timeout, ExceptionSink* xsink);
103  // receive a certain number of bytes as a string
104  DLLEXPORT QoreStringNode* recv(qore_offset_t bufsize, int timeout_ms, ExceptionSink* xsink);
105  // receive a certain number of bytes as a binary object
106  DLLEXPORT BinaryNode* recvBinary(int bufsize, int timeout, ExceptionSink* xsink);
107  // receive a packet of bytes as a binary object
108  DLLEXPORT BinaryNode* recvBinary(int timeout, ExceptionSink* xsink);
109  // receive and write data to a file descriptor
110  DLLEXPORT int recv(int fd, int size, int timeout);
111  // receive integers and convert from network byte order
112  DLLEXPORT int64 recvi1(int timeout, char* b, ExceptionSink* xsink);
113  DLLEXPORT int64 recvi2(int timeout, short *b, ExceptionSink* xsink);
114  DLLEXPORT int64 recvi4(int timeout, int *b, ExceptionSink* xsink);
115  DLLEXPORT int64 recvi8(int timeout, int64 *b, ExceptionSink* xsink);
116  DLLEXPORT int64 recvi2LSB(int timeout, short *b, ExceptionSink* xsink);
117  DLLEXPORT int64 recvi4LSB(int timeout, int *b, ExceptionSink* xsink);
118  DLLEXPORT int64 recvi8LSB(int timeout, int64 *b, ExceptionSink* xsink);
119  DLLEXPORT int64 recvu1(int timeout, unsigned char* b, ExceptionSink* xsink);
120  DLLEXPORT int64 recvu2(int timeout, unsigned short *b, ExceptionSink* xsink);
121  DLLEXPORT int64 recvu4(int timeout, unsigned int *b, ExceptionSink* xsink);
122  DLLEXPORT int64 recvu2LSB(int timeout, unsigned short *b, ExceptionSink* xsink);
123  DLLEXPORT int64 recvu4LSB(int timeout, unsigned int *b, ExceptionSink* xsink);
124  // send HTTP message
125  DLLEXPORT int sendHTTPMessage(ExceptionSink* xsink, QoreHashNode* info, const char* method, const char* path, const char* http_version, const QoreHashNode* headers, const void* ptr, int size, int source, int timeout_ms);
126  DLLEXPORT int sendHTTPMessageWithCallback(ExceptionSink* xsink, QoreHashNode *info, const char *method, const char *path, const char *http_version, const QoreHashNode *headers, const ResolvedCallReferenceNode& send_callback, int source, int timeout_ms);
127  DLLEXPORT int sendHTTPMessageWithCallback(ExceptionSink* xsink, QoreHashNode* info, const char* method, const char *path, const char *http_version, const QoreHashNode *headers, const ResolvedCallReferenceNode& send_callback, int source, int timeout_ms, bool* aborted);
128 
129  // send HTTP response
130  DLLEXPORT int sendHTTPResponse(ExceptionSink* xsink, int code, const char* desc, const char* http_version, const QoreHashNode* headers, const void* ptr, int size, int source, int timeout_ms);
131  DLLEXPORT int sendHTTPResponseWithCallback(ExceptionSink* xsink, int code, const char *desc, const char *http_version, const QoreHashNode *headers, const ResolvedCallReferenceNode& send_callback, int source, int timeout_ms);
132  DLLEXPORT int sendHTTPResponseWithCallback(ExceptionSink* xsink, int code, const char *desc, const char *http_version, const QoreHashNode *headers, const ResolvedCallReferenceNode& send_callback, int source, int timeout_ms, bool* aborted);
133 
134  // read and parse HTTP header
135  DLLEXPORT AbstractQoreNode* readHTTPHeader(ExceptionSink* xsink, QoreHashNode* info, int timeout);
136  // receive a binary message in HTTP chunked format
137  DLLEXPORT QoreHashNode* readHTTPChunkedBodyBinary(int timeout, ExceptionSink* xsink);
138  // receive a string message in HTTP chunked format
139  DLLEXPORT QoreHashNode* readHTTPChunkedBody(int timeout, ExceptionSink* xsink);
140 
141  // receive a binary message in HTTP chunked format
142  DLLEXPORT void readHTTPChunkedBodyBinaryWithCallback(const ResolvedCallReferenceNode& recv_callback, QoreObject* obj, int timeout_ms, ExceptionSink* xsink);
143  // receive a string message in HTTP chunked format
144  DLLEXPORT void readHTTPChunkedBodyWithCallback(const ResolvedCallReferenceNode& recv_callback, QoreObject* obj, int timeout_ms, ExceptionSink* xsink);
145 
146  DLLEXPORT QoreStringNode* readHTTPHeaderString(ExceptionSink* xsink, int timeout_ms);
147 
148  DLLEXPORT int setSendTimeout(int ms);
149  DLLEXPORT int setRecvTimeout(int ms);
150  DLLEXPORT int getSendTimeout();
151  DLLEXPORT int getRecvTimeout();
152  DLLEXPORT int close();
153  DLLEXPORT int shutdown();
154  DLLEXPORT int shutdownSSL(ExceptionSink* xsink) ;
155  DLLEXPORT const char* getSSLCipherName();
156  DLLEXPORT const char* getSSLCipherVersion();
157  DLLEXPORT bool isSecure();
158  DLLEXPORT long verifyPeerCertificate();
159  DLLEXPORT int getSocket();
160  DLLEXPORT void setEncoding(const QoreEncoding *id);
161  DLLEXPORT const QoreEncoding *getEncoding() const;
162  DLLEXPORT bool isDataAvailable(ExceptionSink* xsink, int timeout = 0);
163  DLLEXPORT bool isWriteFinished(ExceptionSink* xsink, int timeout = 0);
164  DLLEXPORT bool isOpen() const;
165  // c must be already referenced before this call
166  DLLEXPORT void setCertificate(QoreSSLCertificate* c);
167  // p must be already referenced before this call
168  DLLEXPORT void setPrivateKey(QoreSSLPrivateKey* p);
169  DLLEXPORT int setNoDelay(int nodelay);
170  DLLEXPORT int getNoDelay();
171  DLLEXPORT void setEventQueue(Queue *cbq, ExceptionSink* xsink);
172  DLLEXPORT QoreHashNode* getPeerInfo(ExceptionSink* xsink, bool host_lookup = true) const;
173  DLLEXPORT QoreHashNode* getSocketInfo(ExceptionSink* xsink, bool host_lookup = true) const;
174 
175  DLLEXPORT void upgradeClientToSSL(ExceptionSink* xsink);
176  DLLEXPORT void upgradeServerToSSL(ExceptionSink* xsink);
177 
178  DLLEXPORT void clearWarningQueue(ExceptionSink* xsink);
179  DLLEXPORT void setWarningQueue(ExceptionSink* xsink, int64 warning_ms, int64 warning_bs, class Queue* wq, AbstractQoreNode* arg, int64 min_ms = 1000);
180  DLLEXPORT QoreHashNode* getUsageInfo() const;
181  DLLEXPORT void clearStats();
182  DLLEXPORT bool pendingHttpChunkedBody() const;
183 };
184 
185 #endif // _QORE_QORE_SOCKET_OBJECT_H
defines string encoding functions in Qore
Definition: QoreEncoding.h:72
a helper class for getting socket origination information
Definition: QoreSocket.h:62
represents an X509 certificate, reference-counted, dynamically-allocated only
Definition: QoreSSLCertificate.h:34
This is the hash or associative list container type in Qore, dynamically allocated only...
Definition: QoreHashNode.h:41
the base class for all data to be used as private data of Qore objects
Definition: AbstractPrivateData.h:36
The base class for all value and parse types in Qore expression trees.
Definition: AbstractQoreNode.h:47
long qore_offset_t
used for offsets that could be negative
Definition: common.h:65
provides access to a private key data structure for SSL connections
Definition: QoreSSLPrivateKey.h:32
Qore's string type supported by the QoreEncoding class.
Definition: QoreString.h:42
Qore's string value type, reference counted, dynamically-allocated only.
Definition: QoreStringNode.h:40
virtual DLLLOCAL void deref()
decrements the reference count of the object without the possibility of throwing a Qore-language exce...
Definition: AbstractPrivateData.h:57
provides access to sockets using Qore data structures
Definition: QoreSocket.h:115
the implementation of Qore's object data type, reference counted, dynamically-allocated only ...
Definition: QoreObject.h:56
container for holding Qore-language exception information and also for registering a "thread_exit" ca...
Definition: ExceptionSink.h:35
base class for resolved call references
Definition: CallReferenceNode.h:120
holds arbitrary binary data
Definition: BinaryNode.h:33