// Copyright (c) 1999-2000 David Muse
// See the COPYING file for more information.

#ifndef SERVER_H
#define SERVER_H

#include <strstream.h>

#include <sys/types.h>
#include <sys/un.h>
#include <netinet/in.h>
#include <netdb.h>

// The server class allows you to write programs that can talk to client
// programs over TCP connections.
//
// It provides connection management and data transfer methods but
// does not imply any particular client/server protocol.
//
// The class supports TCP connections over Inet and Unix domain sockets.
// Inet sockets allow clients and servers on different machines to talk over a
// network.  Unix sockets allow clients and servers on the same machine to 
// talk.  Inet sockets can be used by clients and servers on the same machine
// but Unix sockets generally perform better.

class server {
        public:
                        server();
                virtual ~server();

                int     listenOnInetPort(unsigned short int port, 
                                                int backlog);
                                // Listen on inet port: "port" and allow 
                                // "backlog" connections to pile up before 
                                // refusing them.
                                // Entering a value of 0 for the "port" causes
                                // the server to listen on an arbitrary port.
                                // The getInetServerPort() method can be
                                // used later to discover this port.
                int     listenOnUnixPort(char *port, mode_t permissions,
                                                int backlog);
                                // Listen on unix port: "port" and allow 
                                // "backlog" connections to pile up before 
                                // refusing them.  Set the permissions on
                                // "port" to "permissions".
                int     listenOnPorts(unsigned short int inetport, 
                                        char *unixport, mode_t permissions,
                                        int backlog);
                                // listen on inet port: "inetport" and
                                // unix port: "unixport" and allow
                                // "backlog" connections to pile up before 
                                // refusing them.  Set the permissions on
                                // "unixport" to "permissions".

                int     waitForClientConnection(int timeoutsec, 
                                                int timeoutusec);
                                // Wait for connections on any ports opened
                                // by the calls above for "timeoutsec" seconds
                                // and "timeoutusec" milliseconds before 
                                // falling through.  
                                //
                                // Entering -1 for either parameter causes the 
                                // method to wait indefinitely.  
                                //
                                // Entering 0 for both parameters causes the 
                                // method to fall through immediately unless a
                                // client is already trying to connect.  
                                //
                                // Returns 1 for a successful connection and 0
                                // otherwise.

                // These methods write data to the server.  On success, the
                // number of bytes written is returned.  On failure, a -1 is
                // returned.
                int     writeToClient(short number) const;
                int     writeToClient(long number) const;
                int     writeToClient(float number) const;
                int     writeToClient(double number) const;
                int     writeToClient(char character) const;
                int     writeToClient(char *string) const;
                int     writeToClient(char *string, int size) const;
                int     writeToClient(void *buffer, int size) const;

                // These methods read data from the server.  If no data is
                // available, they will wait until the server sends data or
                // closes the connection.  On success, the number of bytes 
                // read is returned.  On failure, a -1 is returned.
                int     readFromClient(short *buffer) const;
                int     readFromClient(long *buffer) const;
                int     readFromClient(float *buffer) const;
                int     readFromClient(double *buffer) const;
                int     readFromClient(char *buffer, int size) const;
                int     readFromClient(void *buffer, int size) const;
                int     readFromClient(strstream *buffer, char *terminator);

                void    disconnectClient();
                        // disconnects any connected client
                void    stopListening();
                        // disconnects the server process from any ports
                        // it may be listening on

                unsigned short int      getInetServerPort();
                                        // returns the inet port number that 
                                        // the server is listening on
                char                    *getUnixServerPort();
                                        // returns the filename of the unix 
                                        // port that the server is listening on

                int     getServerSocket();
                        // returns the internal file descriptor 
                        // of the server socket
                int     getClientSocket();
                        // returns the internal file descriptor 
                        // of the client socket

        protected:
                #include <private/server.h>

};

#endif