windows porting changesw
[csql.git] / src / network / TCPClient.cxx
blob87c9a6679cc1e1819032f2426887fa290121aa1d
1 /***************************************************************************
2 * Copyright (C) 2007 by Prabakaran Thirumalai *
3 * praba_tuty@yahoo.com *
4 * *
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. *
9 * *
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. *
14 * *
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 ***************************************************************************/
20 #include <os.h>
21 #include <CSql.h>
22 #include <Network.h>
23 #include <Parser.h>
24 #if defined(SOLARIS)
25 #define MSG_NOSIGNAL 0
26 #endif
27 TCPClient::TCPClient()
29 isConnectedFlag =false;
30 cacheClient = false;
31 sockfd = -1;
32 respPkt = new ResponsePacket();
33 pktHdr = new PacketHeader();
36 TCPClient::~TCPClient()
38 if (isConnectedFlag) disconnect();
39 delete respPkt;
40 delete pktHdr;
42 DbRetVal TCPClient::send(NetworkPacketType type)
44 DbRetVal rv = OK;
45 if (sockfd == -1) {
46 printError(ErrNoConnection, "Connection lost with the peer.");
47 return ErrNoConnection;
49 pktHdr->packetType = type;
50 pktHdr->packetLength = 0;
51 int numbytes=0;
52 if ((numbytes=os::send(sockfd, pktHdr, sizeof(PacketHeader), MSG_NOSIGNAL)) == -1) {
53 if (errno == EPIPE) {
54 printError(ErrNoConnection, "connection not present");
55 isConnectedFlag = false;
56 return ErrNoConnection;
58 printError(ErrOS, "Unable to send the packet");
59 os::close(sockfd);
60 sockfd = -1;
61 isConnectedFlag = false;
62 return ErrNoConnection;
64 return rv;
67 DbRetVal TCPClient::send(NetworkPacketType type, int stmtid)
69 DbRetVal rv = OK;
70 if (sockfd == -1) {
71 printError(ErrNoConnection, "Connection lost with the peer.");
72 return ErrNoConnection;
74 pktHdr->packetType = type;
75 pktHdr->packetLength = 0;
76 pktHdr->stmtID = stmtid;
77 int numbytes=0;
78 if ((numbytes=os::send(sockfd, pktHdr, sizeof(PacketHeader), MSG_NOSIGNAL)) == -1) {
79 if (errno == EPIPE) {
80 printError(ErrNoConnection, "connection not present");
81 isConnectedFlag = false;
82 return ErrNoConnection;
84 printError(ErrOS, "Unable to send the packet");
85 close(sockfd);
86 sockfd = -1;
87 isConnectedFlag = false;
88 return ErrNoConnection;
90 return rv;
92 DbRetVal TCPClient::send(NetworkPacketType type, char *buf, int len)
94 DbRetVal rv = OK;
95 if (sockfd == -1) {
96 printError(ErrNoConnection, "Connection lost with the peer.");
97 return ErrNoConnection;
99 // printf("NW:TCP Send\n");
100 //void* totalBuffer = malloc(sizeof(PacketHeader)+ len);
101 //PacketHeader *hdr= new PacketHeader();
102 pktHdr->packetType = type;
103 pktHdr->packetLength = len;
104 pktHdr->srcNetworkID = networkid;
105 pktHdr->version = 1;
106 pktHdr->stmtID = 0;
107 //memcpy(((char*)totalBuffer) + sizeof(PacketHeader) , buf, len);
108 int numbytes=0;
109 if ((numbytes=os::send(sockfd, pktHdr, sizeof(PacketHeader), MSG_NOSIGNAL)) == -1) {
110 if (errno == EPIPE) {
111 printError(ErrNoConnection, "connection not present");
112 isConnectedFlag = false;
113 return ErrNoConnection;
115 printError(ErrOS, "Unable to send the packet");
116 close(sockfd);
117 sockfd = -1;
118 isConnectedFlag = false;
119 return ErrNoConnection;
121 // printf("Sent bytes %d\n", numbytes);
122 if ((numbytes=os::send(sockfd, buf, len, MSG_NOSIGNAL)) == -1) {
123 if (errno == EPIPE) {
124 printError(ErrNoConnection, "connection not present");
125 isConnectedFlag = false;
126 return ErrNoConnection;
128 printError(ErrOS, "Unable to send the packet");
129 close(sockfd);
130 sockfd = -1;
131 isConnectedFlag = false;
132 return ErrNoConnection;
134 // printf("Sent bytes %d\n", numbytes);
135 //free(totalBuffer);
136 //delete hdr;
137 return rv;
139 DbRetVal TCPClient::receive()
141 DbRetVal rv = OK;
142 // printf("NW:TCP receive\n");
143 fd_set fdset;
144 FD_ZERO(&fdset);
145 FD_SET(sockfd, &fdset);
146 struct timeval timeout;
147 timeout.tv_sec = Conf::config.getNetworkResponseTimeout();
148 timeout.tv_usec = 0;
149 int ret = os::select(sockfd+1, &fdset, 0, 0, &timeout);
150 if (ret <= 0) {
151 printError(ErrPeerTimeOut,"Response timeout for peer site");
152 os::close(sockfd);
153 sockfd = -1;
154 isConnectedFlag = false;
155 return ErrPeerTimeOut;
157 socklen_t len = sizeof(struct sockaddr);
158 int numbytes = os::recv(sockfd, respPkt, sizeof(ResponsePacket), 0);
159 if (numbytes == -1)
161 printError(ErrOS, "Unable to receive response from peer");
162 os::close(sockfd);
163 sockfd = -1;
164 isConnectedFlag = false;
165 return ErrNoConnection;
167 return respPkt->errRetVal;
170 DbRetVal TCPClient::connect()
172 //printf("NW:TCP connect %s %d %d\n", hostName, port, networkid);
173 //TODO::send endian to the peer site
174 //do not do endian conversion here. it will be done at the server side
175 isConnectedFlag = false;
176 struct hostent *he;
177 int numbytes;
178 if ((he=gethostbyname(hostName)) == NULL) { // get the host info
179 printError(ErrOS,"Unable to get the peer host name");
180 return ErrOS;
182 if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) == -1) {
183 printError(ErrOS,"Unable to create socket to peer host name");
184 return ErrOS;
186 srvAddr.sin_family = AF_INET; // host byte order
187 srvAddr.sin_port = htons(port); // short, network byte order
188 srvAddr.sin_addr = *((struct in_addr *)he->h_addr);
189 memset(&(srvAddr.sin_zero), '\0', 8); // zero the rest of the struct
190 if (::connect(sockfd, (struct sockaddr*) & srvAddr, sizeof(struct sockaddr))
191 == -1)
193 printError(ErrOS, "Unable to connect to peer site");
194 return ErrOS;
196 // printf("NW:TCP connecting\n");
197 fd_set fdset;
198 FD_ZERO(&fdset);
199 FD_SET(sockfd, &fdset);
200 struct timeval timeout;
201 timeout.tv_sec = Conf::config.getNetworkConnectTimeout();
202 timeout.tv_usec = 0;
203 int ret = os::select(sockfd+1, &fdset, 0, 0, &timeout);
204 if (ret <= 0) {
205 printError(ErrPeerTimeOut,"Response timeout for peer site");
206 return ErrPeerTimeOut;
208 int response=0;
209 socklen_t len = sizeof(struct sockaddr);
210 numbytes = os::recv(sockfd, &response, 4, 0);
211 if (numbytes !=4)
213 printError(ErrOS, "Unable to receive response from peer");
214 return ErrOS;
216 // printf("Response from peer site is %d\n", response);
217 // if (response != 1) return ErrPeerResponse;
219 //packet header information
220 pktHdr->srcNetworkID = networkid;
221 pktHdr->version = 1;
222 isConnectedFlag = true;
223 return OK;
226 DbRetVal TCPClient::disconnect()
228 if (isConnectedFlag) {
229 // printf("NW:TCP disconnect %s %d\n", hostName, port);
230 pktHdr->packetType = SQL_NW_PKT_DISCONNECT;
231 pktHdr->packetLength = 0;
232 int numbytes=0;
233 if ((numbytes=os::send(sockfd, pktHdr, sizeof(PacketHeader), MSG_NOSIGNAL)) == -1) {
234 if (errno == EPIPE) {
235 printError(ErrNoConnection, "connection not present");
236 isConnectedFlag = false;
237 return ErrNoConnection;
239 printError(ErrOS, "Unable to send the packet");
240 close(sockfd);
241 sockfd = -1;
242 isConnectedFlag = false;
243 return ErrNoConnection;
244 } else {
245 // printf("Sent bytes %d\n", numbytes);
246 DbRetVal rv = receive();
247 isConnectedFlag = false;
248 close(sockfd);
249 sockfd = -1;
252 return OK;