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 class SocketAddress 42 { 43 public: 44 SocketAddress (void); 45 ~SocketAddress (void); 46 47 void setHost (const char* host); 48 void setPort (int port); 49 void setFamily (deSocketFamily family); 50 void setType (deSocketType type); 51 void setProtocol (deSocketProtocol protocol); 52 getHost(void) const53 const char* getHost (void) const { return deSocketAddress_getHost(m_address); } getPort(void) const54 int getPort (void) const { return deSocketAddress_getPort(m_address); } getFamily(void) const55 deSocketFamily getFamily (void) const { return deSocketAddress_getFamily(m_address); } getType(void) const56 deSocketType getType (void) const { return deSocketAddress_getType(m_address); } getProtocol(void) const57 deSocketProtocol getProtocol (void) const { return deSocketAddress_getProtocol(m_address); } 58 operator deSocketAddress*()59 operator deSocketAddress* () { return m_address; } operator const deSocketAddress*() const60 operator const deSocketAddress* () const { return m_address; } 61 getPtr(void)62 deSocketAddress* getPtr (void) { return m_address; } 63 64 65 private: 66 SocketAddress (const SocketAddress& other); 67 SocketAddress& operator= (const SocketAddress& other); 68 69 deSocketAddress* m_address; 70 }; 71 72 class Socket 73 { 74 public: 75 Socket (void); 76 ~Socket (void); 77 78 void setFlags (deUint32 flags); 79 getState(void) const80 deSocketState getState (void) const { return deSocket_getState(m_socket); } isConnected(void) const81 bool isConnected (void) const { return getState() == DE_SOCKETSTATE_CONNECTED; } 82 83 void listen (const SocketAddress& address); accept(SocketAddress & clientAddress)84 Socket* accept (SocketAddress& clientAddress) { return accept(clientAddress.getPtr()); } accept(void)85 Socket* accept (void) { return accept(DE_NULL); } 86 87 void connect (const SocketAddress& address); 88 89 void shutdown (void); 90 void shutdownSend (void); 91 void shutdownReceive (void); 92 isSendOpen(void)93 bool isSendOpen (void) { return (deSocket_getOpenChannels(m_socket) & DE_SOCKETCHANNEL_SEND ) != 0; } isReceiveOpen(void)94 bool isReceiveOpen (void) { return (deSocket_getOpenChannels(m_socket) & DE_SOCKETCHANNEL_RECEIVE ) != 0; } 95 96 void close (void); 97 send(const void * buf,size_t bufSize,size_t * numSent)98 deSocketResult send (const void* buf, size_t bufSize, size_t* numSent) { return deSocket_send(m_socket, buf, bufSize, numSent); } receive(void * buf,size_t bufSize,size_t * numRecv)99 deSocketResult receive (void* buf, size_t bufSize, size_t* numRecv) { return deSocket_receive(m_socket, buf, bufSize, numRecv); } 100 101 private: Socket(deSocket * socket)102 Socket (deSocket* socket) : m_socket(socket) {} 103 Socket (const Socket& other); 104 Socket& operator= (const Socket& other); 105 106 Socket* accept (deSocketAddress* clientAddress); 107 108 deSocket* m_socket; 109 }; 110 111 } // de 112 113 #endif // _DESOCKET_HPP 114