rockin-refbox /src/libs/netcomm/socket/socket.h

Language C/C++ Header Lines 149
MD5 Hash 9e0ab67b8f5b178f98f6befc12de323c
Repository https://gitlab.com/F34140r/rockin-refbox.git View Raw File
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
/***************************************************************************
 *  socket.h - Fawkes socket base class
 *
 *  Created: Thu Nov 09 12:55:25 2006
 *  Copyright  2006  Tim Niemueller [www.niemueller.de]
 *
 ****************************************************************************/

/*  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. A runtime exception applies to
 *  this software (see LICENSE.GPL_WRE file mentioned below for details).
 *
 *  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 Library General Public License for more details.
 *
 *  Read the full text in the LICENSE.GPL_WRE file in the doc directory.
 */

#ifndef __NETCOMM_SOCKET_SOCKET_H_
#define __NETCOMM_SOCKET_SOCKET_H_

#include <core/exception.h>
#include <core/exceptions/software.h>

#include <sys/socket.h>
#include <sys/types.h>
#include <netinet/in.h>
// just to be safe nobody else can do it
#include <sys/signal.h>

#ifdef POLL_IN
#  undef POLL_IN
#endif
#ifdef POLL_OUT
#  undef POLL_OUT
#endif
#ifdef POLL_PRI
#  undef POLL_PRI
#endif
#ifdef POLL_RDHUP
#  undef POLL_RDHUP
#endif
#ifdef POLL_ERR
#  undef POLL_ERR
#endif
#ifdef POLL_HUP
#  undef POLL_HUP
#endif


namespace fawkes {

class SocketException : public Exception
{
 public:
  SocketException(const char *msg, int _errno);
  SocketException(const char *msg);
};

class Socket
{
 public:

  static const short POLL_IN;
  static const short POLL_OUT;
  static const short POLL_PRI;
  static const short POLL_RDHUP;
  static const short POLL_ERR;
  static const short POLL_HUP;
  static const short POLL_NVAL;

  Socket(int domain, int type, int protocol, float timeout = 0.f);
  Socket(Socket &socket);
  virtual ~Socket();

  virtual void         connect(const char *hostname, const unsigned short int port);
  virtual void         connect(struct sockaddr *addr_port, unsigned int struct_size);

  virtual void         bind(const unsigned short int port);
  virtual void         bind(const unsigned short int port,
			    const char *hostname);

  virtual void         listen(int backlog = 1);
  virtual Socket *     accept();
  virtual void         close();
  virtual bool         available();

  virtual size_t       read(void *buf, size_t count, bool read_all = true);
  virtual void         write(const void *buf, size_t count);
  virtual void         send(void *buf, size_t buf_len);
  virtual void         send(void *buf, size_t buf_len,
			    const struct sockaddr *to_addr, socklen_t addr_len);
  virtual size_t       recv(void *buf, size_t buf_len);
  virtual size_t       recv(void *buf, size_t buf_len,
			    struct sockaddr *from_addr, socklen_t *addr_len);

  /** Clone socket.
   * This method has to be implemented by subclass to correctly clone the instance.
   * @return cloned socket
   */
  virtual Socket *     clone() = 0;

  virtual short        poll(int timeout = -1, short what = POLL_IN | POLL_HUP | POLL_PRI | POLL_RDHUP);

  virtual bool         listening();

  virtual unsigned int mtu();

  /** Accept connection.
   * This method works like accept() but it ensures that the returned socket is of
   * the given type.
   * @return socket to client
   */
  template <class SocketType>
    SocketType *     accept();

 protected:
  Socket();

  int sock_fd;
  float timeout;
  struct ::sockaddr_in  *client_addr;
  unsigned int         client_addr_len;

};


template <class SocketType>
SocketType *
Socket::accept()
{
  Socket *s = accept();
  if (SocketType *ts = dynamic_cast<SocketType *>(s)) {
    return ts;
  } else {
    delete s;
    throw TypeMismatchException("Socket types do not match");
  }
}

} // end namespace fawkes

#endif
Back to Top