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

#ifndef CLIENT_H
#define CLIENT_H

#include <strstream.h>

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

// The client class allows you to write programs that can talk to server
// 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 client {
        public:
                        client();
                                // creates a client instance
                        client(char *hostname, unsigned short int port, 
                                int retrytime, int tries);
                                // creates a client instance and calls
                                // openInetConnection() below
                        client(char *unixport, int retrytime, int tries);
                                // creates a client instance and calls
                                // openUnixConnection() below
                virtual ~client();


                // These methods establish connections to a server.  
                // In each, if the connection fails, they will try again
                // for a total of "tries" times, waiting "retrytime" seconds
                // between tries.
                int     openInetConnection(char *hostname, 
                                        unsigned short int port, 
                                        int retrytime, int tries);
                                // connects to "hostname"/"port"
                int     openUnixConnection(char *unixport, 
                                        int retrytime, int tries);
                                // connects to unix socket "unixport"

                // These methods write data to the server.  On success, the
                // number of bytes written is returned.  On failure, a -1 is
                // returned.
                int     writeToServer(short number) const;
                int     writeToServer(long number) const;
                int     writeToServer(float number) const;
                int     writeToServer(double number) const;
                int     writeToServer(char character) const;
                int     writeToServer(char *string) const;
                int     writeToServer(char *string, int size) const;
                int     writeToServer(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     readFromServer(short *buffer) const;
                int     readFromServer(long *buffer) const;
                int     readFromServer(float *buffer) const;
                int     readFromServer(double *buffer) const;
                int     readFromServer(char *buffer, int size) const;
                int     readFromServer(void *buffer, int size) const;
                int     readFromServer(strstream *buffer, char *terminator);

                void    closeConnection();
                        // disconnects from the server

                int     getServerSocket();
                        // Returns the internal file descriptor of the unixport
                        // used to connect to the server.

        private:
                #include <private/client.h>

};

#endif