1 /* Copyright (C) 2014 InfiniDB, Inc.
2
3 This program is free software; you can redistribute it and/or
4 modify it under the terms of the GNU General Public License
5 as published by the Free Software Foundation; version 2 of
6 the License.
7
8 This program is distributed in the hope that it will be useful,
9 but WITHOUT ANY WARRANTY; without even the implied warranty of
10 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 GNU General Public License for more details.
12
13 You should have received a copy of the GNU General Public License
14 along with this program; if not, write to the Free Software
15 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
16 MA 02110-1301, USA. */
17
18 /***********************************************************************
19 * $Id: iosocket.cpp 3632 2013-03-13 18:08:46Z pleblanc $
20 *
21 *
22 ***********************************************************************/
23 #include <string>
24 #include <sstream>
25 #include <stdexcept>
26 #include <cstring>
27 using namespace std;
28
29 #include <sys/types.h>
30 #ifdef _MSC_VER
31 #include <winsock2.h>
32 #include <ws2tcpip.h>
33 #include <stdio.h>
34 #else
35 #include <arpa/inet.h>
36 #include <sys/socket.h>
37 #endif
38
39 #define IOSOCKET_DLLEXPORT
40 #include "iosocket.h"
41 #undef IOSOCKET_DLLEXPORT
42
43 namespace messageqcpp
44 {
45
IOSocket(Socket * socket)46 IOSocket::IOSocket(Socket* socket) :
47 fSocket(socket), sockID(0)
48 {
49 memset(&fSa, 0, sizeof(fSa));
50 }
51
~IOSocket()52 IOSocket::~IOSocket()
53 {
54 delete fSocket;
55 }
56
doCopy(const IOSocket & rhs)57 void IOSocket::doCopy(const IOSocket& rhs)
58 {
59 fSocket = rhs.fSocket->clone();
60 fSa = rhs.fSa;
61 sockID = rhs.sockID;
62 }
63
IOSocket(const IOSocket & rhs)64 IOSocket::IOSocket(const IOSocket& rhs)
65 {
66 doCopy(rhs);
67 }
68
operator =(const IOSocket & rhs)69 IOSocket& IOSocket::operator=(const IOSocket& rhs)
70 {
71 if (this != &rhs)
72 {
73 delete fSocket;
74 doCopy(rhs);
75 }
76
77 return *this;
78 }
79
toString() const80 const string IOSocket::toString() const
81 {
82 #ifdef NOSSTREAM
83 return "IOSocket";
84 #else
85 ostringstream oss;
86 char buf[INET_ADDRSTRLEN];
87 SocketParms sp = fSocket->socketParms();
88 const sockaddr_in* sinp = reinterpret_cast<const sockaddr_in*>(&fSa);
89 oss << "IOSocket: sd: " << sp.sd() <<
90 #ifndef _MSC_VER
91 " inet: " << inet_ntop(AF_INET, &sinp->sin_addr, buf, INET_ADDRSTRLEN) <<
92 #endif
93 " port: " << ntohs(sinp->sin_port);
94 return oss.str();
95 #endif
96 }
97
98 } //namespace messageqcpp
99
100