-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfdpack.h
126 lines (110 loc) · 2.77 KB
/
fdpack.h
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
/*
* Copyright 2012+ Michal Soltys <[email protected]>
*
* This file is part of Yancat.
*
* Yancat is free software: you can redistribute it and/or modify it under the
* terms of the GNU General Public License as published by the Free Software
* Foundation, either version 3 of the License, or (at your option) any later
* version.
*
* Yancat 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 General Public License for more
* details.
*
* You should have received a copy of the GNU General Public License along with
* Yancat. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef __fdpack_h__
#define __fdpack_h__
#ifndef h_mingw
# include <netinet/in.h>
# define SOCKET int
# define INVALID_SOCKET -1
# define SOCKET_ERROR -1
# define SOCKET_FPF "%d"
#else
# include <winsock2.h>
# define SOCKET_FPF "%u"
#endif
#include "parse.h"
struct fdtype_s;
struct fdpack_s;
struct fdtype_s {
const char *kind;
void (*info)(struct fdpack_s *);
int (*open)(struct fdpack_s*);
int (*close)(struct fdpack_s*);
int (*dtor)(struct fdpack_s*);
ssize_t (*read)(struct fdpack_s *, void *, size_t);
ssize_t (*write)(struct fdpack_s *, const void *, size_t);
};
struct fdpack_s {
const struct fdtype_s *type;
int fd, dir, sync;
union {
struct {
char *path;
} f;
struct {
/*
* dumb fucking winsock2, we cannot use fd as int ...
* or rather we can, but better not ...
*/
SOCKET fds;
struct sockaddr_in saddr;
struct netpnt_s np;
int flags;
} s;
};
};
extern const struct fdtype_s _fdfd;
extern const struct fdtype_s _fdfile;
extern const struct fdtype_s _fdsock;
#if 0
void fd_info(struct fdpack_s *fd);
ssize_t fd_read(struct fdpack_s *fd, void *buf, size_t count);
ssize_t fd_write(struct fdpack_s *fd, const void *buf, size_t count);
int fd_open(struct fdpack_s *fd);
int fd_dtor(struct fdpack_s *fd);
#endif
int fd_ctor (struct fdpack_s* fd, int dir, const char *sd, int sync);
int fd_ctor_f(struct fdpack_s* fd, int dir, const char *path, int sync);
int fd_ctor_s(struct fdpack_s* fd, int dir, struct netpnt_s *a, int msgwait);
/*
* virtuals
*/
static inline void
fd_info(struct fdpack_s *fd)
{
fd->type->info(fd);
}
static inline ssize_t
fd_read(struct fdpack_s *fd, void *buf, size_t count)
{
return fd->type->read(fd, buf, count);
}
static inline ssize_t
fd_write(struct fdpack_s *fd, const void *buf, size_t count)
{
return fd->type->write(fd, buf, count);
}
static inline int
fd_open(struct fdpack_s *fd)
{
return fd->type->open(fd);
}
static inline int
fd_close(struct fdpack_s *fd)
{
return fd->type->close(fd);
}
static inline int
fd_dtor(struct fdpack_s *fd)
{
if (!fd)
return -1;
return fd->type->dtor(fd);
}
#endif