// -*- c++ -*-

/*
 * Jakelib2 - General purpose C++ library
 * Copyright (C) 2001 Florian Wolff  (florian@donuz.de)
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library 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.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 * $Id: Socket.h,v 1.20 2003/09/05 15:17:38 florian Exp $
 */

#ifndef INCLUDE_JAKELIB2_NET_SOCKET_H
# define INCLUDE_JAKELIB2_NET_SOCKET_H

namespace jakelib {
  namespace net {

    class InetAddress;
    class SocketOutputStream;
    class SocketInputStream;

    /**
     * A Socket encapsulates on end of a TCP network connection between
     * a server and a client host.
     *
     * A client can create a new \c Socket object to connect to a server
     * with a certain hostname and port address.
     *
     * @class Socket
     * @version $Id: Socket.h,v 1.20 2003/09/05 15:17:38 florian Exp $
     * @author Florian 'Overflo' Wolff (florian@donuz.de)
     */
    class Socket : public jakelib::lang::Object
    {
    public:

      /**
       * @brief Creates a stream socket and connects it to the specified port
       * number on the named host.
       *
       * @param host The host name.
       * @param port The port number.
       * @throws IOException If an I/O error occurs when creating the socket.
       */
      Socket(jakelib::lang::String *host, int port);

      /**
       * Used internally when accepting incoming connections via
       * \c ServerSocket.
       */
      Socket(JL2_SOCKET sock, int port);

      ~Socket();
  
      /**
       * @brief Closes this socket.
       */
      void close();
  
      /**
       * @brief Returns an output stream for this socket.
       *
       * @return An output stream for writing jbytes to this socket.
       * @exception  IOException If an I/O error occurs when creating the output stream.
       */
      SocketOutputStream* getOutputStream();
  
      /**
       * @brief Returns an input stream for this socket.
       *
       * @return An input stream for reading jbytes from this socket.
       * @throw IOException  if an I/O error occurs when creating the input stream.
       */
      SocketInputStream* getInputStream();
  
      /**
       * @brief Returns the remote port to which this socket is connected.
       *
       * @return The remote port number to which this socket is connected.
       */
      int getPort();
  
      /**
       * @brief Returns the address of the remote host.
       */
      InetAddress* getInetAddress();
  
      /**
       * Internally used to initialize WinSock library on Win32 systems.
       * Can safely be called more than once.
       */
      static void initWinsock();

      /**
       * Returns the error code of the last error that occurred.
       */
      static int getLastError();
  
    protected:

      /** Holds the host name. */
      jakelib::lang::String* host;

      /** Holds the port address. */
      int port;

      /** Holds the system-dependend handle for the socket. */
      JL2_SOCKET sock;

      /** Holds the output stream. */
      SocketOutputStream* outputStream;

      /** Holds the input stream. */
      SocketInputStream* inputStream;
  
      /** Indicates whether WinSock library is initialized. */
      static jboolean winsockInitialized;

    private:

      /**
       * The actual 'constructor' method.
       */
      void init(jakelib::lang::String* host, int port);

      JAKELIB_DECLARE_CLASS(Socket);
    };

  }
}
#endif

