1 /*-------------------------------------------------------------------------
2 * drawElements C++ Base Library
3 * -----------------------------
5 * Copyright 2014 The Android Open Source Project
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
21 * \brief deSocket C++ wrapper.
22 *//*--------------------------------------------------------------------*/
24 #include "deSocket.hpp"
34 SocketAddress::SocketAddress (void)
36 m_address = deSocketAddress_create();
38 throw std::bad_alloc();
41 SocketAddress::~SocketAddress (void)
43 deSocketAddress_destroy(m_address);
46 void SocketAddress::setHost (const char* host)
48 if (!deSocketAddress_setHost(m_address, host))
49 throw std::runtime_error("SocketAddress::setHost()");
52 void SocketAddress::setPort (int port)
54 if (!deSocketAddress_setPort(m_address, port))
55 throw std::runtime_error("SocketAddress::setPort()");
58 void SocketAddress::setFamily (deSocketFamily family)
60 if (!deSocketAddress_setFamily(m_address, family))
61 throw std::runtime_error("SocketAddress::setFamily()");
64 void SocketAddress::setType (deSocketType type)
66 if (!deSocketAddress_setType(m_address, type))
67 throw std::runtime_error("SocketAddress::setType()");
70 void SocketAddress::setProtocol (deSocketProtocol protocol)
72 if (!deSocketAddress_setProtocol(m_address, protocol))
73 throw std::runtime_error("SocketAddress::setProtocol()");
80 m_socket = deSocket_create();
82 throw std::bad_alloc();
85 Socket::~Socket (void)
87 deSocket_destroy(m_socket);
90 void Socket::setFlags (deUint32 flags)
92 if (!deSocket_setFlags(m_socket, flags))
93 throw SocketError("Setting socket flags failed");
96 void Socket::listen (const SocketAddress& address)
98 if (!deSocket_listen(m_socket, address))
99 throw SocketError("Listening on socket failed");
102 void Socket::connect (const SocketAddress& address)
104 if (!deSocket_connect(m_socket, address))
105 throw SocketError("Connecting socket failed");
108 void Socket::shutdown (void)
110 if (!deSocket_shutdown(m_socket, DE_SOCKETCHANNEL_BOTH))
111 throw SocketError("Socket shutdown failed");
114 void Socket::shutdownSend (void)
116 if (!deSocket_shutdown(m_socket, DE_SOCKETCHANNEL_SEND))
117 throw SocketError("Socket send channel shutdown failed");
120 void Socket::shutdownReceive (void)
122 if (!deSocket_shutdown(m_socket, DE_SOCKETCHANNEL_RECEIVE))
123 throw SocketError("Socket receive channel shutdown failed");
126 void Socket::close (void)
128 if (!deSocket_close(m_socket))
129 throw SocketError("Closing socket failed");
132 Socket* Socket::accept (deSocketAddress* clientAddress)
134 deSocket* clientSocket = deSocket_accept(m_socket, clientAddress);
136 throw SocketError("Accepting connection to socket failed");
140 return new Socket(clientSocket);
144 deSocket_destroy(clientSocket);