/* This file is part of cardwords
   (c) 1999 Tobias Peters
   see file COPYING for the copyright terms.
   
   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 2 of the License, or
   (at your option) any later version.

   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.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program; if not, write to the Free Software
   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
*/
// cardwords_tcpclient.hh

// The purpose of class CardWords_TCPClient is:
// - establishing a TCP connection to the cardwords_server
// - act as a base class for CardWords_BAseClient, which also handles
//   messages and the protocol overhead

#ifndef CARDWORDS_TCPCLIENT_HH
#define CARDWORDS_TCPCLIENT_HH


#include <arpa/inet.h>
#include <string>

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

  // returns true if a TCP connection exists
  bool is_connected(void) const;
  
  // Call this only while not connected. While connected,
  // this call will fail and return -1.
  // If the server_name can not be resolved, returns -2.
  // returns 0 on success.
  int
  set_remote_hostname(const string &);

  // Returns 0 if no name is set
  string
  get_remote_hostname(void) const;
  
  // Call this only while not connected. While connected to a
  // cardwords_server, this call will fail and return -1.
  // returns 0 on success.
  int set_remote_port_number(const string &);
  int set_remote_port_number(unsigned short);

  unsigned short get_remote_port_number(void) const;

  // connect is virtual because a child class might also do some logging
  // after the TCP connection is established.
  // Returns 0 on success.
  // Returns -1 if the tcp connection failed.
  virtual int connect(void);

  // Disconnect closes a connection. If not connected, it does nothing.
  virtual void disconnect(void);
  
  int get_fd(void) const;
private:
  struct sockaddr_in sn;
  struct hostent *host;
  unsigned short port;

  bool connected;

  int socket_fd;

};
#endif // CARDWORDS_TCPCLIENT_HH
