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 ***************************************************************************/
23 TCPClient::~TCPClient()
25 if (isConnectedFlag
) disconnect();
27 DbRetVal
TCPClient::send(NetworkPacketType type
, char *buf
, int len
)
30 printf("NW:TCP Send\n");
31 void* totalBuffer
= malloc(sizeof(PacketHeader
)+ len
);
32 PacketHeader
*hdr
= new PacketHeader();
33 hdr
->packetType
= type
;
34 hdr
->packetLength
= len
;
35 hdr
->srcNetworkID
= networkid
;
37 memcpy(((char*)totalBuffer
) + sizeof(PacketHeader
) , buf
, len
);
39 if ((numbytes
=os::send(sockfd
, hdr
, sizeof(PacketHeader
), 0)) == -1) {
40 printError(ErrOS
, "Unable to send the packet\n");
43 printf("Sent bytes %d\n", numbytes
);
44 if ((numbytes
=os::send(sockfd
, buf
, len
, 0)) == -1) {
45 printError(ErrOS
, "Unable to send the packet\n");
48 printf("Sent bytes %d\n", numbytes
);
52 DbRetVal
TCPClient::receive()
55 printf("NW:TCP receive\n");
58 FD_SET(sockfd
, &fdset
);
59 struct timeval timeout
;
60 timeout
.tv_sec
= Conf::config
.getNetworkResponseTimeout();
62 int ret
= os::select(sockfd
+1, &fdset
, 0, 0, &timeout
);
64 printError(ErrPeerTimeOut
,"Response timeout for peer site\n");
65 return ErrPeerTimeOut
;
69 socklen_t len
= sizeof(struct sockaddr
);
70 int numbytes
= os::recv(sockfd
, &response
, 4, 0);
73 printf("Unable to receive response from peer\n");
76 //printf("NW:UDP receive\n");
77 if (response
!= 1) rv
= ErrPeerResponse
;
80 DbRetVal
TCPClient::connect()
82 //printf("NW:TCP connect %s %d %d\n", hostName, port, networkid);
83 //TODO::send endian to the peer site
84 //do not do endian conversion here. it will be done at the server side
85 isConnectedFlag
= false;
88 if ((he
=gethostbyname(hostName
)) == NULL
) { // get the host info
89 printError(ErrOS
,"Unable to get the peer host name\n");
92 if ((sockfd
= socket(AF_INET
, SOCK_STREAM
, 0)) == -1) {
93 printError(ErrOS
,"Unable to create socket to peer host name\n");
96 srvAddr
.sin_family
= AF_INET
; // host byte order
97 srvAddr
.sin_port
= htons(port
); // short, network byte order
98 srvAddr
.sin_addr
= *((struct in_addr
*)he
->h_addr
);
99 memset(&(srvAddr
.sin_zero
), '\0', 8); // zero the rest of the struct
100 if (::connect(sockfd
, (struct sockaddr
*) & srvAddr
, sizeof(struct sockaddr
))
103 printError(ErrOS
, "Unable to connect to peer site\n");
106 printf("NW:TCP connecting\n");
109 FD_SET(sockfd
, &fdset
);
110 struct timeval timeout
;
111 timeout
.tv_sec
= Conf::config
.getNetworkConnectTimeout();
113 int ret
= os::select(sockfd
+1, &fdset
, 0, 0, &timeout
);
115 printError(ErrPeerTimeOut
,"Response timeout for peer site\n");
116 return ErrPeerTimeOut
;
119 socklen_t len
= sizeof(struct sockaddr
);
120 numbytes
= os::recv(sockfd
, &response
, 4, 0);
123 printf("Unable to receive response from peer\n");
126 printf("Response from peer site is %d\n", response
);
127 if (response
!= 1) return ErrPeerResponse
;
128 isConnectedFlag
= true;
132 DbRetVal
TCPClient::disconnect()
134 if (isConnectedFlag
) {
135 printf("NW:TCP disconnect %s %d\n", hostName
, port
);
136 PacketHeader
*hdr
= new PacketHeader();
137 hdr
->packetType
= NW_PKT_DISCONNECT
;
138 hdr
->packetLength
= 0;
142 if ((numbytes
=os::send(sockfd
, hdr
, sizeof(PacketHeader
), 0))
144 printError(ErrOS
, "Unable to send the packet\n");
146 printf("Sent bytes %d\n", numbytes
);
150 isConnectedFlag
= false;