// ---------------------------------------------------------------------------
// - Socket.hpp                                                              -
// - aleph:net library - socket class definition                             -
// ---------------------------------------------------------------------------
// - This program is free software;  you can redistribute it  and/or  modify -
// - it provided that this copyright notice is kept intact.                  -
// -                                                                         -
// - This program  is  distributed in  the hope  that it will be useful, but -
// - without  any  warranty;  without  even   the   implied    warranty   of -
// - merchantability or fitness for a particular purpose.  In no event shall -
// - the copyright holder be liable for any  direct, indirect, incidental or -
// - special damages arising in any way out of the use of this software.     -
// ---------------------------------------------------------------------------
// - copyright (c) 1999-2001 amaury darsch                                   -
// ---------------------------------------------------------------------------

#ifndef  ALEPH_SOCKET_HPP
#define  ALEPH_SOCKET_HPP

#ifndef  ALEPH_INPUT_HPP
#include "Input.hpp"
#endif

#ifndef  ALEPH_OUTPUT_HPP
#include "Output.hpp"
#endif

#ifndef  ALEPH_ADDRESS_HPP
#include "Address.hpp"
#endif

namespace aleph {

  /// The Socket class is a base class for the socket server and socket
  /// client objects. A socket is composed of an address, a port number 
  /// and a stream descriptor. 
  /// @author amaury darsch

  class Socket : public Input, public Output {
  public:
    /// the socket options
    enum t_option {
      SOCK_BROADCAST = 0,  // enable packet broadcast
      SOCK_DONTROUTE = 1,  // bypass routing table
      SOCK_KEEPALIVE = 2,  // set connection test
      SOCK_LINGER    = 3,  // set linger time
      SOCK_RCVSIZE   = 4,  // receive buffer size
      SOCK_SNDSIZE   = 5,  // send buffer size
      SOCK_HOPLIMIT  = 6,  // set hop limit
      SOCK_MCASTLOOP = 7,  // multicast use lopback
      SOCK_MCASTHOP  = 8, // multicast hop limit
      SOCK_MAXSEG    = 9, // tcp maximum segment size
      SOCK_NODELAY   = 10  // disable naggle algorithm
    };

  protected:
    /// the socket descriptor
    int d_sid;

  public:
    /// create a default socket
    Socket (void);

    /// create a socket by id
    /// @param sid  the socket id
    Socket (const int sid);

    /// destroy this socket
    ~Socket (void);

    /// @return the class name
    String repr (void) const;

    /// close this socket
    virtual bool close (void);

    /// shutdown this socket. If the mode is false, further receive is not
    /// possible - if the mode is tue, further send is not possible
    /// @param mode the shutdown mode
    virtual bool shutdown (const bool mode);

    /// @return true if we have an ipv6 socket
    virtual bool isipv6 (void) const;

    /// set a socket option
    /// @param opt  the socket option
    /// @param flag the flag to set
    virtual bool setopt (t_option opt, bool flag);

    /// set a socket option with a value
    /// @param opt  the socket option
    /// @param flag the flag to set
    /// @param val  the value to set
    virtual bool setopt (t_option opt, bool val, long val1);

    /// connect this socket by port and address
    /// @param port the port use
    /// @apram addr the address to use
    virtual bool connect (t_word port, const Address& addr);

    /// bind this socket with a port
    /// @param port the port to bind
    virtual  bool bind (t_word port);

    /// bind this socket with a port and an address
    /// @param port the port to bind
    /// @param addr the addree to bind
    virtual bool bind (t_word port, const Address& addr);

    /// @return the socket address
    virtual Address* getsockaddr (void) const;

    /// @return the socket port
    virtual t_word getsockport (void) const;

    /// @return the peer address
    virtual Address* getpeeraddr (void) const;

    /// @return the peer port
    virtual t_word getpeerport (void) const;

    /// evaluate a socket data member
    /// @param robj  robj the current runnable
    /// @param nset  the current nameset    
    /// @param quark the quark to evaluate
    static Object* meval (Runnable* robj, Nameset* nset, const long quark);

    /// apply this socket with a set of arguments and a quark
    /// @param robj  robj the current runnable
    /// @param nset  the current nameset    
    /// @param quark the quark to apply these arguments
    /// @param argv  the arguments to apply
    Object* apply (Runnable* robj, Nameset* nset, const long quark,
                   Vector* argv);

  private:
    // make the copy constructor private
    Socket (const Socket&);
    // make the assignment operator private
    Socket& operator = (const Socket&);
  };
}

#endif
