00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020 #include <CSql.h>
00021 #include <Network.h>
00022
00023 UDPClient::~UDPClient()
00024 {
00025 if (isConnectedFlag) disconnect();
00026 }
00027 DbRetVal UDPClient::send(NetworkPacketType type, char *buf, int len)
00028 {
00029 DbRetVal rv = OK;
00030
00031 void* totalBuffer = malloc(sizeof(PacketHeader)+ len);
00032
00033 PacketHeader *hdr= new PacketHeader();
00034 hdr->packetType = type;
00035 hdr->packetLength = len;
00036 hdr->srcNetworkID = networkid;
00037 hdr->version = 1;
00038 memcpy(((char*)totalBuffer) + sizeof(PacketHeader) , buf, len);
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049 int numbytes=0;
00050 if ((numbytes=sendto(sockfd, hdr, sizeof(PacketHeader), 0,
00051 (struct sockaddr *)&srvAddr, sizeof(struct sockaddr))) == -1) {
00052 printError(ErrOS, "Unable to send the packet\n");
00053 return ErrOS;
00054 }
00055
00056 if ((numbytes=sendto(sockfd, buf, len, 0,
00057 (struct sockaddr *)&srvAddr, sizeof(struct sockaddr))) == -1) {
00058 printError(ErrOS, "Unable to send the packet\n");
00059 return ErrOS;
00060 }
00061
00062 free(totalBuffer);
00063 return rv;
00064
00065 }
00066 DbRetVal UDPClient::receive()
00067 {
00068
00069 DbRetVal rv = OK;
00070 fd_set fdset;
00071 FD_ZERO(&fdset);
00072 FD_SET(sockfd, &fdset);
00073 struct timeval timeout;
00074 timeout.tv_sec = Conf::config.getNetworkResponseTimeout();
00075 timeout.tv_usec = 0;
00076 int ret = os::select(sockfd+1, &fdset, 0, 0, &timeout);
00077 if (ret <= 0) {
00078 printError(ErrPeerTimeOut,"Response timeout for peer site\n");
00079 return ErrPeerTimeOut;
00080 }
00081
00082 int response =0;
00083 socklen_t len = sizeof(struct sockaddr);
00084 int numbytes = recvfrom(sockfd, &response, 4, 0,
00085 (struct sockaddr *)&fromAddr, &len);
00086 if (numbytes !=4)
00087 {
00088 printf("Unable to receive response from peer\n");
00089 return ErrOS;
00090 }
00091
00092 if (response != 1) rv = ErrPeerResponse;
00093 if(srvAddr.sin_addr.s_addr != fromAddr.sin_addr.s_addr)
00094 {
00095 printf("Packet received from unknown source\n");
00096 rv = ErrPeerResponse;
00097 }
00098
00099 return rv;
00100 }
00101 DbRetVal UDPClient::connect()
00102 {
00103 printf("NW:UDP connect %s %d %d\n", hostName, port, networkid);
00104
00105
00106
00107 DbRetVal rv = OK;
00108 isConnectedFlag = false;
00109 struct hostent *he;
00110 int numbytes;
00111 if ((he=gethostbyname(hostName)) == NULL) {
00112 printError(ErrOS,"Unable to get the peer host name\n");
00113 return ErrOS;
00114 }
00115 if ((sockfd = socket(AF_INET, SOCK_DGRAM, 0)) == -1) {
00116 printError(ErrOS,"Unable to create socket to peer host name\n");
00117 return ErrOS;
00118 }
00119 srvAddr.sin_family = AF_INET;
00120 srvAddr.sin_port = htons(port);
00121 srvAddr.sin_addr = *((struct in_addr *)he->h_addr);
00122 memset(&(srvAddr.sin_zero), '\0', 8);
00123 printf("NW:UDP connecting\n");
00124 PacketHeader *hdr= new PacketHeader();
00125 hdr->packetType = NW_PKT_CONNECT;
00126 hdr->packetLength = 0;
00127 hdr->srcNetworkID =
00128 hdr->version = 1;
00129 if ((numbytes=sendto(sockfd, hdr, sizeof(PacketHeader), 0,
00130 (struct sockaddr *)&srvAddr, sizeof(struct sockaddr))) == -1) {
00131 printError(ErrOS, "Unable to send the packet\n");
00132 return ErrOS;
00133 }
00134 printf("Sent bytes %d\n", numbytes);
00135
00136 fd_set fdset;
00137 FD_ZERO(&fdset);
00138 FD_SET(sockfd, &fdset);
00139 struct timeval timeout;
00140 timeout.tv_sec = Conf::config.getNetworkConnectTimeout();
00141 timeout.tv_usec = 0;
00142 int ret = os::select(sockfd+1, &fdset, 0, 0, &timeout);
00143 if (ret <= 0) {
00144 printError(ErrPeerTimeOut,"Response timeout for peer site\n");
00145 return ErrPeerTimeOut;
00146 }
00147
00148 int response =0;
00149 socklen_t len = sizeof(struct sockaddr);
00150 numbytes = recvfrom(sockfd, &response, 4, 0,
00151 (struct sockaddr *)&fromAddr, &len);
00152 if (numbytes !=4)
00153 {
00154 printf("Unable to receive response from peer\n");
00155 return ErrOS;
00156 }
00157 if (response != 1) return ErrPeerResponse;
00158 isConnectedFlag = true;
00159 printf("NW:UDP connected\n");
00160 return OK;
00161 }
00162
00163 DbRetVal UDPClient::disconnect()
00164 {
00165 if (isConnectedFlag)
00166 printf("NW:UDP disconnect\n");
00167
00168 isConnectedFlag = false;
00169 return OK;
00170 }