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 UDPClient::~UDPClient()
25 if (isConnectedFlag
) disconnect();
27 DbRetVal
UDPClient::send(NetworkPacketType type
, char *buf
, int len
)
30 //printf("NW:UDP Send\n");
31 void* totalBuffer
= malloc(sizeof(PacketHeader
)+ len
);
32 //PacketHeader *hdr= (PacketHeader*) totalBuffer;
33 PacketHeader
*hdr
= new PacketHeader();
34 hdr
->packetType
= type
;
35 hdr
->packetLength
= len
;
36 hdr
->srcNetworkID
= networkid
;
38 memcpy(((char*)totalBuffer
) + sizeof(PacketHeader
) , buf
, len
);
41 int numbytes =sendto(sockfd, totalBuffer, sizeof(PacketHeader)+ len, 0,
42 (struct sockaddr *)&srvAddr, sizeof(struct sockaddr));
45 printError(ErrOS,"Unable to send the packet\n");
50 if ((numbytes
=sendto(sockfd
, hdr
, sizeof(PacketHeader
), 0,
51 (struct sockaddr
*)&srvAddr
, sizeof(struct sockaddr
))) == -1) {
52 printError(ErrOS
, "Unable to send the packet\n");
55 //printf("Sent bytes %d\n", numbytes);
56 if ((numbytes
=sendto(sockfd
, buf
, len
, 0,
57 (struct sockaddr
*)&srvAddr
, sizeof(struct sockaddr
))) == -1) {
58 printError(ErrOS
, "Unable to send the packet\n");
61 //printf("Sent bytes %d\n", numbytes);
66 DbRetVal
UDPClient::receive()
68 //TODO:: resoibse timeout during socket read
70 fd_set fdset
; //TODO::Move it to UDPClient class
72 FD_SET(sockfd
, &fdset
);
73 struct timeval timeout
;
74 timeout
.tv_sec
= Conf::config
.getNetworkResponseTimeout();
76 int ret
= os::select(sockfd
+1, &fdset
, 0, 0, &timeout
);
78 printError(ErrPeerTimeOut
,"Response timeout for peer site\n");
79 return ErrPeerTimeOut
;
83 socklen_t len
= sizeof(struct sockaddr
);
84 int numbytes
= recvfrom(sockfd
, &response
, 4, 0,
85 (struct sockaddr
*)&fromAddr
, &len
);
88 printf("Unable to receive response from peer\n");
91 //printf("NW:UDP receive\n");
92 if (response
!= 1) rv
= ErrPeerResponse
;
93 if(srvAddr
.sin_addr
.s_addr
!= fromAddr
.sin_addr
.s_addr
)
95 printf("Packet received from unknown source\n");
98 //printf("Response rv %d\n", rv);
101 DbRetVal
UDPClient::connect()
103 printf("NW:UDP connect %s %d %d\n", hostName
, port
, networkid
);
105 //TODO::send endian to the peer site
106 //do not do endian conversion here. it will be done at the server side
108 isConnectedFlag
= false;
111 if ((he
=gethostbyname(hostName
)) == NULL
) { // get the host info
112 printError(ErrOS
,"Unable to get the peer host name\n");
115 if ((sockfd
= socket(AF_INET
, SOCK_DGRAM
, 0)) == -1) {
116 printError(ErrOS
,"Unable to create socket to peer host name\n");
119 srvAddr
.sin_family
= AF_INET
; // host byte order
120 srvAddr
.sin_port
= htons(port
); // short, network byte order
121 srvAddr
.sin_addr
= *((struct in_addr
*)he
->h_addr
);
122 memset(&(srvAddr
.sin_zero
), '\0', 8); // zero the rest of the struct
123 printf("NW:UDP connecting\n");
124 PacketHeader
*hdr
= new PacketHeader();
125 hdr
->packetType
= NW_PKT_CONNECT
;
126 hdr
->packetLength
= 0;
129 if ((numbytes
=sendto(sockfd
, hdr
, sizeof(PacketHeader
), 0,
130 (struct sockaddr
*)&srvAddr
, sizeof(struct sockaddr
))) == -1) {
131 printError(ErrOS
, "Unable to send the packet\n");
134 printf("Sent bytes %d\n", numbytes
);
138 FD_SET(sockfd
, &fdset
);
139 struct timeval timeout
;
140 timeout
.tv_sec
= Conf::config
.getNetworkConnectTimeout();
142 int ret
= os::select(sockfd
+1, &fdset
, 0, 0, &timeout
);
144 printError(ErrPeerTimeOut
,"Response timeout for peer site\n");
145 return ErrPeerTimeOut
;
149 socklen_t len
= sizeof(struct sockaddr
);
150 numbytes
= recvfrom(sockfd
, &response
, 4, 0,
151 (struct sockaddr
*)&fromAddr
, &len
);
154 printf("Unable to receive response from peer\n");
157 if (response
!= 1) return ErrPeerResponse
;
158 isConnectedFlag
= true;
159 printf("NW:UDP connected\n");
163 DbRetVal
UDPClient::disconnect()
166 printf("NW:UDP disconnect\n");
168 isConnectedFlag
= false;