#include <Network.h>
Inheritance diagram for TCPClient:
Public Member Functions | |
TCPClient () | |
DbRetVal | send (NetworkPacketType type, char *buf, int len) |
DbRetVal | receive () |
DbRetVal | connect () |
DbRetVal | disconnect () |
~TCPClient () | |
Data Fields | |
int | sockfd |
sockaddr_in | srvAddr |
Definition at line 86 of file Network.h.
TCPClient::TCPClient | ( | ) | [inline] |
Definition at line 90 of file Network.h.
References NetworkClient::cacheClient, and NetworkClient::isConnectedFlag.
00090 { isConnectedFlag =false; cacheClient = false;}
TCPClient::~TCPClient | ( | ) |
Definition at line 23 of file TCPClient.cxx.
References disconnect(), and NetworkClient::isConnectedFlag.
00024 { 00025 if (isConnectedFlag) disconnect(); 00026 }
Here is the call graph for this function:
DbRetVal TCPClient::connect | ( | ) | [virtual] |
Implements NetworkClient.
Definition at line 80 of file TCPClient.cxx.
References Conf::config, ErrOS, ErrPeerResponse, ErrPeerTimeOut, Config::getNetworkConnectTimeout(), NetworkClient::hostName, NetworkClient::isConnectedFlag, len, OK, NetworkClient::port, printError, os::recv(), os::select(), sockfd, and srvAddr.
00081 { 00082 //printf("NW:TCP connect %s %d %d\n", hostName, port, networkid); 00083 //TODO::send endian to the peer site 00084 //do not do endian conversion here. it will be done at the server side 00085 isConnectedFlag = false; 00086 struct hostent *he; 00087 int numbytes; 00088 if ((he=gethostbyname(hostName)) == NULL) { // get the host info 00089 printError(ErrOS,"Unable to get the peer host name\n"); 00090 return ErrOS; 00091 } 00092 if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) == -1) { 00093 printError(ErrOS,"Unable to create socket to peer host name\n"); 00094 return ErrOS; 00095 } 00096 srvAddr.sin_family = AF_INET; // host byte order 00097 srvAddr.sin_port = htons(port); // short, network byte order 00098 srvAddr.sin_addr = *((struct in_addr *)he->h_addr); 00099 memset(&(srvAddr.sin_zero), '\0', 8); // zero the rest of the struct 00100 if (::connect(sockfd, (struct sockaddr*) & srvAddr, sizeof(struct sockaddr)) 00101 == -1) 00102 { 00103 printError(ErrOS, "Unable to connect to peer site\n"); 00104 return ErrOS; 00105 } 00106 printf("NW:TCP connecting\n"); 00107 fd_set fdset; 00108 FD_ZERO(&fdset); 00109 FD_SET(sockfd, &fdset); 00110 struct timeval timeout; 00111 timeout.tv_sec = Conf::config.getNetworkConnectTimeout(); 00112 timeout.tv_usec = 0; 00113 int ret = os::select(sockfd+1, &fdset, 0, 0, &timeout); 00114 if (ret <= 0) { 00115 printError(ErrPeerTimeOut,"Response timeout for peer site\n"); 00116 return ErrPeerTimeOut; 00117 } 00118 int response =0; 00119 socklen_t len = sizeof(struct sockaddr); 00120 numbytes = os::recv(sockfd, &response, 4, 0); 00121 if (numbytes !=4) 00122 { 00123 printf("Unable to receive response from peer\n"); 00124 return ErrOS; 00125 } 00126 printf("Response from peer site is %d\n", response); 00127 if (response != 1) return ErrPeerResponse; 00128 isConnectedFlag = true; 00129 return OK; 00130 }
Here is the call graph for this function:
DbRetVal TCPClient::disconnect | ( | ) | [virtual] |
Implements NetworkClient.
Definition at line 132 of file TCPClient.cxx.
References ErrOS, NetworkClient::hostName, NetworkClient::isConnectedFlag, NW_PKT_DISCONNECT, OK, PacketHeader::packetLength, PacketHeader::packetType, NetworkClient::port, printError, os::send(), sockfd, PacketHeader::srcNetworkID, and PacketHeader::version.
Referenced by ~TCPClient().
00133 { 00134 if (isConnectedFlag) { 00135 printf("NW:TCP disconnect %s %d\n", hostName, port); 00136 PacketHeader *hdr= new PacketHeader(); 00137 hdr->packetType = NW_PKT_DISCONNECT; 00138 hdr->packetLength = 0; 00139 hdr->srcNetworkID = 00140 hdr->version = 1; 00141 int numbytes=0; 00142 if ((numbytes=os::send(sockfd, hdr, sizeof(PacketHeader), 0)) 00143 == -1) { 00144 printError(ErrOS, "Unable to send the packet\n"); 00145 } else 00146 printf("Sent bytes %d\n", numbytes); 00147 close(sockfd); 00148 00149 } 00150 isConnectedFlag = false; 00151 return OK; 00152 }
Here is the call graph for this function:
Here is the caller graph for this function:
DbRetVal TCPClient::receive | ( | ) | [virtual] |
Implements NetworkClient.
Definition at line 52 of file TCPClient.cxx.
References Conf::config, ErrOS, ErrPeerResponse, ErrPeerTimeOut, Config::getNetworkResponseTimeout(), len, OK, printError, os::recv(), os::select(), and sockfd.
00053 { 00054 DbRetVal rv = OK; 00055 printf("NW:TCP receive\n"); 00056 fd_set fdset; 00057 FD_ZERO(&fdset); 00058 FD_SET(sockfd, &fdset); 00059 struct timeval timeout; 00060 timeout.tv_sec = Conf::config.getNetworkResponseTimeout(); 00061 timeout.tv_usec = 0; 00062 int ret = os::select(sockfd+1, &fdset, 0, 0, &timeout); 00063 if (ret <= 0) { 00064 printError(ErrPeerTimeOut,"Response timeout for peer site\n"); 00065 return ErrPeerTimeOut; 00066 } 00067 00068 int response =0; 00069 socklen_t len = sizeof(struct sockaddr); 00070 int numbytes = os::recv(sockfd, &response, 4, 0); 00071 if (numbytes !=4) 00072 { 00073 printf("Unable to receive response from peer\n"); 00074 return ErrOS; 00075 } 00076 //printf("NW:UDP receive\n"); 00077 if (response != 1) rv = ErrPeerResponse; 00078 return rv; 00079 }
Here is the call graph for this function:
DbRetVal TCPClient::send | ( | NetworkPacketType | type, | |
char * | buf, | |||
int | len | |||
) | [virtual] |
Implements NetworkClient.
Definition at line 27 of file TCPClient.cxx.
References ErrOS, NetworkClient::networkid, OK, PacketHeader::packetLength, PacketHeader::packetType, printError, os::send(), sockfd, PacketHeader::srcNetworkID, and PacketHeader::version.
00028 { 00029 DbRetVal rv = OK; 00030 printf("NW:TCP Send\n"); 00031 void* totalBuffer = malloc(sizeof(PacketHeader)+ len); 00032 PacketHeader *hdr= new PacketHeader(); 00033 hdr->packetType = type; 00034 hdr->packetLength = len; 00035 hdr->srcNetworkID = networkid; 00036 hdr->version = 1; 00037 memcpy(((char*)totalBuffer) + sizeof(PacketHeader) , buf, len); 00038 int numbytes=0; 00039 if ((numbytes=os::send(sockfd, hdr, sizeof(PacketHeader), 0)) == -1) { 00040 printError(ErrOS, "Unable to send the packet\n"); 00041 return ErrOS; 00042 } 00043 printf("Sent bytes %d\n", numbytes); 00044 if ((numbytes=os::send(sockfd, buf, len, 0)) == -1) { 00045 printError(ErrOS, "Unable to send the packet\n"); 00046 return ErrOS; 00047 } 00048 printf("Sent bytes %d\n", numbytes); 00049 free(totalBuffer); 00050 return rv; 00051 }
Here is the call graph for this function:
struct sockaddr_in TCPClient::srvAddr |