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 TCPClient::~TCPClient()
00024 {
00025 if (isConnectedFlag) disconnect();
00026 }
00027 DbRetVal TCPClient::send(NetworkPacketType type, char *buf, int len)
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 }
00052 DbRetVal TCPClient::receive()
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
00077 if (response != 1) rv = ErrPeerResponse;
00078 return rv;
00079 }
00080 DbRetVal TCPClient::connect()
00081 {
00082
00083
00084
00085 isConnectedFlag = false;
00086 struct hostent *he;
00087 int numbytes;
00088 if ((he=gethostbyname(hostName)) == NULL) {
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;
00097 srvAddr.sin_port = htons(port);
00098 srvAddr.sin_addr = *((struct in_addr *)he->h_addr);
00099 memset(&(srvAddr.sin_zero), '\0', 8);
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 }
00131
00132 DbRetVal TCPClient::disconnect()
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 }