| 1 |
////////////////////////////////////////////////////////////////////////////// |
| 2 |
////////////////////////////////////////////////////////////////////////////// |
| 3 |
// |
| 4 |
// copyright : (C) 2014 Eran Ifrah |
| 5 |
// file name : clSocketServer.cpp |
| 6 |
// |
| 7 |
// ------------------------------------------------------------------------- |
| 8 |
// A |
| 9 |
// _____ _ _ _ _ |
| 10 |
// / __ \ | | | | (_) | |
| 11 |
// | / \/ ___ __| | ___| | _| |_ ___ |
| 12 |
// | | / _ \ / _ |/ _ \ | | | __/ _ ) |
| 13 |
// | \__/\ (_) | (_| | __/ |___| | || __/ |
| 14 |
// \____/\___/ \__,_|\___\_____/_|\__\___| |
| 15 |
// |
| 16 |
// F i l e |
| 17 |
// |
| 18 |
// This program is free software; you can redistribute it and/or modify |
| 19 |
// it under the terms of the GNU General Public License as published by |
| 20 |
// the Free Software Foundation; either version 2 of the License, or |
| 21 |
// (at your option) any later version. |
| 22 |
// |
| 23 |
////////////////////////////////////////////////////////////////////////////// |
| 24 |
////////////////////////////////////////////////////////////////////////////// |
| 25 |
|
| 26 |
#include "clConnectionString.h" |
| 27 |
#include "clSocketServer.h" |
| 28 |
|
| 29 |
#ifndef _WIN32 |
| 30 |
#include <arpa/inet.h> |
| 31 |
#include <netinet/in.h> |
| 32 |
#include <stdio.h> |
| 33 |
#include <sys/socket.h> |
| 34 |
#include <sys/stat.h> |
| 35 |
#include <sys/types.h> |
| 36 |
#include <sys/un.h> |
| 37 |
#include <unistd.h> |
| 38 |
#endif |
| 39 |
|
| 40 |
clSocketServer::clSocketServer() {} |
| 41 |
|
| 42 |
clSocketServer::~clSocketServer() { DestroySocket(); } |
| 43 |
|
| 44 |
int clSocketServer::CreateServer(const std::string& pipePath) |
| 45 |
{ |
| 46 |
#ifndef __WXMSW__ |
| 47 |
unlink(pipePath.c_str()); |
| 48 |
|
| 49 |
// Create a socket |
| 50 |
if((m_socket = ::socket(AF_UNIX, SOCK_STREAM, 0)) == INVALID_SOCKET) { |
| 51 |
throw clSocketException("Could not create socket: " + error()); |
| 52 |
} |
| 53 |
|
| 54 |
// must set reuse-address |
| 55 |
int optval; |
| 56 |
|
| 57 |
// set SO_REUSEADDR on a socket to true (1): |
| 58 |
optval = 1; |
| 59 |
::setsockopt(m_socket, SOL_SOCKET, SO_REUSEADDR, (const char*)&optval, sizeof(optval)); |
| 60 |
|
| 61 |
// Prepare the sockaddr_in structure |
| 62 |
struct sockaddr_un server; |
| 63 |
server.sun_family = AF_UNIX; |
| 64 |
strcpy(server.sun_path, pipePath.c_str()); |
| 65 |
|
| 66 |
// Bind |
| 67 |
if(::bind(m_socket, (struct sockaddr*)&server, sizeof(server)) == -1) { |
| 68 |
throw clSocketException("CreateServer: bind operation failed: " + error()); |
| 69 |
} |
| 70 |
|
| 71 |
char mode[] = "0777"; |
| 72 |
int newMode = ::strtol(mode, 0, 8); |
| 73 |
::chmod(pipePath.c_str(), newMode); |
| 74 |
|
| 75 |
// define the accept queue size |
| 76 |
::listen(m_socket, 10); |
| 77 |
return 0; |
| 78 |
#else |
| 79 |
int port = ::atoi(pipePath.c_str()); |
| 80 |
return CreateServer("127.0.0.1", port); |
| 81 |
#endif |
| 82 |
} |
| 83 |
|
| 84 |
int clSocketServer::CreateServer(const std::string& address, int port) |
| 85 |
{ |
| 86 |
// Create a socket |
| 87 |
if((m_socket = ::socket(AF_INET, SOCK_STREAM, 0)) == INVALID_SOCKET) { |
| 88 |
throw clSocketException("Could not create socket: " + error()); |
| 89 |
} |
| 90 |
|
| 91 |
// must set reuse-address |
| 92 |
int optval; |
| 93 |
|
| 94 |
// set SO_REUSEADDR on a socket to true (1): |
| 95 |
optval = 1; |
| 96 |
::setsockopt(m_socket, SOL_SOCKET, SO_REUSEADDR, (const char*)&optval, sizeof(optval)); |
| 97 |
|
| 98 |
// Prepare the sockaddr_in structure |
| 99 |
struct sockaddr_in server; |
| 100 |
server.sin_family = AF_INET; |
| 101 |
#ifdef __WXMSW__ |
| 102 |
server.sin_addr.s_addr = inet_addr(address.c_str()); |
| 103 |
#else |
| 104 |
inet_pton(AF_INET, address.c_str(), &server.sin_addr); |
| 105 |
#endif |
| 106 |
server.sin_port = htons(port); |
| 107 |
|
| 108 |
// Bind |
| 109 |
if(::bind(m_socket, (struct sockaddr*)&server, sizeof(server)) != 0) { |
| 110 |
throw clSocketException("CreateServer: bind() error: " + error()); |
| 111 |
} |
| 112 |
|
| 113 |
if(port == 0) { |
| 114 |
struct sockaddr_in socket_name; |
| 115 |
#ifdef __WXMSW__ |
| 116 |
int name_len = sizeof(socket_name); |
| 117 |
#else |
| 118 |
socklen_t name_len = sizeof(socket_name); |
| 119 |
#endif |
| 120 |
if(::getsockname(m_socket, (struct sockaddr*)&socket_name, &name_len) != 0) { |
| 121 |
throw clSocketException("CreateServer: getsockname() error: " + error()); |
| 122 |
} |
| 123 |
port = ntohs(socket_name.sin_port); |
| 124 |
} |
| 125 |
// define the accept queue size |
| 126 |
if(::listen(m_socket, 10) != 0) { throw clSocketException("CreateServer: listen() error: " + error()); } |
| 127 |
|
| 128 |
// return the bound port number |
| 129 |
return port; |
| 130 |
} |
| 131 |
|
| 132 |
int clSocketServer::Start(const wxString& connectionString) |
| 133 |
{ |
| 134 |
clConnectionString cs(connectionString); |
| 135 |
if(!cs.IsOK()) { throw clSocketException("Invalid connection string provided"); } |
| 136 |
if(cs.GetProtocol() == clConnectionString::kTcp) { |
| 137 |
return CreateServer(cs.GetHost().mb_str(wxConvUTF8).data(), cs.GetPort()); |
| 138 |
} else { |
| 139 |
return CreateServer(cs.GetPath().mb_str(wxConvUTF8).data()); |
| 140 |
} |
| 141 |
} |
| 142 |
|
| 143 |
clSocketBase::Ptr_t clSocketServer::WaitForNewConnection(long timeout) |
| 144 |
{ |
| 145 |
return clSocketBase::Ptr_t(WaitForNewConnectionRaw(timeout)); |
| 146 |
} |
| 147 |
|
| 148 |
clSocketBase* clSocketServer::WaitForNewConnectionRaw(long timeout) |
| 149 |
{ |
| 150 |
if(SelectRead(timeout) == kTimeout) { return NULL; } |
| 151 |
int fd = ::accept(m_socket, 0, 0); |
| 152 |
if(fd < 0) { throw clSocketException("accept error: " + error()); } |
| 153 |
return new clSocketBase(fd); |
| 154 |
} |