Alexandre Lision | ddd731e | 2014-01-31 11:50:08 -0500 | [diff] [blame] | 1 | // Copyright (C) 2006-2010 David Sugar, Tycho Softworks. |
| 2 | // |
| 3 | // This file is part of GNU uCommon C++. |
| 4 | // |
| 5 | // GNU uCommon C++ is free software: you can redistribute it and/or modify |
| 6 | // it under the terms of the GNU Lesser General Public License as published |
| 7 | // by the Free Software Foundation, either version 3 of the License, or |
| 8 | // (at your option) any later version. |
| 9 | // |
| 10 | // GNU uCommon C++ is distributed in the hope that it will be useful, |
| 11 | // but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | // GNU Lesser General Public License for more details. |
| 14 | // |
| 15 | // You should have received a copy of the GNU Lesser General Public License |
| 16 | // along with GNU uCommon C++. If not, see <http://www.gnu.org/licenses/>. |
| 17 | |
| 18 | /** |
| 19 | * Classes which use the buffer protocol to stream data. |
| 20 | * @file ucommon/buffer.h |
| 21 | */ |
| 22 | |
| 23 | #ifndef _UCOMMON_BUFFER_H_ |
| 24 | #define _UCOMMON_BUFFER_H_ |
| 25 | |
| 26 | #ifndef _UCOMMON_CONFIG_H_ |
| 27 | #include <ucommon/platform.h> |
| 28 | #endif |
| 29 | |
| 30 | #ifndef _UCOMMON_PROTOCOLS_H_ |
| 31 | #include <ucommon/protocols.h> |
| 32 | #endif |
| 33 | |
| 34 | #ifndef _UCOMMON_SOCKET_H_ |
| 35 | #include <ucommon/socket.h> |
| 36 | #endif |
| 37 | |
| 38 | #ifndef _UCOMMON_STRING_H_ |
| 39 | #include <ucommon/string.h> |
| 40 | #endif |
| 41 | |
| 42 | #ifndef _UCOMMON_FSYS_H_ |
| 43 | #include <ucommon/fsys.h> |
| 44 | #endif |
| 45 | |
| 46 | #ifndef _UCOMMON_SHELL_H_ |
| 47 | #include <ucommon/shell.h> |
| 48 | #endif |
| 49 | |
| 50 | NAMESPACE_UCOMMON |
| 51 | |
| 52 | /** |
| 53 | * A generic tcp socket class that offers i/o buffering. All user i/o |
| 54 | * operations are directly inherited from the IOBuffer base class public |
| 55 | * members. Some additional members are added for layering ssl services. |
| 56 | * @author David Sugar <dyfet@gnutelephony.org> |
| 57 | */ |
| 58 | class __EXPORT TCPBuffer : public BufferProtocol, protected Socket |
| 59 | { |
| 60 | protected: |
| 61 | void _buffer(size_t size); |
| 62 | |
| 63 | virtual size_t _push(const char *address, size_t size); |
| 64 | virtual size_t _pull(char *address, size_t size); |
| 65 | int _err(void) const; |
| 66 | void _clear(void); |
| 67 | bool _blocking(void); |
| 68 | |
| 69 | /** |
| 70 | * Get the low level socket object. |
| 71 | * @return socket we are using. |
| 72 | */ |
| 73 | inline socket_t getsocket(void) const |
| 74 | {return so;}; |
| 75 | |
| 76 | public: |
| 77 | /** |
| 78 | * Construct an unconnected tcp client and specify our service profile. |
| 79 | */ |
| 80 | TCPBuffer(); |
| 81 | |
| 82 | /** |
| 83 | * Construct a tcp server session from a listening socket. |
| 84 | * @param server socket we are created from. |
| 85 | * @param size of buffer and tcp fragments. |
| 86 | */ |
| 87 | TCPBuffer(const TCPServer *server, size_t size = 536); |
| 88 | |
| 89 | /** |
| 90 | * Construct a tcp client session connected to a specific host uri. |
| 91 | * @param host and optional :port we are connecting to. |
| 92 | * @param service identifier of our client. |
| 93 | * @param size of buffer and tcp fragments. |
| 94 | */ |
| 95 | TCPBuffer(const char *host, const char *service, size_t size = 536); |
| 96 | |
| 97 | /** |
| 98 | * Destroy the tcp socket and release all resources. |
| 99 | */ |
| 100 | virtual ~TCPBuffer(); |
| 101 | |
| 102 | /** |
| 103 | * Connect a tcp socket to a client from a listener. If the socket was |
| 104 | * already connected, it is automatically closed first. |
| 105 | * @param server we are connected from. |
| 106 | * @param size of buffer and tcp fragments. |
| 107 | */ |
| 108 | void open(const TCPServer *server, size_t size = 536); |
| 109 | |
| 110 | /** |
| 111 | * Connect a tcp client session to a specific host uri. If the socket |
| 112 | * was already connected, it is automatically closed first. |
| 113 | * @param host we are connecting. |
| 114 | * @param service to connect to. |
| 115 | * @param size of buffer and tcp fragments. |
| 116 | */ |
| 117 | void open(const char *host, const char *service, size_t size = 536); |
| 118 | |
| 119 | /** |
| 120 | * Close active connection. |
| 121 | */ |
| 122 | void close(void); |
| 123 | |
| 124 | protected: |
| 125 | /** |
| 126 | * Check for pending tcp or ssl data. |
| 127 | * @return true if data pending. |
| 128 | */ |
| 129 | virtual bool _pending(void); |
| 130 | }; |
| 131 | |
| 132 | /** |
| 133 | * Convenience type for pure tcp sockets. |
| 134 | */ |
| 135 | typedef TCPBuffer tcp_t; |
| 136 | |
| 137 | END_NAMESPACE |
| 138 | |
| 139 | #endif |