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 ***************************************************************************/
24 UDPClient::~UDPClient()
26 if (isConnectedFlag
) disconnect();
28 DbRetVal
UDPClient::send(NetworkPacketType type
)
30 return send(type
, NULL
, 0);
32 DbRetVal
UDPClient::send(NetworkPacketType type
, int stmtid
)
35 return send(type
, NULL
, 0);
37 DbRetVal
UDPClient::send(NetworkPacketType type
, char *buf
, int len
)
40 //printf("NW:UDP Send\n");
41 void* totalBuffer
= malloc(sizeof(PacketHeader
)+ len
);
42 //PacketHeader *hdr= (PacketHeader*) totalBuffer;
43 PacketHeader
*hdr
= new PacketHeader();
44 hdr
->packetType
= type
;
45 hdr
->packetLength
= len
;
46 hdr
->srcNetworkID
= networkid
;
48 memcpy(((char*)totalBuffer
) + sizeof(PacketHeader
) , buf
, len
);
51 int numbytes =sendto(sockfd, totalBuffer, sizeof(PacketHeader)+ len, 0,
52 (struct sockaddr *)&srvAddr, sizeof(struct sockaddr));
55 printError(ErrOS,"Unable to send the packet\n");
60 if ((numbytes
=sendto(sockfd
, (const char*)hdr
, sizeof(PacketHeader
), 0,
61 (struct sockaddr
*)&srvAddr
, sizeof(struct sockaddr
))) == -1) {
62 printError(ErrOS
, "Unable to send the packet\n");
65 //printf("Sent bytes %d\n", numbytes);
66 if ((numbytes
=sendto(sockfd
, buf
, len
, 0,
67 (struct sockaddr
*)&srvAddr
, sizeof(struct sockaddr
))) == -1) {
68 printError(ErrOS
, "Unable to send the packet\n");
71 //printf("Sent bytes %d\n", numbytes);
76 DbRetVal
UDPClient::receive()
78 //TODO:: resoibse timeout during socket read
80 fd_set fdset
; //TODO::Move it to UDPClient class
82 FD_SET(sockfd
, &fdset
);
83 struct timeval timeout
;
84 timeout
.tv_sec
= Conf::config
.getNetworkResponseTimeout();
86 int ret
= os::select(sockfd
+1, &fdset
, 0, 0, &timeout
);
88 printError(ErrPeerTimeOut
,"Response timeout for peer site\n");
89 return ErrPeerTimeOut
;
93 socklen_t len
= sizeof(struct sockaddr
);
94 int numbytes
= recvfrom(sockfd
, (char*)&response
, 4, 0,
95 (struct sockaddr
*)&fromAddr
, &len
);
98 printf("Unable to receive response from peer\n");
101 //printf("NW:UDP receive\n");
102 if (response
!= 1) rv
= ErrPeerResponse
;
103 if(srvAddr
.sin_addr
.s_addr
!= fromAddr
.sin_addr
.s_addr
)
105 printf("Packet received from unknown source\n");
106 rv
= ErrPeerResponse
;
108 //printf("Response rv %d\n", rv);
111 DbRetVal
UDPClient::connect()
113 printf("NW:UDP connect %s %d %d\n", hostName
, port
, networkid
);
115 //TODO::send endian to the peer site
116 //do not do endian conversion here. it will be done at the server side
118 isConnectedFlag
= false;
121 if ((he
=gethostbyname(hostName
)) == NULL
) { // get the host info
122 printError(ErrOS
,"Unable to get the peer host name\n");
125 if ((sockfd
= socket(AF_INET
, SOCK_DGRAM
, 0)) == -1) {
126 printError(ErrOS
,"Unable to create socket to peer host name\n");
129 srvAddr
.sin_family
= AF_INET
; // host byte order
130 srvAddr
.sin_port
= htons(port
); // short, network byte order
131 srvAddr
.sin_addr
= *((struct in_addr
*)he
->h_addr
);
132 memset(&(srvAddr
.sin_zero
), '\0', 8); // zero the rest of the struct
133 printf("NW:UDP connecting\n");
134 PacketHeader
*hdr
= new PacketHeader();
135 hdr
->packetType
= NW_PKT_CONNECT
;
136 hdr
->packetLength
= 0;
139 if ((numbytes
=sendto(sockfd
, (const char*) hdr
, sizeof(PacketHeader
), 0,
140 (struct sockaddr
*)&srvAddr
, sizeof(struct sockaddr
))) == -1) {
141 printError(ErrOS
, "Unable to send the packet\n");
144 printf("Sent bytes %d\n", numbytes
);
148 FD_SET(sockfd
, &fdset
);
149 struct timeval timeout
;
150 timeout
.tv_sec
= Conf::config
.getNetworkConnectTimeout();
152 int ret
= os::select(sockfd
+1, &fdset
, 0, 0, &timeout
);
154 printError(ErrPeerTimeOut
,"Response timeout for peer site\n");
155 return ErrPeerTimeOut
;
159 socklen_t len
= sizeof(struct sockaddr
);
160 numbytes
= recvfrom(sockfd
, (char*)&response
, 4, 0,
161 (struct sockaddr
*)&fromAddr
, &len
);
164 printf("Unable to receive response from peer\n");
167 if (response
!= 1) return ErrPeerResponse
;
168 isConnectedFlag
= true;
169 printf("NW:UDP connected\n");
173 DbRetVal
UDPClient::disconnect()
176 printf("NW:UDP disconnect\n");
178 isConnectedFlag
= false;