1 /***************************************************************************
2 * Copyright (C) 2007 by Prabakaran Thirumalai *
3 * praba_tuty@yahoo.com *
5 * This program is free software; you can redistribute it and/or modify *
6 * it under the terms of the GNU General Public License as published by *
7 * the Free Software Foundation; either version 2 of the License, or *
8 * (at your option) any later version. *
10 * This program is distributed in the hope that it will be useful, *
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13 * GNU General Public License for more details. *
15 * You should have received a copy of the GNU General Public License *
16 * along with this program; if not, write to the *
17 * Free Software Foundation, Inc., *
18 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
19 ***************************************************************************/
25 #define MSG_NOSIGNAL 0
27 TCPClient::TCPClient()
29 isConnectedFlag
=false;
32 respPkt
= new ResponsePacket();
33 pktHdr
= new PacketHeader();
36 TCPClient::~TCPClient()
38 if (isConnectedFlag
) disconnect();
42 DbRetVal
TCPClient::send(NetworkPacketType type
)
46 printError(ErrNoConnection
, "Connection lost with the peer.");
47 return ErrNoConnection
;
49 pktHdr
->packetType
= type
;
50 pktHdr
->packetLength
= 0;
52 if ((numbytes
=os::send(sockfd
, pktHdr
, sizeof(PacketHeader
), MSG_NOSIGNAL
)) == -1) {
54 printError(ErrNoConnection
, "connection not present");
55 isConnectedFlag
= false;
56 return ErrNoConnection
;
58 printError(ErrOS
, "Unable to send the packet");
59 os::closeSocket(sockfd
);
61 isConnectedFlag
= false;
62 return ErrNoConnection
;
67 DbRetVal
TCPClient::send(NetworkPacketType type
, int stmtid
)
71 printError(ErrNoConnection
, "Connection lost with the peer.");
72 return ErrNoConnection
;
74 pktHdr
->packetType
= type
;
75 pktHdr
->packetLength
= 0;
76 pktHdr
->stmtID
= stmtid
;
78 if ((numbytes
=os::send(sockfd
, pktHdr
, sizeof(PacketHeader
), MSG_NOSIGNAL
)) == -1) {
80 printError(ErrNoConnection
, "connection not present");
81 isConnectedFlag
= false;
82 os::closeSocket(sockfd
);
83 return ErrNoConnection
;
85 printError(ErrOS
, "Unable to send the packet");
86 os::closeSocket(sockfd
);
88 isConnectedFlag
= false;
89 return ErrNoConnection
;
93 DbRetVal
TCPClient::send(NetworkPacketType type
, char *buf
, int len
)
97 printError(ErrNoConnection
, "Connection lost with the peer.");
98 return ErrNoConnection
;
100 // printf("NW:TCP Send\n");
101 //void* totalBuffer = malloc(sizeof(PacketHeader)+ len);
102 //PacketHeader *hdr= new PacketHeader();
103 pktHdr
->packetType
= type
;
104 pktHdr
->packetLength
= len
;
105 pktHdr
->srcNetworkID
= networkid
;
108 //memcpy(((char*)totalBuffer) + sizeof(PacketHeader) , buf, len);
110 if ((numbytes
=os::send(sockfd
, pktHdr
, sizeof(PacketHeader
), MSG_NOSIGNAL
)) == -1) {
111 if (errno
== EPIPE
) {
112 printError(ErrNoConnection
, "connection not present");
113 isConnectedFlag
= false;
114 os::closeSocket(sockfd
);
115 return ErrNoConnection
;
117 printError(ErrOS
, "Unable to send the packet");
118 os::closeSocket(sockfd
);
120 isConnectedFlag
= false;
121 return ErrNoConnection
;
123 // printf("Sent bytes %d\n", numbytes);
124 if ((numbytes
=os::send(sockfd
, buf
, len
, MSG_NOSIGNAL
)) == -1) {
125 if (errno
== EPIPE
) {
126 printError(ErrNoConnection
, "connection not present");
127 isConnectedFlag
= false;
128 os::closeSocket(sockfd
);
129 return ErrNoConnection
;
131 printError(ErrOS
, "Unable to send the packet");
132 os::closeSocket(sockfd
);
134 isConnectedFlag
= false;
135 return ErrNoConnection
;
137 // printf("Sent bytes %d\n", numbytes);
142 DbRetVal
TCPClient::receive()
145 printDebug(DM_Network
, "NW:TCP receive\n");
148 FD_SET(sockfd
, &fdset
);
149 struct timeval timeout
;
150 timeout
.tv_sec
= Conf::config
.getNetworkResponseTimeout();
152 int ret
= os::select(sockfd
+1, &fdset
, 0, 0, &timeout
);
154 printError(ErrPeerTimeOut
,"Response timeout for peer site");
155 os::closeSocket(sockfd
);
157 isConnectedFlag
= false;
158 return ErrPeerTimeOut
;
160 socklen_t len
= sizeof(struct sockaddr
);
161 printDebug(DM_Network
, "Sizeof response packet %d\n", sizeof(ResponsePacket
));
162 int numbytes
= os::recv(sockfd
, respPkt
, sizeof(ResponsePacket
), 0);
163 printDebug(DM_Network
, " NUMBYTES packet %d\n", numbytes
);
166 printError(ErrOS
, "Unable to receive response from peer");
167 os::closeSocket(sockfd
);
169 isConnectedFlag
= false;
170 return ErrNoConnection
;
172 return respPkt
->errRetVal
;
175 DbRetVal
TCPClient::connect()
177 printDebug(DM_Network
, "NW:TCP connect %s %d %d\n", hostName
, port
, networkid
);
178 //TODO::send endian to the peer site
179 //do not do endian conversion here. it will be done at the server side
182 iResult
= WSAStartup(MAKEWORD(2, 2), &wsaData
);
184 printf("WSAStartup failed: %d\n", iResult
);
188 isConnectedFlag
= false;
191 if ((he
=gethostbyname(hostName
)) == NULL
) { // get the host info
192 printError(ErrOS
,"Unable to get the peer host name");
195 if ((sockfd
= socket(AF_INET
, SOCK_STREAM
, 0)) == -1) {
196 printError(ErrOS
,"Unable to create socket to peer host name");
199 srvAddr
.sin_family
= AF_INET
; // host byte order
200 srvAddr
.sin_port
= htons(port
); // short, network byte order
201 srvAddr
.sin_addr
= *((struct in_addr
*)he
->h_addr
);
202 memset(&(srvAddr
.sin_zero
), '\0', 8); // zero the rest of the struct
203 if (::connect(sockfd
, (struct sockaddr
*) & srvAddr
, sizeof(struct sockaddr
))
206 printError(ErrOS
, "Unable to connect to peer site");
209 // printf("NW:TCP connecting\n");
212 FD_SET(sockfd
, &fdset
);
213 struct timeval timeout
;
214 timeout
.tv_sec
= Conf::config
.getNetworkConnectTimeout();
216 int ret
= os::select(sockfd
+1, &fdset
, 0, 0, &timeout
);
218 printError(ErrPeerTimeOut
,"Response timeout for peer site");
219 return ErrPeerTimeOut
;
222 socklen_t len
= sizeof(struct sockaddr
);
223 numbytes
= os::recv(sockfd
, &response
, 4, 0);
226 printError(ErrOS
, "Unable to receive response from peer");
229 // if (response != 1) return ErrPeerResponse;
231 //packet header information
232 pktHdr
->srcNetworkID
= networkid
;
234 isConnectedFlag
= true;
238 DbRetVal
TCPClient::disconnect()
240 if (isConnectedFlag
) {
241 pktHdr
->packetType
= SQL_NW_PKT_DISCONNECT
;
242 pktHdr
->packetLength
= 0;
244 if ((numbytes
=os::send(sockfd
, pktHdr
, sizeof(PacketHeader
), MSG_NOSIGNAL
)) == -1) {
245 if (errno
== EPIPE
) {
246 printError(ErrNoConnection
, "connection not present");
247 os::closeSocket(sockfd
);
251 isConnectedFlag
= false;
252 return ErrNoConnection
;
254 printError(ErrOS
, "Unable to send the packet");
255 os::closeSocket(sockfd
);
260 isConnectedFlag
= false;
261 return ErrNoConnection
;
263 printDebug(DM_Network
,"Sent bytes %d\n", numbytes
);
264 DbRetVal rv
= receive();
265 isConnectedFlag
= false;
266 os::closeSocket(sockfd
);