bes Updated for version 3.20.13
Socket.cc
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 <string>
36#include <sstream>
37
38#include <cstdio>
39#include <cerrno>
40#include <cstring>
41
42#include <sys/types.h>
43#include <sys/socket.h>
44#include <arpa/inet.h>
45#ifdef HAVE_UNISTD_H
46#include <unistd.h>
47#endif
48
49#include "Socket.h"
50#include "BESLog.h"
51#include "BESInternalError.h"
52
53using namespace std;
54
55Socket::Socket(int socket, struct sockaddr *addr) :
56 _socket(socket), _connected(true), _listening(false), _addr_set(true)
57{
58 char ip[46];
59 unsigned int port;
60 /* ... */
61 switch (addr->sa_family) {
62 case AF_INET:
63 inet_ntop(AF_INET, &(((struct sockaddr_in *) addr)->sin_addr), ip, sizeof(ip));
64 port = ntohs (((struct sockaddr_in *)addr)->sin_port);
65 break;
66 case AF_INET6:
67 inet_ntop(AF_INET6, &(((struct sockaddr_in6 *) addr)->sin6_addr), ip, sizeof(ip));
68 port = ntohs (((struct sockaddr_in6 *)addr)->sin6_port);
69 break;
70 default:
71 snprintf(ip, sizeof(ip), "UNKNOWN FAMILY: %d", addr->sa_family);
72 port = 0;
73 break;
74 }
75 _port = port;
76 _ip = ip;
77}
78
79void Socket::close()
80{
81 if (_connected) {
82 ::close(_socket);
83 _socket = 0;
84 _connected = false;
85 _listening = false;
86 }
87}
88
89void Socket::send(const string &str, int start, int end)
90{
91 string send_str = str.substr(start, end);
92 int bytes_written = write(_socket, send_str.c_str(), send_str.length());
93 if (bytes_written == -1) {
94 string err("socket failure, writing on stream socket");
95 const char* error_info = strerror(errno);
96 if (error_info) err += " " + (string) error_info;
97 throw BESInternalError(err, __FILE__, __LINE__);
98 }
99}
100
101int Socket::receive(char *inBuff, const int inSize)
102{
103 int bytesRead = 0;
104
105 //if ((bytesRead = read(_socket, inBuff, inSize)) < 1) {
106 // check for EINTR and EAGAIN. jhrg 10/30/13
107 errno = 0;
108 while ((bytesRead = read(_socket, inBuff, inSize)) < 1) {
109 if (errno == EINTR || errno == EAGAIN) {
110 // These codes are only returned when no bytes have been read, so
111 // there is no need to update the values of inSize or inBuff.
112 // jhrg 11/6/13
113 ERROR_LOG("Socket::receive: errno: " << strerror(errno) << ", bytesRead: " << bytesRead << endl);
114 errno = 0;
115 continue;
116 }
117
118 if (bytesRead < 0) { // Error
119 std::ostringstream oss;
120 oss << "Socket::receive: socket failure, reading on stream socket: " << strerror(errno) << ", bytesRead: "
121 << bytesRead;
122 throw BESInternalError(oss.str(), __FILE__, __LINE__);
123 }
124 else if (bytesRead == 0) // EOF
125 return 0;
126 }
127
128 return bytesRead;
129}
130
137void Socket::dump(ostream &strm) const
138{
139 strm << BESIndent::LMarg << "Socket::dump - (" << (void *) this << ")" << endl;
140 BESIndent::Indent();
141 strm << BESIndent::LMarg << "socket: " << _socket << endl;
142 strm << BESIndent::LMarg << "is connected? " << _connected << endl;
143 strm << BESIndent::LMarg << "is listening? " << _listening << endl;
144 strm << BESIndent::LMarg << "socket address set? " << _addr_set << endl;
145 if (_addr_set) {
146 strm << BESIndent::LMarg << "socket port: " << _port << endl;
147 strm << BESIndent::LMarg << "socket ip: " << _ip << endl;
148 }
149 BESIndent::UnIndent();
150}
151
exception thrown if internal error encountered
virtual void dump(std::ostream &strm) const
dumps information about this object
Definition: Socket.cc:137