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_SOCKET_H_ |
29 | #define _HDFS_LIBHDFS3_NETWORK_SOCKET_H_ |
30 | |
31 | #include <netdb.h> |
32 | |
33 | #include <string> |
34 | |
35 | namespace Hdfs { |
36 | namespace Internal { |
37 | |
38 | class Socket { |
39 | public: |
40 | |
41 | virtual ~Socket() { |
42 | } |
43 | |
44 | /** |
45 | * Read data from socket. |
46 | * If there is nothing can be read, the caller will be blocked. |
47 | * @param buffer The buffer to store the data. |
48 | * @param size The size of bytes to be read. |
49 | * @return The size of data already read. |
50 | * @throw HdfsNetworkException |
51 | * @throw HdfsEndOfStream |
52 | */ |
53 | virtual int32_t read(char * buffer, int32_t size) = 0; |
54 | |
55 | /** |
56 | * Read data from socket until get enough data. |
57 | * If there is not enough data can be read, the caller will be blocked. |
58 | * @param buffer The buffer to store the data. |
59 | * @param size The size of bytes to be read. |
60 | * @param timeout The timeout interval of this read operation, negative means infinite. |
61 | * @throw HdfsNetworkException |
62 | * @throw HdfsEndOfStream |
63 | * @throw HdfsTimeout |
64 | */ |
65 | virtual void readFully(char * buffer, int32_t size, int timeout) = 0; |
66 | |
67 | /** |
68 | * Send data to socket. |
69 | * The caller will be blocked until send operation finished, |
70 | * but not guarantee that all data has been sent. |
71 | * @param buffer The data to be sent. |
72 | * @param size The size of bytes to be sent. |
73 | * @return The size of data already be sent. |
74 | * @throw HdfsNetworkException |
75 | */ |
76 | virtual int32_t write(const char * buffer, int32_t size) = 0; |
77 | |
78 | /** |
79 | * Send all data to socket. |
80 | * The caller will be blocked until all data has been sent. |
81 | * @param buffer The data to be sent. |
82 | * @param size The size of bytes to be sent. |
83 | * @param timeout The timeout interval of this write operation, negative means infinite. |
84 | * @throw HdfsNetworkException |
85 | * @throw HdfsTimeout |
86 | */ |
87 | virtual void writeFully(const char * buffer, int32_t size, int timeout) = 0; |
88 | |
89 | /** |
90 | * Connection to a tcp server. |
91 | * @param host The host of server. |
92 | * @param port The port of server. |
93 | * @param timeout The timeout interval of this read operation, negative means infinite. |
94 | * @throw HdfsNetworkException |
95 | * @throw HdfsTimeout |
96 | */ |
97 | virtual void connect(const char * host, int port, int timeout) = 0; |
98 | |
99 | /** |
100 | * Connection to a tcp server. |
101 | * @param host The host of server. |
102 | * @param port The port of server. |
103 | * @param timeout The timeout interval of this read operation, negative means infinite. |
104 | * @throw HdfsNetworkException |
105 | * @throw HdfsTimeout |
106 | */ |
107 | virtual void connect(const char * host, const char * port, int timeout) = 0; |
108 | |
109 | /** |
110 | * Connection to a tcp server. |
111 | * @param paddr The address of server. |
112 | * @param host The host of server used in error message. |
113 | * @param port The port of server used in error message. |
114 | * @param timeout The timeout interval of this read operation, negative means infinite. |
115 | * @throw HdfsNetworkException |
116 | * @throw HdfsTimeout |
117 | */ |
118 | virtual void connect(struct addrinfo * paddr, const char * host, |
119 | const char * port, int timeout) = 0; |
120 | |
121 | /** |
122 | * Test if the socket can be read or written without blocking. |
123 | * @param read Test socket if it can be read. |
124 | * @param write Test socket if it can be written. |
125 | * @param timeout Time timeout interval of this operation, negative means infinite. |
126 | * @return Return true if the socket can be read or written without blocking, false on timeout. |
127 | * @throw HdfsNetworkException |
128 | * @throw HdfsTimeout |
129 | */ |
130 | virtual bool poll(bool read, bool write, int timeout) = 0; |
131 | |
132 | /** |
133 | * Set socket blocking mode. |
134 | * @param enable If true, set socket into blocking mode, else non-block mode. |
135 | * @throw HdfsNetworkException |
136 | */ |
137 | virtual void setBlockMode(bool enable) = 0; |
138 | |
139 | /** |
140 | * Set socket no delay mode. |
141 | * @param enable If true, set socket into no delay mode, else delay mode. |
142 | * @throw HdfsNetworkException |
143 | */ |
144 | virtual void setNoDelay(bool enable) = 0; |
145 | |
146 | /** |
147 | * Set socket linger timeout |
148 | * @param timeout Linger timeout of the socket in millisecond, disable linger if it is less than 0. |
149 | * @throw HdfsNetworkException |
150 | */ |
151 | virtual void setLingerTimeout(int timeout) = 0; |
152 | |
153 | /** |
154 | * Disable SIGPIPE on this socket. |
155 | * It only works on some platform. |
156 | * Write operation guarantee that no SIGPIPE will be raised. |
157 | * @throw HdfsNetworkException |
158 | */ |
159 | virtual void disableSigPipe() = 0; |
160 | |
161 | /** |
162 | * Shutdown and close the socket. |
163 | * @throw nothrow |
164 | */ |
165 | virtual void close() = 0; |
166 | |
167 | }; |
168 | |
169 | } |
170 | } |
171 | |
172 | #endif /* _HDFS_LIBHDFS3_NETWORK_SOCKET_H_ */ |
173 | |