#include <Network.h>
Inheritance diagram for UDPServer:
Public Member Functions | |
UDPServer () | |
DbRetVal | start () |
DbRetVal | stop () |
DbRetVal | handleClient () |
Definition at line 241 of file Network.h.
UDPServer::UDPServer | ( | ) | [inline] |
Definition at line 245 of file Network.h.
References NetworkServer::port, and NetworkServer::sockfd.
DbRetVal UDPServer::handleClient | ( | ) | [virtual] |
Implements NetworkServer.
Definition at line 55 of file UDPServer.cxx.
References Conf::config, ErrOS, ErrPeerTimeOut, Config::getNetworkResponseTimeout(), NW_PKT_CONNECT, OK, PacketHeader::packetLength, PacketHeader::packetType, printError, SqlNetworkHandler::process(), os::select(), and NetworkServer::sockfd.
00056 { 00057 DbRetVal rv = OK; 00058 PacketHeader header; 00059 socklen_t addressLen = sizeof(struct sockaddr); 00060 //printf("UDP Server receives packet\n"); 00061 int numbytes = recvfrom(sockfd, &header, sizeof(PacketHeader), 0, 00062 (struct sockaddr*) &clientAddress, &addressLen); 00063 if (numbytes == -1) 00064 { 00065 printf("Error reading from socket\n"); 00066 return ErrOS; 00067 } 00068 //printf("HEADERINFO %d %d %d %d\n", header.packetType, header.packetLength, 00069 // header.srcNetworkID, header.version); 00070 if (header.packetType == NW_PKT_CONNECT) 00071 { 00072 int response =1; 00073 numbytes = sendto(sockfd, &response, 4, 0, 00074 (struct sockaddr*) &clientAddress, addressLen); 00075 if (numbytes != 4) 00076 { 00077 printf("Error writing to socket\n"); 00078 return ErrOS; 00079 } 00080 return OK; 00081 } 00082 char *buffer = (char*) malloc(header.packetLength); 00083 fd_set fdset; //TODO::Move it to UDPClient class 00084 FD_ZERO(&fdset); 00085 FD_SET(sockfd, &fdset); 00086 struct timeval timeout; 00087 timeout.tv_sec = Conf::config.getNetworkResponseTimeout(); 00088 timeout.tv_usec = 0; 00089 int ret = os::select(sockfd+1, &fdset, 0, 0, &timeout); 00090 if (ret <= 0) { 00091 printError(ErrPeerTimeOut,"Response timeout for peer site\n"); 00092 return ErrPeerTimeOut; 00093 } 00094 00095 numbytes = recvfrom(sockfd, buffer, header.packetLength, 0, 00096 (struct sockaddr*) &clientAddress, &addressLen); 00097 00098 //printf("Bytes read %d\n", numbytes); 00099 if (numbytes == -1) 00100 { 00101 printf("Error reading from socket\n"); 00102 return ErrOS; 00103 } 00104 SqlNetworkHandler handler; 00105 int response = handler.process(header, buffer); 00106 numbytes = sendto(sockfd, &response, 4, 0, 00107 (struct sockaddr*) &clientAddress, addressLen); 00108 if (numbytes != 4) 00109 { 00110 printf("Error writing to socket\n"); 00111 return ErrOS; 00112 } 00113 return OK; 00114 }
Here is the call graph for this function:
DbRetVal UDPServer::start | ( | ) | [virtual] |
Implements NetworkServer.
Definition at line 24 of file UDPServer.cxx.
References ErrBadArg, ErrOS, OK, NetworkServer::port, printError, and NetworkServer::sockfd.
00025 { 00026 DbRetVal rv = OK; 00027 if (port == 0 ) 00028 { 00029 printError(ErrBadArg, "Set the port first before starting\n"); 00030 return ErrBadArg; 00031 } 00032 struct sockaddr_in my_addr; 00033 if ((sockfd = socket(AF_INET, SOCK_DGRAM, 0)) == -1) { 00034 printError(ErrOS, "Unable to create socket\n"); 00035 return ErrOS; 00036 } 00037 my_addr.sin_family = AF_INET; 00038 my_addr.sin_port = htons(port); 00039 my_addr.sin_addr.s_addr = INADDR_ANY; 00040 memset(&(my_addr.sin_zero), '\0', 8); 00041 if (bind(sockfd, (struct sockaddr *)&my_addr, 00042 sizeof(struct sockaddr)) == -1) { 00043 return ErrOS; 00044 } 00045 return rv; 00046 00047 }
DbRetVal UDPServer::stop | ( | ) | [virtual] |
Implements NetworkServer.
Definition at line 48 of file UDPServer.cxx.
References OK, and NetworkServer::sockfd.
00049 { 00050 //TODO:: response timeout during socket read 00051 DbRetVal rv = OK; 00052 close(sockfd); 00053 return rv; 00054 }