Changeset 21:eb59943050c9 in xplcommon for src
- Timestamp:
- 12/31/12 14:17:32 (12 years ago)
- Branch:
- default
- Phase:
- public
- Location:
- src/xplcommon
- Files:
-
- 22 added
- 3 deleted
- 19 edited
- 1 moved
Legend:
- Unmodified
- Added
- Removed
-
src/xplcommon/BufferedStream.h
r17 r21 58 58 public: 59 59 /** 60 * Destroy the buffered waitable.60 * Destroy the buffered stream. 61 61 */ 62 62 virtual ~BufferedStream(); … … 64 64 /** 65 65 * Get the reference of the reading buffer. Note, that it can be a 66 * 0 reference, if the waitablehas no reading buffer.66 * 0 reference, if the stream has no reading buffer. 67 67 */ 68 68 ReadingBuffer& getReadingBuffer(); … … 70 70 /** 71 71 * Get the reference of the writing buffer. Note, that it can be a 72 * 0 reference, if the waitablehas no writing buffer.72 * 0 reference, if the stream has no writing buffer. 73 73 */ 74 74 WritingBuffer& getWritingBuffer(); -
src/xplcommon/LocalAcceptor.h
r17 r21 32 32 //------------------------------------------------------------------------------ 33 33 34 #include " Acceptor.h"34 #include "Failable.h" 35 35 36 36 //------------------------------------------------------------------------------ … … 48 48 * connection. 49 49 */ 50 class LocalAcceptor : public Acceptor50 class LocalAcceptor : public FailableReference<LocalAcceptor> 51 51 { 52 52 public: 53 54 /** 55 * Start accepting a connection. 56 * 57 * If a connection is already accepted, but not retrieved yet, it 58 * returns true. 59 * 60 * If a new socket becomes immediately available, it returns true. 61 * 62 * If some error occurs, the acceptor is marked as failed. 63 */ 64 bool accept(); 65 53 66 /** 54 67 * Get the local socket accepted last. If no socket was accepted, 55 * return 0. The local socket's waiter will be the given one 56 * (which can be 0). 68 * return 0. The local socket's waiter will be the given one. If 69 * the server socket has no waiter, this one should be 0. If the 70 * server socket has a waiter, this one should not be 0. In other 71 * words, if the server socket is non-blocking, the accepted one 72 * should also be non-blocking, if the server socket is blocking, 73 * the accepted one should also be blocking. 57 74 */ 58 75 LocalSocket* getSocket(Waiter* waiter, -
src/xplcommon/LocalClientSocket.h
r17 r21 32 32 //------------------------------------------------------------------------------ 33 33 34 #include " ClientSocket.h"34 #include "BufferedStream.h" 35 35 36 36 //------------------------------------------------------------------------------ … … 44 44 //------------------------------------------------------------------------------ 45 45 46 class LocalConnector; 47 48 //------------------------------------------------------------------------------ 49 46 50 /** 47 51 * Client socket that connects to a server socket locally. On Linux it … … 50 54 * The server socket's name should be given. 51 55 */ 52 class LocalClientSocket : public ClientSocket56 class LocalClientSocket : public BufferedStream 53 57 { 54 58 public: … … 65 69 size_t readingCapacity = DEFAULT_CAPACITY, 66 70 size_t writingCapacity = DEFAULT_CAPACITY); 71 72 /** 73 * Get the connector. Use this object to connect to another socket. 74 */ 75 LocalConnector& getConnector(); 67 76 }; 68 77 -
src/xplcommon/LocalConnector.h
r17 r21 28 28 // either expressed or implied, of the FreeBSD Project. 29 29 30 #ifndef XPLCOMMON_ CONNECTOR_H31 #define XPLCOMMON_ CONNECTOR_H30 #ifndef XPLCOMMON_LOCALCONNECTOR_H 31 #define XPLCOMMON_LOCALCONNECTOR_H 32 32 //------------------------------------------------------------------------------ 33 33 … … 36 36 //------------------------------------------------------------------------------ 37 37 38 #if 0 // The public interface of a connector for a client socket38 #if 0 // The public interface of a connector for a local client socket 39 39 40 40 //------------------------------------------------------------------------------ … … 49 49 * only acquired from a client socket. 50 50 */ 51 class Connector : public FailableReference<Connector>51 class LocalConnector : public FailableReference<LocalConnector> 52 52 { 53 53 public: … … 73 73 //------------------------------------------------------------------------------ 74 74 75 #endif // 0, The public interface of a connector for a client socket75 #endif // 0, The public interface of a connector for a local client socket 76 76 77 77 //------------------------------------------------------------------------------ … … 85 85 //------------------------------------------------------------------------------ 86 86 87 #include "win32/ Connector.h"87 #include "win32/LocalConnector.h" 88 88 89 namespace xplcommon { typedef win32:: ConnectorConnector; }89 namespace xplcommon { typedef win32::LocalConnector LocalConnector; } 90 90 91 91 //------------------------------------------------------------------------------ … … 95 95 //------------------------------------------------------------------------------ 96 96 97 #include "posix/ Connector.h"97 #include "posix/LocalConnector.h" 98 98 99 namespace xplcommon { typedef posix:: ConnectorConnector; }99 namespace xplcommon { typedef posix::LocalConnector LocalConnector; } 100 100 101 101 //------------------------------------------------------------------------------ … … 104 104 105 105 //------------------------------------------------------------------------------ 106 #endif // XPLCOMMON_ CONNECTOR_H106 #endif // XPLCOMMON_LOCALCONNECTOR_H 107 107 108 108 // Local Variables: -
src/xplcommon/Makefile.am
r17 r21 32 32 ReadingBuffer.h \ 33 33 WritingBuffer.h \ 34 Acceptor.h \35 34 LocalAcceptor.h \ 36 35 LocalSocket.h \ 37 36 LocalServerSocket.h \ 38 Connector.h \ 39 ClientSocket.h \ 37 LocalConnector.h \ 40 38 LocalClientSocket.h -
src/xplcommon/ReadingBuffer.h
r17 r21 95 95 //------------------------------------------------------------------------------ 96 96 97 //#include "win32/ReadingBuffer.h"97 #include "win32/ReadingBuffer.h" 98 98 99 //namespace xplcommon { typedef win32::ReadingBuffer ReadingBuffer; }99 namespace xplcommon { typedef win32::ReadingBuffer ReadingBuffer; } 100 100 101 101 //------------------------------------------------------------------------------ -
src/xplcommon/WritingBuffer.h
r17 r21 101 101 //------------------------------------------------------------------------------ 102 102 103 //#include "win32/WritingBuffer.h"103 #include "win32/WritingBuffer.h" 104 104 105 //namespace xplcommon { typedef win32::WritingBuffer WritingBuffer; }105 namespace xplcommon { typedef win32::WritingBuffer WritingBuffer; } 106 106 107 107 //------------------------------------------------------------------------------ -
src/xplcommon/posix/ClientSocket.cc
r16 r21 41 41 42 42 using xplcommon::posix::ClientSocket; 43 using xplcommon::posix::Connector;44 45 //------------------------------------------------------------------------------46 47 ClientSocket::~ClientSocket()48 {49 delete connector;50 }51 52 //------------------------------------------------------------------------------53 54 Connector& ClientSocket::getConnector()55 {56 if (connector==0) {57 connector = createConnector();58 }59 return *connector;60 }61 43 62 44 //------------------------------------------------------------------------------ … … 64 46 void ClientSocket::handleEvents(short events) 65 47 { 66 if (connector !=0 && connector->connecting && (events&POLLOUT)==POLLOUT) {67 connector ->handleWritable();48 if (connector.connecting && (events&POLLOUT)==POLLOUT) { 49 connector.handleWritable(); 68 50 events &= ~POLLOUT; 69 51 } -
src/xplcommon/posix/ClientSocket.h
r14 r21 51 51 private: 52 52 /** 53 * The connector , if any. It is created only, if requested.53 * The connector. 54 54 */ 55 Connector *connector;55 Connector& connector; 56 56 57 57 protected: … … 59 59 * Construct the socket with the given parameters. 60 60 */ 61 ClientSocket(int domain, int type, Waiter* waiter = 0, int protocol = 0, 61 ClientSocket(int domain, int type, Connector& connector, 62 Waiter* waiter = 0, int protocol = 0, 62 63 size_t readingCapacity = DEFAULT_CAPACITY, 63 64 size_t writingCapacity = DEFAULT_CAPACITY); 64 65 65 public:66 /**67 * Destroy the socket.68 */69 virtual ~ClientSocket();70 71 /**72 * Get the connector. Use this object to connect to another socket.73 */74 Connector& getConnector();75 76 66 protected: 77 /**78 * Create a new connector of the correct type.79 */80 virtual Connector* createConnector() = 0;81 82 67 /** 83 68 * Handle any events on the file descriptor. If a connection is in … … 105 90 //------------------------------------------------------------------------------ 106 91 107 inline ClientSocket::ClientSocket(int domain, int type, Waiter* waiter, 108 int protocol, size_t readingCapacity, 92 inline ClientSocket::ClientSocket(int domain, int type, Connector& connector, 93 Waiter* waiter, int protocol, 94 size_t readingCapacity, 109 95 size_t writingCapacity) : 110 96 Socket(domain, type, waiter, protocol, readingCapacity, writingCapacity), 111 connector( 0)97 connector(connector) 112 98 { 113 99 } -
src/xplcommon/posix/LocalClientSocket.cc
r15 r21 32 32 #include "LocalClientSocket.h" 33 33 34 #include " Connector.h"34 #include "LocalConnector.h" 35 35 #include "LocalServerSocket.h" 36 36 … … 40 40 41 41 using xplcommon::posix::LocalClientSocket; 42 using xplcommon::posix::Connector;43 44 //------------------------------------------------------------------------------45 46 /**47 * The connector class used by the local client.48 */49 class LocalClientSocket::LocalConnector : public Connector50 {51 private:52 /**53 * The reference to the corresponding socket's address.54 */55 const struct sockaddr_un& address;56 57 public:58 /**59 * Create the connector for the given local client socket.60 */61 LocalConnector(LocalClientSocket* socket);62 63 protected:64 /**65 * Get the address.66 */67 virtual const struct sockaddr* getAddress(size_t& addrlen);68 };69 70 //------------------------------------------------------------------------------71 72 inline73 LocalClientSocket::LocalConnector::LocalConnector(LocalClientSocket* socket) :74 Connector(socket),75 address(socket->sun)76 {77 }78 79 //------------------------------------------------------------------------------80 81 const struct sockaddr*82 LocalClientSocket::LocalConnector::getAddress(size_t& addrlen)83 {84 addrlen = sizeof(address);85 return reinterpret_cast<const struct sockaddr*>(&address);86 }87 42 88 43 //------------------------------------------------------------------------------ … … 92 47 size_t readingCapacity, 93 48 size_t writingCapacity) : 94 ClientSocket(AF_UNIX, SOCK_STREAM, waiter, 0, 95 readingCapacity, writingCapacity) 49 ClientSocket(AF_UNIX, SOCK_STREAM, connector, waiter, 0, 50 readingCapacity, writingCapacity), 51 connector(this, name) 96 52 { 97 sun.sun_family = AF_UNIX;98 LocalServerSocket::setupPath(sun, name);99 }100 101 //------------------------------------------------------------------------------102 103 Connector* LocalClientSocket::createConnector()104 {105 return new LocalConnector(this);106 53 } 107 54 -
src/xplcommon/posix/LocalClientSocket.h
r15 r21 34 34 #include "ClientSocket.h" 35 35 36 #include <sys/un.h>36 #include "LocalConnector.h" 37 37 38 38 //------------------------------------------------------------------------------ … … 53 53 private: 54 54 /** 55 * The connector class.56 */57 class LocalConnector;58 59 /**60 55 * The address to use for connecting. 61 56 */ 62 struct sockaddr_un sun;57 LocalConnector connector; 63 58 64 59 public: … … 76 71 size_t writingCapacity = DEFAULT_CAPACITY); 77 72 78 protected:79 73 /** 80 * Create a new connector of the correct type.74 * Get the connector for this client. 81 75 */ 82 virtual Connector* createConnector();76 LocalConnector& getConnector(); 83 77 84 78 friend class LocalConnector; 85 79 }; 80 81 //------------------------------------------------------------------------------ 82 // Inline definitions 83 //------------------------------------------------------------------------------ 84 85 inline LocalConnector& LocalClientSocket::getConnector() 86 { 87 return connector; 88 } 86 89 87 90 //------------------------------------------------------------------------------ -
src/xplcommon/posix/Makefile.am
r16 r21 15 15 ClientSocket.cc \ 16 16 LocalServerSocket.cc \ 17 LocalConnector.cc \ 17 18 LocalClientSocket.cc 18 19 … … 35 36 LocalAcceptor.h \ 36 37 LocalServerSocket.h \ 38 LocalConnector.h \ 37 39 LocalClientSocket.h -
src/xplcommon/posix/ReadingBuffer.cc
r16 r21 34 34 #include "BufferedStream.h" 35 35 36 #include <cassert> 37 36 38 #include <poll.h> 37 39 … … 60 62 } else { 61 63 stream.events &= ~POLLIN; 62 addLength(result); 64 size_t added = addLength(result); 65 assert(added==result); 66 assert(getLength()==result); 63 67 } 64 68 return result>=0; -
src/xplcommon/posix/Waitable.h
r14 r21 93 93 94 94 /** 95 * Indicate if this waitable is valid, i.e. the file descriptor is96 * non-negative.97 */98 bool isValid() const;99 100 /**101 95 * Determine if the waitable is ready or not, i.e. if there is no 102 96 * need to wait for it. … … 143 137 //------------------------------------------------------------------------------ 144 138 145 inline bool Waitable::isValid() const146 {147 return fd>=0;148 }149 150 //------------------------------------------------------------------------------151 152 139 inline bool Waitable::ready() 153 140 { -
src/xplcommon/win32/Event.cc
r20 r21 40 40 //------------------------------------------------------------------------------ 41 41 42 bool Event::isFired() const 43 { 44 DWORD result = WaitForSingleObject(handle, (waiter==0) ? INFINITE : 0); 45 if (result==WAIT_FAILED) { 46 eventFailable.setErrorCode(GetLastError()); 47 return false; 48 } else if (result==WAIT_OBJECT_0) { 49 return true; 50 } else { 51 return false; 52 } 53 } 54 55 //------------------------------------------------------------------------------ 56 42 57 void Event::addTo(Waiter& w) 43 58 { -
src/xplcommon/win32/Event.h
r20 r21 32 32 //------------------------------------------------------------------------------ 33 33 34 #include " ../Failable.h"34 #include "EventFailable.h" 35 35 36 36 #include <windows.h> … … 49 49 * Wrapper for an event. 50 50 */ 51 class Event : public ::xplcommon::Failable51 class Event 52 52 { 53 53 private: 54 54 /** 55 * The object receiving the failure codes. 56 */ 57 EventFailable& eventFailable; 58 59 /** 55 60 * The handle of the event. 56 61 */ … … 66 71 * Construct the event. 67 72 */ 68 Event( );73 Event(EventFailable& eventFailable); 69 74 70 75 /** … … 96 101 97 102 /** 103 * Determine if the event is being waited for, i.e. it is 104 * associated with a waiter. 105 */ 106 bool isWaited() const; 107 108 /** 98 109 * Set the event. 99 110 */ 100 111 void fire(); 112 113 /** 114 * Determine if the event is fired or not. 115 */ 116 bool isFired() const; 101 117 102 118 /** … … 106 122 */ 107 123 bool clear(); 124 125 protected: 126 /** 127 * Set the error code on the failable object. 128 */ 129 void setErrorCode(errorCode_t errorCode); 108 130 }; 109 131 … … 112 134 //------------------------------------------------------------------------------ 113 135 114 inline Event::Event() : 136 inline Event::Event(EventFailable& eventFailable) : 137 eventFailable(eventFailable), 115 138 handle(CreateEvent(0, true, false, 0)), 116 139 waiter(0) 117 140 { 118 if (handle==0) setErrorCode(GetLastError());141 if (handle==0) eventFailable.setErrorCode(GetLastError()); 119 142 } 120 143 … … 143 166 //------------------------------------------------------------------------------ 144 167 168 inline bool Event::isWaited() const 169 { 170 return waiter!=0; 171 } 172 173 //------------------------------------------------------------------------------ 174 145 175 inline void Event::fire() 146 176 { 147 177 if (!SetEvent(handle)) { 148 setErrorCode(GetLastError());178 eventFailable.setErrorCode(GetLastError()); 149 179 } 150 180 } … … 155 185 { 156 186 if (!ResetEvent(handle)) { 157 setErrorCode(GetLastError());187 eventFailable.setErrorCode(GetLastError()); 158 188 return false; 159 189 } else { 160 190 return true; 161 191 } 192 } 193 194 //------------------------------------------------------------------------------ 195 196 inline void Event::setErrorCode(errorCode_t errorCode) 197 { 198 eventFailable.setErrorCode(errorCode); 162 199 } 163 200 -
src/xplcommon/win32/Makefile.am
r20 r21 6 6 Overlapped.cc \ 7 7 Waiter.cc \ 8 WaitableEvent.cc 8 BufferedStream.cc \ 9 ReadingBuffer.cc \ 10 WritingBuffer.cc \ 11 LocalAcceptor.cc \ 12 LocalServerSocketBase.cc\ 13 LocalConnector.cc \ 14 LocalClientSocket.cc 9 15 10 16 include_xplcommon_win32dir=$(includedir)/xplcommon/win32 11 17 include_xplcommon_win32_HEADERS=\ 12 18 Thread.h \ 19 EventFailable.h \ 13 20 Event.h \ 14 21 Overlapped.h \ 15 22 Waiter.h \ 16 WaitableEvent.h 23 WaitableEvent.h \ 24 Overlappable.h \ 25 Completer.h \ 26 StreamBuffer.h \ 27 BufferedStream.h \ 28 ReadingBuffer.h \ 29 WritingBuffer.h \ 30 LocalSocket.h \ 31 LocalAcceptor.h \ 32 LocalServerSocketBase.h \ 33 LocalServerSocket.h \ 34 LocalConnector.h \ 35 LocalClientSocket.h -
src/xplcommon/win32/Overlapped.cc
r20 r21 46 46 //------------------------------------------------------------------------------ 47 47 48 bool Overlapped::getResult(DWORD& size, HANDLE file) 49 { 50 bool result = GetOverlappedResult(file, &overlapped, &size, FALSE); 51 if (!result) { 52 DWORD error = GetLastError(); 53 if (error!=ERROR_IO_PENDING && error!=ERROR_IO_INCOMPLETE) { 54 reset(); 55 setErrorCode(error); 56 } 57 } else { 58 reset(); 59 } 60 return result; 61 } 62 63 //------------------------------------------------------------------------------ 64 48 65 // Local Variables: 49 66 // mode: C++ -
src/xplcommon/win32/Overlapped.h
r20 r21 59 59 * 0, and the event handle will be set. 60 60 */ 61 Overlapped( );61 Overlapped(EventFailable& eventFailable); 62 62 63 63 /** … … 75 75 */ 76 76 const OVERLAPPED* get() const; 77 78 /** 79 * Get the result of the overlapped operation. 80 * 81 * If the operation has completed, the event will be cleared. 82 * 83 * @param size on successful return, it will contain the number of 84 * bytes read or written, which may be 0 for an end-of-file 85 * condition 86 * @param file the handle of the file on which the operation in 87 * question was performed 88 * 89 * @return true if the operation has completed, or false if it has 90 * not, or a failure has occured. 91 */ 92 bool getResult(DWORD& size, HANDLE file); 77 93 }; 78 94 … … 81 97 //------------------------------------------------------------------------------ 82 98 83 inline Overlapped::Overlapped() 99 inline Overlapped::Overlapped(EventFailable& eventFailable) : 100 Event(eventFailable) 84 101 { 85 102 reset(); -
src/xplcommon/win32/WaitableEvent.h
r20 r21 33 33 34 34 #include "Event.h" 35 #include "EventFailable.h" 35 36 36 37 //------------------------------------------------------------------------------ … … 43 44 * An event which can be waited for. 44 45 */ 45 class WaitableEvent : public Event 46 class WaitableEvent : public Event, public EventFailable 46 47 { 47 48 public: … … 62 63 //------------------------------------------------------------------------------ 63 64 64 inline WaitableEvent::WaitableEvent(Waiter* waiter) 65 inline WaitableEvent::WaitableEvent(Waiter* waiter) : 66 Event(static_cast<EventFailable&>(*this)) 65 67 { 66 68 if (waiter!=0) addTo(*waiter); 69 } 70 71 //------------------------------------------------------------------------------ 72 73 inline bool WaitableEvent::check() 74 { 75 if (isFired()) { 76 clear(); 77 return true; 78 } else { 79 return false; 80 } 67 81 } 68 82
Note:
See TracChangeset
for help on using the changeset viewer.