Commit 7a8bdb3e5edb1d7e4648c07f6cf7416ad4f8b57a

Authored by Georg Hopp
1 parent 9ff3d493

Add a way to create TR_Socket socket pairs

... ... @@ -150,6 +150,8 @@ TR_TcpSocket TR_socketAccept(TR_TcpSocket);
150 150 #define TR_socketOpen(socket) \
151 151 (TR_socketInit((socket), NULL))
152 152
  153 +void TR_socketPair(TR_Socket[2], int);
  154 +
153 155 #endif // __TR_SOCKET_H__
154 156
155 157 // vim: set ts=4 sw=4:
... ...
... ... @@ -12,6 +12,7 @@ TRIO = stream.c \
12 12 socket_accept.c \
13 13 socket_nonblock.c \
14 14 socket_close.c \
  15 + socket_pair.c \
15 16 socket_shutdown.c \
16 17 socket_shutdown_read.c \
17 18 socket_shutdown_write.c \
... ...
  1 +/**
  2 + * \file
  3 + *
  4 + * \author Georg Hopp
  5 + *
  6 + * \copyright
  7 + * Copyright © 2014 Georg Hopp
  8 + *
  9 + * This program is free software: you can redistribute it and/or modify
  10 + * it under the terms of the GNU General Public License as published by
  11 + * the Free Software Foundation, either version 3 of the License, or
  12 + * (at your option) any later version.
  13 + *
  14 + * This program is distributed in the hope that it will be useful,
  15 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17 + * GNU General Public License for more details.
  18 + *
  19 + * You should have received a copy of the GNU General Public License
  20 + * along with this program. If not, see <http://www.gnu.org/licenses/>.
  21 + */
  22 +
  23 +//#include <errno.h>
  24 +//#include <stdlib.h>
  25 +//#include <unistd.h>
  26 +//#include <stdio.h>
  27 +//#include <fcntl.h>
  28 +
  29 +#include <sys/types.h>
  30 +#include <sys/socket.h>
  31 +#include <netdb.h>
  32 +
  33 +#include "tr/socket.h"
  34 +#include "trbase.h"
  35 +
  36 +void
  37 +TR_socketPair(TR_Socket sockets[2], int type)
  38 +{
  39 + int handles[2];
  40 +
  41 + if (0 > socketpair(AF_UNIX, SOCK_DGRAM, 0, handles)) {
  42 + TR_loggerLog(
  43 + TR_logger,
  44 + TR_LOGGER_ERR,
  45 + "Unable to create socket pair");
  46 + return;
  47 + }
  48 +
  49 + sockets[0] = TR_new(TR_Socket, NULL, NULL, 0, 0);
  50 + sockets[1] = TR_new(TR_Socket, NULL, NULL, 0, 0);
  51 + TR_socketType(sockets[0]) = type;
  52 + TR_socketType(sockets[1]) = type;
  53 + TR_socketHandle(sockets[0]) = handles[0];
  54 + TR_socketHandle(sockets[1]) = handles[1];
  55 +}
  56 +
  57 +// vim: set ts=4 sw=4:
... ...
... ... @@ -55,7 +55,7 @@ udpSocketRecv(void * _this, size_t size)
55 55 ssize_t received;
56 56 TR_RemoteData rdata;
57 57 TR_Socket remote = TR_new(TR_UdpSocket, this->log, NULL, 0, 0);
58   -
  58 +
59 59 size = size>TR_UDP_MAX_READ_BLOCK ? TR_UDP_MAX_READ_BLOCK : size;
60 60
61 61 unsigned char buffer[size];
... ...
Please register or login to post a comment