xref: /aosp_15_r20/external/deqp/framework/delibs/decpp/deSocket.hpp (revision 35238bce31c2a825756842865a792f8cf7f89930)
1 #ifndef _DESOCKET_HPP
2 #define _DESOCKET_HPP
3 /*-------------------------------------------------------------------------
4  * drawElements C++ Base Library
5  * -----------------------------
6  *
7  * Copyright 2014 The Android Open Source Project
8  *
9  * Licensed under the Apache License, Version 2.0 (the "License");
10  * you may not use this file except in compliance with the License.
11  * You may obtain a copy of the License at
12  *
13  *      http://www.apache.org/licenses/LICENSE-2.0
14  *
15  * Unless required by applicable law or agreed to in writing, software
16  * distributed under the License is distributed on an "AS IS" BASIS,
17  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18  * See the License for the specific language governing permissions and
19  * limitations under the License.
20  *
21  *//*!
22  * \file
23  * \brief deSocket C++ wrapper.
24  *//*--------------------------------------------------------------------*/
25 
26 #include "deDefs.hpp"
27 #include "deSocket.h"
28 
29 #include <string>
30 #include <stdexcept>
31 
32 namespace de
33 {
34 
35 class SocketError : public std::runtime_error
36 {
37 public:
SocketError(const std::string & message)38     SocketError(const std::string &message) : std::runtime_error(message)
39     {
40     }
41 };
42 
43 class SocketAddress
44 {
45 public:
46     SocketAddress(void);
47     ~SocketAddress(void);
48 
49     void setHost(const char *host);
50     void setPort(int port);
51     void setFamily(deSocketFamily family);
52     void setType(deSocketType type);
53     void setProtocol(deSocketProtocol protocol);
54 
getHost(void) const55     const char *getHost(void) const
56     {
57         return deSocketAddress_getHost(m_address);
58     }
getPort(void) const59     int getPort(void) const
60     {
61         return deSocketAddress_getPort(m_address);
62     }
getFamily(void) const63     deSocketFamily getFamily(void) const
64     {
65         return deSocketAddress_getFamily(m_address);
66     }
getType(void) const67     deSocketType getType(void) const
68     {
69         return deSocketAddress_getType(m_address);
70     }
getProtocol(void) const71     deSocketProtocol getProtocol(void) const
72     {
73         return deSocketAddress_getProtocol(m_address);
74     }
75 
operator deSocketAddress*()76     operator deSocketAddress *()
77     {
78         return m_address;
79     }
operator const deSocketAddress*() const80     operator const deSocketAddress *() const
81     {
82         return m_address;
83     }
84 
getPtr(void)85     deSocketAddress *getPtr(void)
86     {
87         return m_address;
88     }
89 
90 private:
91     SocketAddress(const SocketAddress &other);
92     SocketAddress &operator=(const SocketAddress &other);
93 
94     deSocketAddress *m_address;
95 };
96 
97 class Socket
98 {
99 public:
100     Socket(void);
101     ~Socket(void);
102 
103     void setFlags(uint32_t flags);
104 
getState(void) const105     deSocketState getState(void) const
106     {
107         return deSocket_getState(m_socket);
108     }
isConnected(void) const109     bool isConnected(void) const
110     {
111         return getState() == DE_SOCKETSTATE_CONNECTED;
112     }
113 
114     void listen(const SocketAddress &address);
accept(SocketAddress & clientAddress)115     Socket *accept(SocketAddress &clientAddress)
116     {
117         return accept(clientAddress.getPtr());
118     }
accept(void)119     Socket *accept(void)
120     {
121         return accept(DE_NULL);
122     }
123 
124     void connect(const SocketAddress &address);
125 
126     void shutdown(void);
127     void shutdownSend(void);
128     void shutdownReceive(void);
129 
isSendOpen(void)130     bool isSendOpen(void)
131     {
132         return (deSocket_getOpenChannels(m_socket) & DE_SOCKETCHANNEL_SEND) != 0;
133     }
isReceiveOpen(void)134     bool isReceiveOpen(void)
135     {
136         return (deSocket_getOpenChannels(m_socket) & DE_SOCKETCHANNEL_RECEIVE) != 0;
137     }
138 
139     void close(void);
140 
send(const void * buf,size_t bufSize,size_t * numSent)141     deSocketResult send(const void *buf, size_t bufSize, size_t *numSent)
142     {
143         return deSocket_send(m_socket, buf, bufSize, numSent);
144     }
receive(void * buf,size_t bufSize,size_t * numRecv)145     deSocketResult receive(void *buf, size_t bufSize, size_t *numRecv)
146     {
147         return deSocket_receive(m_socket, buf, bufSize, numRecv);
148     }
149 
150 private:
Socket(deSocket * socket)151     Socket(deSocket *socket) : m_socket(socket)
152     {
153     }
154     Socket(const Socket &other);
155     Socket &operator=(const Socket &other);
156 
157     Socket *accept(deSocketAddress *clientAddress);
158 
159     deSocket *m_socket;
160 };
161 
162 } // namespace de
163 
164 #endif // _DESOCKET_HPP
165