2 * Copyright (C) 2013 Martin Willi
3 * Copyright (C) 2013 revosec AG
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License as published by the
7 * Free Software Foundation; either version 2 of the License, or (at your
8 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
12 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
20 typedef struct private_stream_t private_stream_t
;
23 * Private data of an stream_t object.
25 struct private_stream_t
{
28 * Public stream_t interface.
38 METHOD(stream_t
, read_
, ssize_t
,
39 private_stream_t
*this, void *buf
, size_t len
, bool block
)
47 ret
= read(this->fd
, buf
, len
);
51 ret
= recv(this->fd
, buf
, len
, MSG_DONTWAIT
);
52 if (ret
== -1 && errno
== EAGAIN
)
54 /* unify EGAIN and EWOULDBLOCK */
58 if (ret
== -1 && errno
== EINTR
)
59 { /* interrupted, try again */
66 METHOD(stream_t
, write_
, ssize_t
,
67 private_stream_t
*this, void *buf
, size_t len
, bool block
)
75 ret
= write(this->fd
, buf
, len
);
79 ret
= send(this->fd
, buf
, len
, MSG_DONTWAIT
);
80 if (ret
== -1 && errno
== EAGAIN
)
82 /* unify EGAIN and EWOULDBLOCK */
86 if (ret
== -1 && errno
== EINTR
)
87 { /* interrupted, try again */
94 METHOD(stream_t
, destroy
, void,
95 private_stream_t
*this)
104 stream_t
*stream_create_from_fd(int fd
)
106 private_stream_t
*this;
117 return &this->public;