Hazelcast C++ Client
TcpSocket.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_TCPSOCKET_H_
17 #define HAZELCAST_CLIENT_INTERNAL_SOCKET_TCPSOCKET_H_
18 
19 #if defined(WIN32) || defined(_WIN32) || defined(WIN64) || defined(_WIN64)
20 #pragma comment(lib, "Ws2_32.lib")
21 #include <winsock2.h>
22 #include <ws2tcpip.h>
23 
24 typedef int socklen_t;
25 
26 #else
27 
28 #include <unistd.h>
29 #include <netdb.h>
30 #include <sys/types.h>
31 #include <sys/socket.h>
32 #include <netinet/in.h>
33 #include <arpa/inet.h>
34 #include <sys/wait.h>
35 #include <sys/errno.h>
36 #include <sys/select.h>
37 #include <fcntl.h>
38 #include <netinet/tcp.h>
39 
40 #endif
41 
42 #include <string>
43 #include <boost/shared_ptr.hpp>
44 
45 #include "hazelcast/client/Socket.h"
46 #include "hazelcast/client/config/SocketOptions.h"
47 #include "hazelcast/client/Address.h"
48 #include "hazelcast/util/AtomicBoolean.h"
49 
50 #if defined(WIN32) || defined(_WIN32) || defined(WIN64) || defined(_WIN64)
51 #pragma warning(push)
52 #pragma warning(disable: 4251) //for dll export
53 #endif
54 
55 #if !defined(MSG_NOSIGNAL)
56 # define MSG_NOSIGNAL 0
57 #endif
58 
59 namespace hazelcast {
60  namespace client {
61  namespace internal {
62  namespace socket {
66  class HAZELCAST_API TcpSocket : public Socket {
67  public:
71  TcpSocket(int socketId);
72 
76  TcpSocket(const client::Address &address, const client::config::SocketOptions *socketOptions);
77 
81  virtual ~TcpSocket();
82 
88  int connect(int timeoutInMillis);
89 
97  int send(const void *buffer, int len, int flag = 0);
98 
106  int receive(void *buffer, int len, int flag = 0);
107 
111  int getSocketId() const;
112 
117  void close();
118 
119  Address getAddress() const;
120 
121  void setBlocking(bool blocking);
122 
123  std::auto_ptr<Address> localSocketAddress() const;
124 
125  private:
126  TcpSocket(const Socket &rhs);
127 
128  TcpSocket &operator=(const Socket &rhs);
129 
130  void throwIOException(const char *methodName, const char *prefix) const;
131 
132  void throwIOException(int error, const char *methodName, const char *prefix) const;
133 
134  void setSocketOptions(const client::config::SocketOptions &socketOptions);
135 
136  const Address configAddress;
137 
138  struct addrinfo *serverInfo;
139  int socketId;
140  util::AtomicBoolean isOpen;
141 
142  #if defined(WIN32) || defined(_WIN32) || defined(WIN64) || defined(_WIN64)
143  WSADATA wsa_data;
144  #endif
145  };
146  }
147  }
148  }
149 }
150 
151 #if defined(WIN32) || defined(_WIN32) || defined(WIN64) || defined(_WIN64)
152 #pragma warning(pop)
153 #endif
154 
155 #endif /* HAZELCAST_CLIENT_INTERNAL_SOCKET_TCPSOCKET_H_ */
IP Address.
Definition: Address.h:41
Sockets wrapper interface class.
Definition: Socket.h:34
TCP Socket options.
Definition: SocketOptions.h:32
PN (Positive-Negative) CRDT counter.
Definition: MapEntryView.h:32
c Sockets wrapper class.
Definition: TcpSocket.h:66