| 1 | /******************************************************************** |
| 2 | * Copyright (c) 2013 - 2014, Pivotal Inc. |
| 3 | * All rights reserved. |
| 4 | * |
| 5 | * Author: Zhanwei Wang |
| 6 | ********************************************************************/ |
| 7 | /******************************************************************** |
| 8 | * 2014 - |
| 9 | * open source under Apache License Version 2.0 |
| 10 | ********************************************************************/ |
| 11 | /** |
| 12 | * Licensed to the Apache Software Foundation (ASF) under one |
| 13 | * or more contributor license agreements. See the NOTICE file |
| 14 | * distributed with this work for additional information |
| 15 | * regarding copyright ownership. The ASF licenses this file |
| 16 | * to you under the Apache License, Version 2.0 (the |
| 17 | * "License"); you may not use this file except in compliance |
| 18 | * with the License. You may obtain a copy of the License at |
| 19 | * |
| 20 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 21 | * |
| 22 | * Unless required by applicable law or agreed to in writing, software |
| 23 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 24 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 25 | * See the License for the specific language governing permissions and |
| 26 | * limitations under the License. |
| 27 | */ |
| 28 | #ifndef _HDFS_LIBHDFS3_NETWORK_TCPSOCKET_H_ |
| 29 | #define _HDFS_LIBHDFS3_NETWORK_TCPSOCKET_H_ |
| 30 | |
| 31 | #include "Socket.h" |
| 32 | |
| 33 | namespace Hdfs { |
| 34 | namespace Internal { |
| 35 | |
| 36 | /** |
| 37 | * A tcp socket client |
| 38 | */ |
| 39 | class TcpSocketImpl: public Socket { |
| 40 | public: |
| 41 | /** |
| 42 | * Construct a Socket object. |
| 43 | * @throw nothrow |
| 44 | */ |
| 45 | TcpSocketImpl(); |
| 46 | |
| 47 | /** |
| 48 | * Destroy a TcpSocketImpl instance. |
| 49 | */ |
| 50 | ~TcpSocketImpl(); |
| 51 | |
| 52 | /** |
| 53 | * Read data from socket. |
| 54 | * If there is nothing can be read, the caller will be blocked. |
| 55 | * @param buffer The buffer to store the data. |
| 56 | * @param size The size of bytes to be read. |
| 57 | * @return The size of data already read. |
| 58 | * @throw HdfsNetworkException |
| 59 | * @throw HdfsEndOfStream |
| 60 | */ |
| 61 | int32_t read(char * buffer, int32_t size); |
| 62 | |
| 63 | /** |
| 64 | * Read data from socket until get enough data. |
| 65 | * If there is not enough data can be read, the caller will be blocked. |
| 66 | * @param buffer The buffer to store the data. |
| 67 | * @param size The size of bytes to be read. |
| 68 | * @param timeout The timeout interval of this read operation, negative means infinite. |
| 69 | * @throw HdfsNetworkException |
| 70 | * @throw HdfsEndOfStream |
| 71 | * @throw HdfsTimeout |
| 72 | */ |
| 73 | void readFully(char * buffer, int32_t size, int timeout); |
| 74 | |
| 75 | /** |
| 76 | * Send data to socket. |
| 77 | * The caller will be blocked until send operation finished, |
| 78 | * but not guarantee that all data has been sent. |
| 79 | * @param buffer The data to be sent. |
| 80 | * @param size The size of bytes to be sent. |
| 81 | * @return The size of data already be sent. |
| 82 | * @throw HdfsNetworkException |
| 83 | */ |
| 84 | int32_t write(const char * buffer, int32_t size); |
| 85 | |
| 86 | /** |
| 87 | * Send all data to socket. |
| 88 | * The caller will be blocked until all data has been sent. |
| 89 | * @param buffer The data to be sent. |
| 90 | * @param size The size of bytes to be sent. |
| 91 | * @param timeout The timeout interval of this write operation, negative means infinite. |
| 92 | * @throw HdfsNetworkException |
| 93 | * @throw HdfsTimeout |
| 94 | */ |
| 95 | void writeFully(const char * buffer, int32_t size, int timeout); |
| 96 | |
| 97 | /** |
| 98 | * Connection to a tcp server. |
| 99 | * @param host The host of server. |
| 100 | * @param port The port of server. |
| 101 | * @param timeout The timeout interval of this read operation, negative means infinite. |
| 102 | * @throw HdfsNetworkException |
| 103 | * @throw HdfsTimeout |
| 104 | */ |
| 105 | void connect(const char * host, int port, int timeout); |
| 106 | |
| 107 | /** |
| 108 | * Connection to a tcp server. |
| 109 | * @param host The host of server. |
| 110 | * @param port The port of server. |
| 111 | * @param timeout The timeout interval of this read operation, negative means infinite. |
| 112 | * @throw HdfsNetworkException |
| 113 | * @throw HdfsTimeout |
| 114 | */ |
| 115 | void connect(const char * host, const char * port, int timeout); |
| 116 | |
| 117 | /** |
| 118 | * Connection to a tcp server. |
| 119 | * @param paddr The address of server. |
| 120 | * @param host The host of server used in error message. |
| 121 | * @param port The port of server used in error message. |
| 122 | * @param timeout The timeout interval of this read operation, negative means infinite. |
| 123 | * @throw HdfsNetworkException |
| 124 | * @throw HdfsTimeout |
| 125 | */ |
| 126 | void connect(struct addrinfo * paddr, const char * host, const char * port, |
| 127 | int timeout); |
| 128 | |
| 129 | /** |
| 130 | * Test if the socket can be read or written without blocking. |
| 131 | * @param read Test socket if it can be read. |
| 132 | * @param write Test socket if it can be written. |
| 133 | * @param timeout Time timeout interval of this operation, negative means infinite. |
| 134 | * @return Return true if the socket can be read or written without blocking, false on timeout. |
| 135 | * @throw HdfsNetworkException |
| 136 | * @throw HdfsTimeout |
| 137 | */ |
| 138 | bool poll(bool read, bool write, int timeout); |
| 139 | |
| 140 | /** |
| 141 | * Set socket blocking mode. |
| 142 | * @param enable If true, set socket into blocking mode, else non-block mode. |
| 143 | * @throw HdfsNetworkException |
| 144 | */ |
| 145 | void setBlockMode(bool enable); |
| 146 | |
| 147 | /** |
| 148 | * Set socket no delay mode. |
| 149 | * @param enable If true, set socket into no delay mode, else delay mode. |
| 150 | * @throw HdfsNetworkException |
| 151 | */ |
| 152 | void setNoDelay(bool enable); |
| 153 | |
| 154 | /** |
| 155 | * Set socket linger timeout |
| 156 | * @param timeout Linger timeout of the socket in millisecond, disable linger if it is less than 0. |
| 157 | * @throw HdfsNetworkException |
| 158 | */ |
| 159 | void setLingerTimeout(int timeout); |
| 160 | |
| 161 | /** |
| 162 | * Disable SIGPIPE on this socket. |
| 163 | * It only works on some platform. |
| 164 | * Write operation guarantee that no SIGPIPE will be raised. |
| 165 | * @throw HdfsNetworkException |
| 166 | */ |
| 167 | void disableSigPipe(); |
| 168 | |
| 169 | /** |
| 170 | * Shutdown and close the socket. |
| 171 | * @throw nothrow |
| 172 | */ |
| 173 | void close(); |
| 174 | |
| 175 | private: |
| 176 | void setLingerTimeoutInternal(int timeout); |
| 177 | void setSendTimeout(int timeout); |
| 178 | |
| 179 | protected: |
| 180 | int sock; |
| 181 | int lingerTimeout; |
| 182 | std::string remoteAddr; //used for error message |
| 183 | |
| 184 | }; |
| 185 | |
| 186 | } |
| 187 | } |
| 188 | |
| 189 | #endif /* _HDFS_LIBHDFS3_NETWORK_TCPSOCKET_H_ */ |
| 190 | |