The R Project SVN R

Rev

Rev 59198 | Rev 68949 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 59198 Rev 60531
Line 44... Line 44...
44
#ifdef HAVE_UNISTD_H
44
#ifdef HAVE_UNISTD_H
45
#include <unistd.h>
45
#include <unistd.h>
46
#endif
46
#endif
47
#include "sock.h"
47
#include "sock.h"
48
 
48
 
-
 
49
#include <R_ext/Print.h> // for REprintf
-
 
50
 
49
static int sock_inited = 0;
51
static int sock_inited = 0;
50
 
52
 
-
 
53
static struct Sock_error_t perr;
51
 
54
 
52
static int enter_sock(int fd)
55
static int enter_sock(int fd)
53
{
56
{
54
#ifdef DEBUG
57
#ifdef DEBUG
55
    printf("enter_sock(%d)\n", fd);
58
    printf("enter_sock(%d)\n", fd);
Line 57... Line 60...
57
    if (fd == -1) return 0; else return fd;
60
    if (fd == -1) return 0; else return fd;
58
}
61
}
59
 
62
 
60
static int close_sock(int fd)
63
static int close_sock(int fd)
61
{
64
{
-
 
65
    perr.error = 0;
62
    return Sock_close(fd, NULL) == -1 ? 0 : 1 ;
66
    int res = Sock_close(fd, &perr);
-
 
67
    if (res == -1) {
-
 
68
	REprintf("socket error: %s\n", strerror(perr.error));
-
 
69
	return -1;
-
 
70
    }
-
 
71
    return 0;
63
}
72
}
64
 
73
 
65
static void check_init(void)
74
static void check_init(void)
66
{
75
{
67
    if (! sock_inited) {
76
    if (! sock_inited) {
Line 74... Line 83...
74
}
83
}
75
 
84
 
76
void in_Rsockopen(int *port)
85
void in_Rsockopen(int *port)
77
{
86
{
78
    check_init();
87
    check_init();
-
 
88
    perr.error = 0;
79
    *port = enter_sock(Sock_open((Sock_port_t)*port, NULL));
89
    *port = enter_sock(Sock_open((Sock_port_t)*port, &perr));
-
 
90
    if(perr.error) REprintf("socket error: %s\n", strerror(perr.error));
80
}
91
}
81
 
92
 
82
void in_Rsocklisten(int *sockp, char **buf, int *len)
93
void in_Rsocklisten(int *sockp, char **buf, int *len)
83
{
94
{
84
    check_init();
95
    check_init();
-
 
96
    perr.error = 0;
85
    *sockp = enter_sock(Sock_listen(*sockp, *buf , *len, NULL));
97
    *sockp = enter_sock(Sock_listen(*sockp, *buf , *len, &perr));
-
 
98
    if(perr.error) REprintf("socket error: %s\n", strerror(perr.error));
86
}
99
}
87
 
100
 
88
void in_Rsockconnect(int *port, char **host)
101
void in_Rsockconnect(int *port, char **host)
89
{
102
{
90
    check_init();
103
    check_init();
91
#ifdef DEBUG
104
#ifdef DEBUG
92
    printf("connect to %d at %s\n",*port, *host);
105
    printf("connect to %d at %s\n",*port, *host);
93
#endif
106
#endif
-
 
107
    perr.error = perr.h_error = 0;
94
    *port = enter_sock(Sock_connect((Sock_port_t)*port, *host, NULL));
108
    *port = enter_sock(Sock_connect((Sock_port_t)*port, *host, &perr));
-
 
109
//    if(perr.h_error) REprintf("host lookup error: %s\n", hstrerror(perr.h_error));
-
 
110
    if(perr.error)
-
 
111
	REprintf("socket error: %s\n", strerror(perr.error));
95
}
112
}
96
 
113
 
97
void in_Rsockclose(int *sockp)
114
void in_Rsockclose(int *sockp)
98
{
115
{
99
    *sockp = close_sock(*sockp);
116
    *sockp = close_sock(*sockp);
Line 103... Line 120...
103
{
120
{
104
    check_init();
121
    check_init();
105
#ifdef DEBUG
122
#ifdef DEBUG
106
    printf("Reading from %d\n",*sockp);
123
    printf("Reading from %d\n",*sockp);
107
#endif
124
#endif
-
 
125
    perr.error = 0;
108
    *maxlen = (int) Sock_read(*sockp, *buf, *maxlen, NULL);
126
    *maxlen = (int) Sock_read(*sockp, *buf, *maxlen, &perr);
-
 
127
    if(perr.error) REprintf("socket error: %s\n", strerror(perr.error));
109
}
128
}
110
 
129
 
111
void in_Rsockwrite(int *sockp, char **buf, int *start, int *end, int *len)
130
void in_Rsockwrite(int *sockp, char **buf, int *start, int *end, int *len)
112
{
131
{
113
    ssize_t n;
132
    ssize_t n;
114
    if (*end > *len)
133
    if (*end > *len)
115
	*end = *len;
134
	*end = *len;
116
    if (*start < 0)
135
    if (*start < 0)
117
	*start = 0;
136
	*start = 0;
118
    if (*end < *start){
137
    if (*end < *start) {
119
	*len = -1;
138
	*len = -1;
120
	return;
139
	return;
121
    }
140
    }
122
    check_init();
141
    check_init();
123
#ifdef DEBUG
142
#ifdef DEBUG
124
    printf("writing %s to %d", *buf, *sockp);
143
    printf("writing %s to %d", *buf, *sockp);
125
#endif
144
#endif
-
 
145
    perr.error = 0;
126
    n = Sock_write(*sockp, *buf + *start, *end - *start, NULL);
146
    n = Sock_write(*sockp, *buf + *start, *end - *start, &perr);
127
    *len = (int) n;
147
    *len = (int) n;
-
 
148
    if(perr.error) REprintf("socket error: %s\n", strerror(perr.error));
128
}
149
}
129
 
150
 
130
/* --------- for use in socket connections ---------- */
151
/* --------- for use in socket connections ---------- */
131
 
152
 
132
#ifdef HAVE_SOCKETS
153
#ifdef HAVE_SOCKETS