1/*-------------------------------------------------------------------------
2 * drawElements C++ Base Library
3 * -----------------------------
4 *
5 * Copyright 2014 The Android Open Source Project
6 *
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
10 *
11 *      http://www.apache.org/licenses/LICENSE-2.0
12 *
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.
18 *
19 *//*!
20 * \file
21 * \brief deSocket C++ wrapper.
22 *//*--------------------------------------------------------------------*/
23
24#include "deSocket.hpp"
25
26#include <new>
27#include <exception>
28
29namespace de
30{
31
32// SocketAddress
33
34SocketAddress::SocketAddress (void)
35{
36	m_address = deSocketAddress_create();
37	if (!m_address)
38		throw std::bad_alloc();
39}
40
41SocketAddress::~SocketAddress (void)
42{
43	deSocketAddress_destroy(m_address);
44}
45
46void SocketAddress::setHost (const char* host)
47{
48	if (!deSocketAddress_setHost(m_address, host))
49		throw std::runtime_error("SocketAddress::setHost()");
50}
51
52void SocketAddress::setPort (int port)
53{
54	if (!deSocketAddress_setPort(m_address, port))
55		throw std::runtime_error("SocketAddress::setPort()");
56}
57
58void SocketAddress::setFamily (deSocketFamily family)
59{
60	if (!deSocketAddress_setFamily(m_address, family))
61		throw std::runtime_error("SocketAddress::setFamily()");
62}
63
64void SocketAddress::setType (deSocketType type)
65{
66	if (!deSocketAddress_setType(m_address, type))
67		throw std::runtime_error("SocketAddress::setType()");
68}
69
70void SocketAddress::setProtocol (deSocketProtocol protocol)
71{
72	if (!deSocketAddress_setProtocol(m_address, protocol))
73		throw std::runtime_error("SocketAddress::setProtocol()");
74}
75
76// Socket
77
78Socket::Socket (void)
79{
80	m_socket = deSocket_create();
81	if (!m_socket)
82		throw std::bad_alloc();
83}
84
85Socket::~Socket (void)
86{
87	deSocket_destroy(m_socket);
88}
89
90void Socket::setFlags (deUint32 flags)
91{
92	if (!deSocket_setFlags(m_socket, flags))
93		throw SocketError("Setting socket flags failed");
94}
95
96void Socket::listen (const SocketAddress& address)
97{
98	if (!deSocket_listen(m_socket, address))
99		throw SocketError("Listening on socket failed");
100}
101
102void Socket::connect (const SocketAddress& address)
103{
104	if (!deSocket_connect(m_socket, address))
105		throw SocketError("Connecting socket failed");
106}
107
108void Socket::shutdown (void)
109{
110	if (!deSocket_shutdown(m_socket, DE_SOCKETCHANNEL_BOTH))
111		throw SocketError("Socket shutdown failed");
112}
113
114void Socket::shutdownSend (void)
115{
116	if (!deSocket_shutdown(m_socket, DE_SOCKETCHANNEL_SEND))
117		throw SocketError("Socket send channel shutdown failed");
118}
119
120void Socket::shutdownReceive (void)
121{
122	if (!deSocket_shutdown(m_socket, DE_SOCKETCHANNEL_RECEIVE))
123		throw SocketError("Socket receive channel shutdown failed");
124}
125
126void Socket::close (void)
127{
128	if (!deSocket_close(m_socket))
129		throw SocketError("Closing socket failed");
130}
131
132Socket* Socket::accept (deSocketAddress* clientAddress)
133{
134	deSocket* clientSocket = deSocket_accept(m_socket, clientAddress);
135	if (!clientSocket)
136		throw SocketError("Accepting connection to socket failed");
137
138	try
139	{
140		return new Socket(clientSocket);
141	}
142	catch (...)
143	{
144		deSocket_destroy(clientSocket);
145		throw;
146	}
147}
148
149} // de
150