Skip to content
connection.hh 2.27 KiB
Newer Older
/*
  This file is part of SLS.
  Copyright (C) 2008 Sebastien LUTTRINGER <contact@seblu.net>

Seblu's avatar
Seblu committed
  SLL 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; version 2 of the License.

Seblu's avatar
Seblu committed
  SLL 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
Seblu's avatar
Seblu committed
  along with SLL; if not, write to the Free Software
  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
*/

#ifndef CONNECTION_HH
# define CONNECTION_HH

# include <pthread.h>

class Connection {
  // class methods
public:
  unsigned long int getconnid();

  // public methods
public:
  Connection(int fd = -1);
  virtual ~Connection();

  void connect(const char *addr, int port);
  void listen(int port, int max);

  void disconnect();
Seblu's avatar
Seblu committed
  inline bool connected();

  Connection *accept();

  // send methods
  void send(const char* buf, size_t len);
Seblu's avatar
Seblu committed
  inline void send(const string &data);
  inline void sendln(const string &data);

  // recv methods
Seblu's avatar
Seblu committed
  char *recv(size_t size);
  string recvln();

Seblu's avatar
Seblu committed
  // buffer methods
  void flush();

  // info methods
  string getlocalip();
Seblu's avatar
Seblu committed
  string getlocalhostname();
  int getlocalport();

  string getremoteip();
Seblu's avatar
Seblu committed
  string getremotehostname();
  int getremoteport();

  unsigned long int getid();

  int getsocket();

  // protected methods
protected:
Seblu's avatar
Seblu committed
  inline void socket_();
  inline void disconnect_();

  inline void setlocalip_();
  inline void setlocalport_();
  inline void setremoteip_();
Seblu's avatar
Seblu committed
  inline void setremotehostname_();
Seblu's avatar
Seblu committed
  inline void setremoteport_();

  // Protected datas
protected:
  int socket_fd_; // connection socket

  // storage of info about connection
  int local_port_;
  int remote_port_;

  string local_ip_;
Seblu's avatar
Seblu committed
  string local_hostname_;
  string remote_ip_;
Seblu's avatar
Seblu committed
  string remote_hostname_;
  unsigned long int id_;

  string rbuf_; // read buffer
  string wbuf_; // write buffer

  pthread_mutex_t c_mutex_; // connection mutex
  pthread_mutex_t r_mutex_; // read mutex
  pthread_mutex_t w_mutex_; // write mutex
};

# include "connection.hxx"

#endif