From 28ebc94f1f9fbfb982aca287a11d4b3ad7d10b46 Mon Sep 17 00:00:00 2001 From: Vic Lee Date: Sun, 3 Jul 2011 16:37:36 +0800 Subject: [PATCH] libfreerdp-core/transport: implement sending. --- include/freerdp/utils/stream.h | 2 ++ libfreerdp-core/transport.c | 52 +++++++++++++++++++++++++++++++++- libfreerdp-core/transport.h | 1 + 3 files changed, 54 insertions(+), 1 deletion(-) diff --git a/include/freerdp/utils/stream.h b/include/freerdp/utils/stream.h index 167f9b027..3e636e5aa 100644 --- a/include/freerdp/utils/stream.h +++ b/include/freerdp/utils/stream.h @@ -45,6 +45,8 @@ stream_extend(STREAM * stream); #define stream_get_pos(_s) (_s->ptr - _s->buffer) #define stream_set_pos(_s,_m) _s->ptr = _s->buffer + (_m) #define stream_seek(_s,_offset) _s->ptr += (_offset) +#define stream_get_head(_s) _s->buffer +#define stream_get_tail(_s) _s->ptr #define stream_read_uint8(_s, _v) do { _v = *_s->ptr++; } while (0) #define stream_read_uint16(_s, _v) do { _v = \ diff --git a/libfreerdp-core/transport.c b/libfreerdp-core/transport.c index ac1b44601..a1e14c6fc 100644 --- a/libfreerdp-core/transport.c +++ b/libfreerdp-core/transport.c @@ -23,6 +23,8 @@ #include #include +#include +#include #include #include #include @@ -39,6 +41,10 @@ transport_new(void) transport->sockfd = -1; + /* a small 0.1ms delay when transport is blocking. */ + transport->ts.tv_sec = 0; + transport->ts.tv_nsec = 100000; + return transport; } @@ -145,10 +151,54 @@ transport_start_tls(rdpTransport * transport) return 0; } +static int +transport_delay(rdpTransport * transport) +{ + nanosleep(&transport->ts, NULL); + return 0; +} + +static int +transport_send_tls(rdpTransport * transport, STREAM * stream) +{ + return 0; +} + +static int +transport_send_tcp(rdpTransport * transport, STREAM * stream) +{ + uint8 * head; + uint8 * tail; + int r; + + head = stream_get_head(stream); + tail = stream_get_tail(stream); + while (head < tail) + { + r = send(transport->sockfd, head, tail - head, MSG_NOSIGNAL); + if (r < 0) + { + if (errno == EAGAIN || errno == EWOULDBLOCK) + { + if (transport_delay(transport) != 0) + return -1; + continue; + } + printf("transport_send_tcp: send (%d)\n", errno); + return -1; + } + head += r; + } + return 0; +} + int transport_send(rdpTransport * transport, STREAM * stream) { - return 0; + if (transport->tls) + return transport_send_tls(transport, stream); + else + return transport_send_tcp(transport, stream); } int diff --git a/libfreerdp-core/transport.h b/libfreerdp-core/transport.h index 6fa02cd4d..8fe62aef1 100644 --- a/libfreerdp-core/transport.h +++ b/libfreerdp-core/transport.h @@ -29,6 +29,7 @@ struct rdp_transport int sockfd; struct crypto_tls * tls; PacketReceivedCallback * recv_callback; + struct timespec ts; }; typedef struct rdp_transport rdpTransport;