1 /* gs_fd_net_driver.c */
3 #include "DataDesc/gs_private.h"
5 struct s_gs_fd_net_driver {
10 struct s_gs_fd_connection {
15 struct s_gs_net_driver_ops *net_driver_ops = NULL;
18 struct s_gs_connection_ops *connection_ops = NULL;
20 struct s_gs_net_driver_ops *
21 gs_fd_net_driver(void) {
22 if (!connection_ops) {
23 connection_ops = calloc(1, sizeof(struct s_gs_connection_ops));
25 connection_ops->_init = gs_fd_connection__init;
26 connection_ops->_exit = gs_fd_connection__exit;
27 connection_ops->write = gs_fd_connection_write;
28 connection_ops->read = gs_fd_connection_read;
29 connection_ops->flush = gs_fd_connection_flush;
32 if (!net_driver_ops) {
33 net_driver_ops = calloc(1, sizeof(struct s_gs_net_driver_ops));
35 net_driver_ops->_init = gs_fd__init;
36 net_driver_ops->_exit = gs_fd__exit;
39 return net_driver_ops;
43 gs_fd__init(struct s_gs_net_driver *p_net_driver) {
45 struct s_gs_fd_net_driver *p_fd = NULL;
47 p_fd = calloc(1, sizeof(struct s_gs_fd_net_driver));
48 p_net_driver->connection_ops = connection_ops;
49 p_net_driver->specific = p_fd;
53 gs_fd__exit(struct s_gs_net_driver *p_net_driver) {
55 struct s_gs_fd_net_driver *p_fd = p_net_driver->specific;
59 p_net_driver->specific = NULL;
64 gs_fd_connection__init(struct s_gs_connection *p_connection,
67 struct s_gs_fd_net_connection_arg *p_fd_arg = arg;
68 struct s_gs_fd_connection *p_fd_cnx = NULL;
70 p_fd_cnx = calloc(1, sizeof(struct s_gs_fd_connection));
71 p_fd_cnx->fd = p_fd_arg->fd;
72 p_connection->specific = p_fd_cnx;
76 gs_fd_connection__exit(struct s_gs_connection *p_connection) {
77 struct s_gs_fd_connection *p_fd_cnx = p_connection->specific;
81 p_connection->specific = NULL;
85 gs_fd_connection_write(struct s_gs_connection *p_connection,
88 struct s_gs_fd_connection *p_fd_cnx = p_connection->specific;
89 unsigned char *ptr = _ptr;
91 if (p_connection->direction != e_gs_connection_direction_outgoing)
92 GS_FAILURE("invalid operation");
97 status = write(p_fd_cnx->fd, ptr, (size_t)length);
98 fprintf(stderr, "write(%d, %p, %ld);\n", p_fd_cnx->fd, ptr, length);
102 GS_FAILURE("system call failed");
109 GS_FAILURE("file descriptor closed");
115 gs_fd_connection_read(struct s_gs_connection *p_connection,
118 struct s_gs_fd_connection *p_fd_cnx = p_connection->specific;
119 unsigned char *ptr = _ptr;
121 if (p_connection->direction != e_gs_connection_direction_incoming)
122 GS_FAILURE("invalid operation");
127 status = read(p_fd_cnx->fd, ptr, (size_t)length);
128 fprintf(stderr, "read(%d, %p, %ld);\n", p_fd_cnx->fd, ptr, length);
132 GS_FAILURE("system call failed");
139 GS_FAILURE("file descriptor closed");
144 gs_fd_connection_flush(struct s_gs_connection *p_connection) {