OPeNDAP Hyrax Back End Server (BES)  Updated for version 3.8.3
Socket.cc
Go to the documentation of this file.
1 // Socket.cc
2 
3 // This file is part of bes, A C++ back-end server implementation framework
4 // for the OPeNDAP Data Access Protocol.
5 
6 // Copyright (c) 2004-2009 University Corporation for Atmospheric Research
7 // Author: Patrick West <pwest@ucar.edu> and Jose Garcia <jgarcia@ucar.edu>
8 //
9 // This library is free software; you can redistribute it and/or
10 // modify it under the terms of the GNU Lesser General Public
11 // License as published by the Free Software Foundation; either
12 // version 2.1 of the License, or (at your option) any later version.
13 //
14 // This library is distributed in the hope that it will be useful,
15 // but WITHOUT ANY WARRANTY; without even the implied warranty of
16 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 // Lesser General Public License for more details.
18 //
19 // You should have received a copy of the GNU Lesser General Public
20 // License along with this library; if not, write to the Free Software
21 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
22 //
23 // You can contact University Corporation for Atmospheric Research at
24 // 3080 Center Green Drive, Boulder, CO 80301
25 
26 // (c) COPYRIGHT University Corporation for Atmospheric Research 2004-2005
27 // Please read the full copyright statement in the file COPYRIGHT_UCAR.
28 //
29 // Authors:
30 // pwest Patrick West <pwest@ucar.edu>
31 // jgarcia Jose Garcia <jgarcia@ucar.edu>
32 
33 #include "config.h"
34 
35 #include <sys/types.h>
36 #include <sys/socket.h>
37 #include <arpa/inet.h>
38 #ifdef HAVE_UNISTD_H
39 #include <unistd.h>
40 #endif
41 
42 #include <cstdio>
43 #include <cerrno>
44 #include <cstring>
45 
46 #include <sstream>
47 
48 #include "Socket.h"
49 #include "BESLog.h"
50 #include "BESInternalError.h"
51 
52 Socket::Socket(int socket, struct sockaddr *addr) :
53  _socket(socket), _connected(true), _listening(false), _addr_set(true)
54 {
55  char ip[46];
56  unsigned int port;
57  /* ... */
58  switch (addr->sa_family) {
59  case AF_INET:
60  inet_ntop(AF_INET, &(((struct sockaddr_in *) addr)->sin_addr), ip, sizeof(ip));
61  port = ntohs (((struct sockaddr_in *)addr)->sin_port);
62  break;
63  case AF_INET6:
64  inet_ntop(AF_INET6, &(((struct sockaddr_in6 *) addr)->sin6_addr), ip, sizeof(ip));
65  port = ntohs (((struct sockaddr_in6 *)addr)->sin6_port);
66  break;
67  default:
68  snprintf(ip, sizeof(ip), "UNKNOWN FAMILY: %d", addr->sa_family);
69  port = 0;
70  break;
71  }
72  _port = port;
73  _ip = ip;
74 }
75 
77 {
78  if (_connected) {
80  _socket = 0;
81  _connected = false;
82  _listening = false;
83  }
84 }
85 
86 void Socket::send(const string &str, int start, int end)
87 {
88  string send_str = str.substr(start, end);
89  int bytes_written = write(_socket, send_str.c_str(), send_str.length());
90  if (bytes_written == -1) {
91  string err("socket failure, writing on stream socket");
92  const char* error_info = strerror(errno);
93  if (error_info) err += " " + (string) error_info;
94  throw BESInternalError(err, __FILE__, __LINE__);
95  }
96 }
97 
98 int Socket::receive(char *inBuff, const int inSize)
99 {
100  int bytesRead = 0;
101 
102  //if ((bytesRead = read(_socket, inBuff, inSize)) < 1) {
103  // check for EINTR and EAGAIN. jhrg 10/30/13
104  errno = 0;
105  while ((bytesRead = read(_socket, inBuff, inSize)) < 1) {
106  if (errno == EINTR || errno == EAGAIN) {
107  // These codes are only returned when no bytes have been read, so
108  // there is no need to update the values of inSize or inBuff.
109  // jhrg 11/6/13
110  *(BESLog::TheLog()) << "Socket::receive: errno: " << strerror(errno) << ", bytesRead: " << bytesRead << endl;
111  errno = 0;
112  continue;
113  }
114 
115  if (bytesRead < 0) { // Error
116  std::ostringstream oss;
117  oss << "Socket::receive: socket failure, reading on stream socket: " << strerror(errno) << ", bytesRead: "
118  << bytesRead;
119  throw BESInternalError(oss.str(), __FILE__, __LINE__);
120  }
121  else if (bytesRead == 0) // EOF
122  return 0;
123  }
124 
125  return bytesRead;
126 }
127 
134 void Socket::dump(ostream &strm) const
135 {
136  strm << BESIndent::LMarg << "Socket::dump - (" << (void *) this << ")" << endl;
138  strm << BESIndent::LMarg << "socket: " << _socket << endl;
139  strm << BESIndent::LMarg << "is connected? " << _connected << endl;
140  strm << BESIndent::LMarg << "is listening? " << _listening << endl;
141  strm << BESIndent::LMarg << "socket address set? " << _addr_set << endl;
142  if (_addr_set) {
143  strm << BESIndent::LMarg << "socket port: " << _port << endl;
144  strm << BESIndent::LMarg << "socket ip: " << _ip << endl;
145  }
147 }
148 
std::string _ip
Definition: Socket.h:47
exception thrown if inernal error encountered
int _socket
Definition: Socket.h:44
bool _addr_set
Definition: Socket.h:49
virtual void send(const std::string &str, int start, int end)
Definition: Socket.cc:86
virtual void dump(ostream &strm) const
dumps information about this object
Definition: Socket.cc:134
static void Indent()
Definition: BESIndent.cc:38
bool _listening
Definition: Socket.h:46
static ostream & LMarg(ostream &strm)
Definition: BESIndent.cc:73
unsigned int _port
Definition: Socket.h:48
virtual void close()
Definition: Socket.cc:76
virtual int receive(char *inBuff, const int inSize)
Definition: Socket.cc:98
Socket()
Definition: Socket.h:51
static BESLog * TheLog()
Definition: BESLog.cc:347
static void UnIndent()
Definition: BESIndent.cc:44
bool _connected
Definition: Socket.h:45