#include <Network.h>
Inheritance diagram for UDPClient:
Public Member Functions | |
UDPClient () | |
DbRetVal | send (NetworkPacketType type, char *buf, int len) |
DbRetVal | receive () |
DbRetVal | connect () |
DbRetVal | disconnect () |
~UDPClient () | |
Data Fields | |
int | sockfd |
sockaddr_in | srvAddr |
sockaddr_in | fromAddr |
Definition at line 74 of file Network.h.
UDPClient::UDPClient | ( | ) | [inline] |
Definition at line 79 of file Network.h.
References NetworkClient::cacheClient, and NetworkClient::isConnectedFlag.
00079 { isConnectedFlag =false; cacheClient = false;}
UDPClient::~UDPClient | ( | ) |
Definition at line 23 of file UDPClient.cxx.
References disconnect(), and NetworkClient::isConnectedFlag.
00024 { 00025 if (isConnectedFlag) disconnect(); 00026 }
Here is the call graph for this function:
DbRetVal UDPClient::connect | ( | ) | [virtual] |
Implements NetworkClient.
Definition at line 101 of file UDPClient.cxx.
References Conf::config, ErrOS, ErrPeerResponse, ErrPeerTimeOut, fromAddr, Config::getNetworkConnectTimeout(), NetworkClient::hostName, NetworkClient::isConnectedFlag, len, NetworkClient::networkid, NW_PKT_CONNECT, OK, PacketHeader::packetLength, PacketHeader::packetType, NetworkClient::port, printError, os::select(), sockfd, PacketHeader::srcNetworkID, srvAddr, and PacketHeader::version.
00102 { 00103 printf("NW:UDP connect %s %d %d\n", hostName, port, networkid); 00104 00105 //TODO::send endian to the peer site 00106 //do not do endian conversion here. it will be done at the server side 00107 DbRetVal rv = OK; 00108 isConnectedFlag = false; 00109 struct hostent *he; 00110 int numbytes; 00111 if ((he=gethostbyname(hostName)) == NULL) { // get the host info 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; // host byte order 00120 srvAddr.sin_port = htons(port); // short, network byte order 00121 srvAddr.sin_addr = *((struct in_addr *)he->h_addr); 00122 memset(&(srvAddr.sin_zero), '\0', 8); // zero the rest of the struct 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 }
Here is the call graph for this function:
DbRetVal UDPClient::disconnect | ( | ) | [virtual] |
Implements NetworkClient.
Definition at line 163 of file UDPClient.cxx.
References NetworkClient::isConnectedFlag, and OK.
Referenced by ~UDPClient().
00164 { 00165 if (isConnectedFlag) 00166 printf("NW:UDP disconnect\n"); 00167 00168 isConnectedFlag = false; 00169 return OK; 00170 }
Here is the caller graph for this function:
DbRetVal UDPClient::receive | ( | ) | [virtual] |
Implements NetworkClient.
Definition at line 66 of file UDPClient.cxx.
References Conf::config, ErrOS, ErrPeerResponse, ErrPeerTimeOut, fromAddr, Config::getNetworkResponseTimeout(), len, OK, printError, os::select(), sockfd, and srvAddr.
00067 { 00068 //TODO:: resoibse timeout during socket read 00069 DbRetVal rv = OK; 00070 fd_set fdset; //TODO::Move it to UDPClient class 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 //printf("NW:UDP receive\n"); 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 //printf("Response rv %d\n", rv); 00099 return rv; 00100 }
Here is the call graph for this function:
DbRetVal UDPClient::send | ( | NetworkPacketType | type, | |
char * | buf, | |||
int | len | |||
) | [virtual] |
Implements NetworkClient.
Definition at line 27 of file UDPClient.cxx.
References ErrOS, NetworkClient::networkid, OK, PacketHeader::packetLength, PacketHeader::packetType, printError, sockfd, PacketHeader::srcNetworkID, srvAddr, and PacketHeader::version.
00028 { 00029 DbRetVal rv = OK; 00030 //printf("NW:UDP Send\n"); 00031 void* totalBuffer = malloc(sizeof(PacketHeader)+ len); 00032 //PacketHeader *hdr= (PacketHeader*) totalBuffer; 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 int numbytes =sendto(sockfd, totalBuffer, sizeof(PacketHeader)+ len, 0, 00042 (struct sockaddr *)&srvAddr, sizeof(struct sockaddr)); 00043 00044 if (numbytes== -1) { 00045 printError(ErrOS,"Unable to send the packet\n"); 00046 return ErrOS; 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 //printf("Sent bytes %d\n", numbytes); 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 //printf("Sent bytes %d\n", numbytes); 00062 free(totalBuffer); 00063 return rv; 00064 00065 }
struct sockaddr_in UDPClient::fromAddr |
struct sockaddr_in UDPClient::srvAddr |