Hazelcast C++ Client
TcpSocket.h
1 /*
2  * Copyright (c) 2008-2017, 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 #include "hazelcast/client/Socket.h"
20 
21 #include "hazelcast/client/Address.h"
22 #include "hazelcast/util/AtomicBoolean.h"
23 #include <string>
24 
25 #if defined(WIN32) || defined(_WIN32) || defined(WIN64) || defined(_WIN64)
26 #pragma warning(push)
27 #pragma warning(disable: 4251) //for dll export
28 #endif
29 
30 #if defined(WIN32) || defined(_WIN32) || defined(WIN64) || defined(_WIN64)
31 #pragma comment(lib, "Ws2_32.lib")
32 #include <winsock2.h>
33 #include <ws2tcpip.h>
34 
35 typedef int socklen_t;
36 
37 #else
38 
39 #include <unistd.h>
40 #include <netdb.h>
41 #include <sys/types.h>
42 #include <sys/socket.h>
43 #include <netinet/in.h>
44 #include <arpa/inet.h>
45 #include <sys/wait.h>
46 #include <sys/errno.h>
47 #include <sys/select.h>
48 #include <fcntl.h>
49 
50 #endif
51 
52 #if !defined(MSG_NOSIGNAL)
53 # define MSG_NOSIGNAL 0
54 #endif
55 
56 namespace hazelcast {
57  namespace client {
58  namespace internal {
59  namespace socket {
63  class HAZELCAST_API TcpSocket : public Socket {
64  public:
68  TcpSocket(int socketId);
69 
73  TcpSocket(const client::Address &address);
74 
78  virtual ~TcpSocket();
79 
85  int connect(int timeoutInMillis);
86 
93  int send(const void *buffer, int len) const;
94 
102  int receive(void *buffer, int len, int flag = 0) const;
103 
107  int getSocketId() const;
108 
112  void setRemoteEndpoint(const client::Address &address);
113 
117  const client::Address &getRemoteEndpoint() const;
118 
123  void close();
124 
125  client::Address getAddress() const;
126 
127  void setBlocking(bool blocking);
128 
129  private:
130  TcpSocket(const Socket &rhs);
131 
132  TcpSocket &operator=(const Socket &rhs);
133 
134  void throwIOException(const char *methodName, const char *prefix) const;
135 
136  void throwIOException(int error, const char *methodName, const char *prefix) const;
137 
138  client::Address remoteEndpoint;
139 
140  const client::Address configAddress;
141 
142  struct addrinfo *serverInfo;
143  int socketId;
144  util::AtomicBoolean isOpen;
145 
146  #if defined(WIN32) || defined(_WIN32) || defined(WIN64) || defined(_WIN64)
147  WSADATA wsa_data;
148  #endif
149  };
150  }
151  }
152  }
153 }
154 
155 #if defined(WIN32) || defined(_WIN32) || defined(WIN64) || defined(_WIN64)
156 #pragma warning(pop)
157 #endif
158 
159 #endif /* HAZELCAST_CLIENT_INTERNAL_SOCKET_TCPSOCKET_H_ */
IP Address.
Definition: Address.h:36
Sockets wrapper interface class.
Definition: Socket.h:31
Definition: MapEntryView.h:32
c Sockets wrapper class.
Definition: TcpSocket.h:63