TCPServer Class Reference

#include <Network.h>

Inheritance diagram for TCPServer:

Inheritance graph
[legend]
Collaboration diagram for TCPServer:

Collaboration graph
[legend]

Public Member Functions

 TCPServer ()
DbRetVal start ()
DbRetVal stop ()
DbRetVal handleClient ()

Data Fields

sockaddr_in clientAddress
int clientfd

Detailed Description

Definition at line 250 of file Network.h.


Constructor & Destructor Documentation

TCPServer::TCPServer (  )  [inline]

Definition at line 255 of file Network.h.

References clientfd, NetworkServer::port, and NetworkServer::sockfd.

00255 { port = 0; sockfd = -1; clientfd = -1;}


Member Function Documentation

DbRetVal TCPServer::handleClient (  )  [virtual]

Implements NetworkServer.

Definition at line 57 of file TCPServer.cxx.

References clientAddress, clientfd, ErrOS, os::fork(), NW_PKT_DISCONNECT, OK, PacketHeader::packetLength, PacketHeader::packetType, printError, os::recv(), os::select(), os::send(), and NetworkServer::sockfd.

00058 {   
00059    printf("PRABA::handling client \n");
00060    DbRetVal rv = OK;
00061    socklen_t addressLen = sizeof(struct sockaddr);
00062    clientfd = accept(sockfd, (struct sockaddr*) &clientAddress, &addressLen);
00063    int ret = os::fork();
00064    if (ret) {
00065        //parent
00066        close(clientfd);
00067        return OK;
00068    }else if (ret == 0) {
00069        //child
00070        int response = 1;
00071        int ret = os::send(clientfd, &response, 4, 0); 
00072        if (ret == -1)
00073        {
00074            printError(ErrOS, "Unable to communicate to peer\n");
00075            return ErrOS;
00076        }
00077        printf("sent connect ack packet to client\n");
00078        fd_set fdset; 
00079        struct timeval timeout;
00080        SqlNetworkHandler handler;
00081        PacketHeader header;
00082        while(true) {
00083            FD_ZERO(&fdset);
00084            FD_SET(clientfd, &fdset);
00085            timeout.tv_sec = 5;
00086            timeout.tv_usec = 0;
00087            int ret = os::select(clientfd+1, &fdset, 0, 0, &timeout);
00088            if (ret > 0) {
00089                 printf("something in fd\n");
00090                int numbytes = os::recv(clientfd,&header,sizeof(PacketHeader),0);
00091                if (numbytes == -1)
00092                {
00093                    printError(ErrOS, "Error reading from socket\n");
00094                    return ErrOS;
00095                }
00096                printf("HEADER says packet type is %d\n", header.packetType);
00097                if (header.packetType == NW_PKT_DISCONNECT) exit(0);
00098                char *buffer = (char*) malloc(header.packetLength);
00099                numbytes = os::recv(clientfd,buffer,header.packetLength,0);
00100                if (numbytes == -1)
00101                {
00102                    printError(ErrOS, "Error reading from socket\n");
00103                    return ErrOS;
00104                }
00105                
00106                int response = handler.process(header, buffer);
00107                numbytes = os::send(clientfd, &response, 4, 0);
00108                if (numbytes != 4)
00109                {
00110                    printError(ErrOS, "Error writing to socket\n");
00111                    return ErrOS;
00112                }
00113            } else printf("Nothing in fd %d\n", ret);
00114        }
00115    }else {
00116         printError(ErrOS, "Unable to fork new process");
00117         return ErrOS;
00118    } 
00119    return OK;
00120 }

Here is the call graph for this function:

DbRetVal TCPServer::start (  )  [virtual]

Implements NetworkServer.

Definition at line 24 of file TCPServer.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_STREAM, 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     if (listen(sockfd, 10) == -1) {
00046         return ErrOS;
00047     }
00048     return rv;
00049     
00050 }

DbRetVal TCPServer::stop (  )  [virtual]

Implements NetworkServer.

Definition at line 51 of file TCPServer.cxx.

References OK, and NetworkServer::sockfd.

00052 {
00053     DbRetVal rv = OK;
00054     close (sockfd);
00055     return rv;
00056 }


Field Documentation

struct sockaddr_in TCPServer::clientAddress

Definition at line 253 of file Network.h.

Referenced by handleClient().

int TCPServer::clientfd

Definition at line 254 of file Network.h.

Referenced by handleClient(), and TCPServer().


The documentation for this class was generated from the following files:
Generated on Mon Jun 9 22:54:11 2008 for csql by  doxygen 1.4.7