Bug in putting the prepare packet in the list
[csql.git] / src / network / TCPServer.cxx
blob802f7a3fa968ad4fff65e5b5f13c308da9fd9dbf
1 /***************************************************************************
2 * Copyright (C) 2007 by Prabakaran Thirumalai *
3 * praba_tuty@yahoo.com *
4 * *
5 * This program is free software; you can redistribute it and/or modify *
6 * it under the terms of the GNU General Public License as published by *
7 * the Free Software Foundation; either version 2 of the License, or *
8 * (at your option) any later version. *
9 * *
10 * This program is distributed in the hope that it will be useful, *
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13 * GNU General Public License for more details. *
14 * *
15 * You should have received a copy of the GNU General Public License *
16 * along with this program; if not, write to the *
17 * Free Software Foundation, Inc., *
18 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
19 ***************************************************************************/
20 #include <CSql.h>
21 #include <Network.h>
22 #include <SqlNetworkHandler.h>
24 DbRetVal TCPServer::start()
26 DbRetVal rv = OK;
27 if (port == 0 )
29 printError(ErrBadArg, "Set the port first before starting\n");
30 return ErrBadArg;
32 struct sockaddr_in my_addr;
33 if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) == -1) {
34 printError(ErrOS, "Unable to create socket\n");
35 return ErrOS;
37 my_addr.sin_family = AF_INET;
38 my_addr.sin_port = htons(port);
39 my_addr.sin_addr.s_addr = INADDR_ANY;
40 memset(&(my_addr.sin_zero), '\0', 8);
41 if (bind(sockfd, (struct sockaddr *)&my_addr,
42 sizeof(struct sockaddr)) == -1) {
43 return ErrOS;
45 if (listen(sockfd, 10) == -1) {
46 return ErrOS;
48 return rv;
51 DbRetVal TCPServer::stop()
53 DbRetVal rv = OK;
54 close (sockfd);
55 return rv;
57 DbRetVal TCPServer::handleClient()
59 printf("PRABA::handling client \n");
60 DbRetVal rv = OK;
61 socklen_t addressLen = sizeof(struct sockaddr);
62 clientfd = accept(sockfd, (struct sockaddr*) &clientAddress, &addressLen);
63 int ret = os::fork();
64 if (ret) {
65 //parent
66 close(clientfd);
67 return OK;
68 }else if (ret == 0) {
69 //child
70 int response = 1;
71 int ret = os::send(clientfd, &response, 4, 0);
72 if (ret == -1)
74 printError(ErrOS, "Unable to communicate to peer\n");
75 return ErrOS;
77 printf("sent connect ack packet to client\n");
78 fd_set fdset;
79 struct timeval timeout;
80 SqlNetworkHandler handler;
81 PacketHeader header;
82 while(true) {
83 FD_ZERO(&fdset);
84 FD_SET(clientfd, &fdset);
85 timeout.tv_sec = 5;
86 timeout.tv_usec = 0;
87 int ret = os::select(clientfd+1, &fdset, 0, 0, &timeout);
88 if (ret > 0) {
89 printf("something in fd\n");
90 int numbytes = os::recv(clientfd,&header,sizeof(PacketHeader),0);
91 if (numbytes == -1)
93 printError(ErrOS, "Error reading from socket\n");
94 return ErrOS;
96 printf("HEADER says packet type is %d\n", header.packetType);
97 if (header.packetType == NW_PKT_DISCONNECT) exit(0);
98 char *buffer = (char*) malloc(header.packetLength);
99 numbytes = os::recv(clientfd,buffer,header.packetLength,0);
100 if (numbytes == -1)
102 printError(ErrOS, "Error reading from socket\n");
103 return ErrOS;
106 int response = handler.process(header, buffer);
107 numbytes = os::send(clientfd, &response, 4, 0);
108 if (numbytes != 4)
110 printError(ErrOS, "Error writing to socket\n");
111 return ErrOS;
113 } else printf("Nothing in fd %d\n", ret);
115 }else {
116 printError(ErrOS, "Unable to fork new process");
117 return ErrOS;
119 return OK;