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 148 149 150 151 152
|
/*
Copyright (C) 2000 Stefan Westerfeld
stefan@space.twc.de
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Library General Public
License as published by the Free Software Foundation; either
version 2 of the License, or (at your option) any later version.
This library 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.
You should have received a copy of the GNU Library General Public License
along with this library; see the file COPYING.LIB. If not, write to
the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
Boston, MA 02111-1307, USA.
*/
#include "tcpconnection.h"
#include "dispatcher.h"
#include <sys/types.h>
#include <sys/socket.h>
#include <stdio.h>
#define queue cqueue
#include <netdb.h>
#undef queue
#include <netinet/in.h>
#include <netinet/tcp.h>
#include <arpa/inet.h>
#include <sys/un.h>
#include <errno.h>
#include "debug.h"
#include <stdlib.h>
using namespace std;
using namespace Arts;
static struct sockaddr_in *parse_tcp_url(const char *url)
{
static struct sockaddr_in addr;
char *work = strdup(url);
char *type = strtok(work,":");
if(type == 0 || strcmp(type,"tcp") != 0) {
free(work);
return 0;
}
char *host = strtok(NULL,":");
if(host == 0) {
free(work);
return 0;
}
char *port = strtok(NULL,":\n");
if(port == 0) {
free(work);
return 0;
}
long portno = atol(port);
if(portno < 1 || portno > 65535) {
free(work);
return 0;
}
struct hostent *server = gethostbyname(host);
if(server == 0)
{
arts_warning("parse_tcp_url: unknown host '%s'",host);
free(work);
return 0;
}
memset((void *) &addr, '\0', sizeof(addr));
addr.sin_family = AF_INET;
addr.sin_addr.s_addr = *(u_long *)server->h_addr;
addr.sin_port = htons(portno);
free(work);
return &addr;
}
static int tcp_connect(const char *url)
{
struct sockaddr_in *remote_addr = parse_tcp_url(url);
if(remote_addr == 0)
{
arts_warning("tcp_connect: couldn't parse url %s",url);
return -1;
}
int my_socket = socket(AF_INET,SOCK_STREAM,0);
if(my_socket < 0)
{
arts_warning("tcp_connect: unable to open socket for read");
return -1;
}
struct linger lin;
lin.l_onoff=1; /* block a closing socket for 1 second */
lin.l_linger=100; /* if data is waiting to be sent */
if ( setsockopt( my_socket, SOL_SOCKET, SO_LINGER,
(char*)&lin, sizeof(struct linger) ) < 0 )
{
arts_warning("tcp_connect: unable to set socket linger value to %d",
lin.l_linger);
close(my_socket);
return -1;
}
#ifdef TCP_NODELAY
// enable TCP sending without nagle algorithm - this sends out requests
// faster, because they are not queued in the hope that more data will
// need to be sent soon
int on = 1;
if(setsockopt(my_socket, IPPROTO_TCP, TCP_NODELAY,
(char *)&on, sizeof(on)) < 0)
{
arts_debug("couldn't set TCP_NODELAY on socket %d\n", my_socket);
}
#endif
int rc;
rc=connect(my_socket,(struct sockaddr *)remote_addr, sizeof(*remote_addr));
if(rc != 0)
{
arts_warning("tcp_connect: can't connect to server (%s)", url);
close(my_socket);
return -1;
}
return my_socket;
}
TCPConnection::TCPConnection(const string& url)
{
fd = tcp_connect(url.c_str());
_broken = (fd == -1);
if(!_broken)
{
Dispatcher::the()->ioManager()->watchFD(fd,
IOType::read|IOType::except|IOType::reentrant,this);
initReceive();
}
}
|