changes for windows jdbc driverw
[csql.git] / src / network / TCPClient.cxx
blob4e507f82ee3aa262593510c8bee8fee0ec6fa991
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::closeSocket(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 os::closeSocket(sockfd);
83 return ErrNoConnection;
85 printError(ErrOS, "Unable to send the packet");
86 os::closeSocket(sockfd);
87 sockfd = -1;
88 isConnectedFlag = false;
89 return ErrNoConnection;
91 return rv;
93 DbRetVal TCPClient::send(NetworkPacketType type, char *buf, int len)
95 DbRetVal rv = OK;
96 if (sockfd == -1) {
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;
106 pktHdr->version = 1;
107 pktHdr->stmtID = 0;
108 //memcpy(((char*)totalBuffer) + sizeof(PacketHeader) , buf, len);
109 int numbytes=0;
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);
119 sockfd = -1;
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);
133 sockfd = -1;
134 isConnectedFlag = false;
135 return ErrNoConnection;
137 // printf("Sent bytes %d\n", numbytes);
138 //free(totalBuffer);
139 //delete hdr;
140 return rv;
142 DbRetVal TCPClient::receive()
144 DbRetVal rv = OK;
145 printDebug(DM_Network, "NW:TCP receive\n");
146 fd_set fdset;
147 FD_ZERO(&fdset);
148 FD_SET(sockfd, &fdset);
149 struct timeval timeout;
150 timeout.tv_sec = Conf::config.getNetworkResponseTimeout();
151 timeout.tv_usec = 0;
152 int ret = os::select(sockfd+1, &fdset, 0, 0, &timeout);
153 if (ret <= 0) {
154 printError(ErrPeerTimeOut,"Response timeout for peer site");
155 os::closeSocket(sockfd);
156 sockfd = -1;
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);
164 if (numbytes == -1)
166 printError(ErrOS, "Unable to receive response from peer");
167 os::closeSocket(sockfd);
168 sockfd = -1;
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
180 int iResult=0;
181 iResult = WSAStartup(MAKEWORD(2, 2), &wsaData);
182 if (iResult != 0) {
183 printf("WSAStartup failed: %d\n", iResult);
184 return ErrSysInit;
186 isConnectedFlag = false;
187 struct hostent *he;
188 int numbytes;
189 if ((he=gethostbyname(hostName)) == NULL) { // get the host info
190 printError(ErrOS,"Unable to get the peer host name");
191 return ErrOS;
193 if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) == -1) {
194 printError(ErrOS,"Unable to create socket to peer host name");
195 return ErrOS;
197 srvAddr.sin_family = AF_INET; // host byte order
198 srvAddr.sin_port = htons(port); // short, network byte order
199 srvAddr.sin_addr = *((struct in_addr *)he->h_addr);
200 memset(&(srvAddr.sin_zero), '\0', 8); // zero the rest of the struct
201 if (::connect(sockfd, (struct sockaddr*) & srvAddr, sizeof(struct sockaddr))
202 == -1)
204 printError(ErrOS, "Unable to connect to peer site");
205 return ErrOS;
207 // printf("NW:TCP connecting\n");
208 fd_set fdset;
209 FD_ZERO(&fdset);
210 FD_SET(sockfd, &fdset);
211 struct timeval timeout;
212 timeout.tv_sec = Conf::config.getNetworkConnectTimeout();
213 timeout.tv_usec = 0;
214 int ret = os::select(sockfd+1, &fdset, 0, 0, &timeout);
215 if (ret <= 0) {
216 printError(ErrPeerTimeOut,"Response timeout for peer site");
217 return ErrPeerTimeOut;
219 int response=0;
220 socklen_t len = sizeof(struct sockaddr);
221 numbytes = os::recv(sockfd, &response, 4, 0);
222 if (numbytes !=4)
224 printError(ErrOS, "Unable to receive response from peer");
225 return ErrOS;
227 // if (response != 1) return ErrPeerResponse;
229 //packet header information
230 pktHdr->srcNetworkID = networkid;
231 pktHdr->version = 1;
232 isConnectedFlag = true;
233 return OK;
236 DbRetVal TCPClient::disconnect()
238 if (isConnectedFlag) {
239 pktHdr->packetType = SQL_NW_PKT_DISCONNECT;
240 pktHdr->packetLength = 0;
241 int numbytes=0;
242 if ((numbytes=os::send(sockfd, pktHdr, sizeof(PacketHeader), MSG_NOSIGNAL)) == -1) {
243 if (errno == EPIPE) {
244 printError(ErrNoConnection, "connection not present");
245 os::closeSocket(sockfd);
246 WSACleanup();
247 isConnectedFlag = false;
248 return ErrNoConnection;
250 printError(ErrOS, "Unable to send the packet");
251 os::closeSocket(sockfd);
252 WSACleanup();
253 sockfd = -1;
254 isConnectedFlag = false;
255 return ErrNoConnection;
256 } else {
257 printDebug(DM_Network,"Sent bytes %d\n", numbytes);
258 DbRetVal rv = receive();
259 isConnectedFlag = false;
260 os::closeSocket(sockfd);
261 WSACleanup();
262 sockfd = -1;
265 return OK;