Hazelcast C++ Client
SSLSocket.h
1 /*
2  * Copyright (c) 2008-2018, Hazelcast, Inc. All Rights Reserved.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 #ifndef HAZELCAST_CLIENT_INTERNAL_SOCKET_SSLSOCKET_H_
17 #define HAZELCAST_CLIENT_INTERNAL_SOCKET_SSLSOCKET_H_
18 
19 #ifdef HZ_BUILD_WITH_SSL
20 
21 #include <asio.hpp>
22 #include <asio/ssl.hpp>
23 
24 #include "hazelcast/client/Socket.h"
25 #include "hazelcast/client/Address.h"
26 #include "hazelcast/client/config/SocketOptions.h"
27 #include "hazelcast/util/AtomicBoolean.h"
28 
29 #if !defined(MSG_NOSIGNAL)
30 # define MSG_NOSIGNAL 0
31 #endif
32 
33 #if defined(WIN32) || defined(_WIN32) || defined(WIN64) || defined(_WIN64)
34 #pragma warning(push)
35 #pragma warning(disable: 4251) //for dll export
36 #pragma warning(disable: 4003) //for not enough actual parameters for macro 'min' in asio wait_traits
37 #endif
38 
39 namespace hazelcast {
40  namespace client {
41  namespace internal {
42  namespace socket {
46  class HAZELCAST_API SSLSocket : public Socket {
47  public:
48  struct CipherInfo {
49  std::string name;
50  int numberOfBits;
51  std::string version;
52  std::string description;
53  };
54 
58  SSLSocket(const client::Address &address, asio::ssl::context &sslContext,
59  client::config::SocketOptions &socketOptions);
60 
64  virtual ~SSLSocket();
65 
71  int connect(int timeoutInMillis);
72 
80  int send(const void *buffer, int len, int flag = 0);
81 
89  int receive(void *buffer, int len, int flag = 0);
90 
94  int getSocketId() const;
95 
100  void close();
101 
102  client::Address getAddress() const;
103 
104  void setBlocking(bool blocking);
105 
109  std::vector<SSLSocket::CipherInfo> getCiphers() const;
110 
111  std::auto_ptr<Address> localSocketAddress() const;
112 
113  private:
114  SSLSocket(const Socket &rhs);
115 
116  SSLSocket &operator=(const Socket &rhs);
117 
118  class ReadHandler {
119  public:
120  ReadHandler(size_t &numRead, asio::error_code &ec);
121 
122  void operator()(const asio::error_code &err, std::size_t bytes_transferred);
123 
124  size_t &getNumRead() const;
125 
126  asio::error_code &getErrorCode() const;
127 
128  private:
129  size_t &numRead;
130  asio::error_code &errorCode;
131  };
132 
137  int handleError(const std::string &source, size_t numBytes, const asio::error_code &error) const;
138 
139  void handleConnect(const asio::error_code &error);
140 
141  void checkDeadline(const asio::error_code &ec);
142 
143  void setSocketOptions();
144 
145  client::Address remoteEndpoint;
146 
147  asio::io_service ioService;
148  asio::ssl::context &sslContext;
149  std::auto_ptr<asio::ssl::stream<asio::ip::tcp::socket> > socket;
150  asio::deadline_timer deadline;
151  asio::error_code errorCode;
152  int socketId;
153  const client::config::SocketOptions &socketOptions;
154  };
155 
156  std::ostream &operator<<(std::ostream &out, const SSLSocket::CipherInfo &info);
157  }
158  }
159  }
160 }
161 
162 #if defined(WIN32) || defined(_WIN32) || defined(WIN64) || defined(_WIN64)
163 #pragma warning(pop)
164 #endif
165 
166 #endif /* HZ_BUILD_WITH_SSL */
167 
168 #endif /* HAZELCAST_CLIENT_INTERNAL_SOCKET_SSLSOCKET_H_ */
PN (Positive-Negative) CRDT counter.
Definition: MapEntryView.h:32