aboutsummaryrefslogtreecommitdiffstats
path: root/lib/erl_interface/src/misc/ei_portio.h
blob: a84b5ca09ca92e11ec19b7b84b31ace5072f98b5 (plain) (blame)
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
/*
 * %CopyrightBegin%
 * 
 * Copyright Ericsson AB 1996-2016. All Rights Reserved.
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * 
 * %CopyrightEnd%
 *

 */
#ifndef _EI_PORTIO_H
#define _EI_PORTIO_H

#undef EI_HAVE_STRUCT_IOVEC__
#if !defined(__WIN32__) && !defined(VXWORKS) && defined(HAVE_SYS_UIO_H)
/* Declaration of struct iovec *iov should be visible in this scope. */
#  include <sys/uio.h>
#  define EI_HAVE_STRUCT_IOVEC__
#endif

/*
 * Internal API. Should not be used outside of the erl_interface application...
 */

int ei_socket_ctx__(ei_socket_callbacks *cbs, void **ctx, void *setup);
int ei_close_ctx__(ei_socket_callbacks *cbs, void *ctx);
int ei_listen_ctx__(ei_socket_callbacks *cbs, void *ctx, void *adr, int *len, int backlog);
int ei_accept_ctx_t__(ei_socket_callbacks *cbs, void **ctx, void *addr, int *len, unsigned ms);
int ei_connect_ctx_t__(ei_socket_callbacks *cbs, void *ctx, void *addr, int len, unsigned ms);
int ei_read_fill_ctx__(ei_socket_callbacks *cbs, void *ctx, char* buf, ssize_t *len);
int ei_write_fill_ctx__(ei_socket_callbacks *cbs, void *ctx, const char *buf, ssize_t *len);
int ei_read_fill_ctx_t__(ei_socket_callbacks *cbs, void *ctx, char* buf, ssize_t *len, unsigned  ms);
int ei_write_fill_ctx_t__(ei_socket_callbacks *cbs, void *ctx, const char *buf, ssize_t *len, unsigned ms);
#if defined(EI_HAVE_STRUCT_IOVEC__)
int ei_writev_fill_ctx_t__(ei_socket_callbacks *cbs, void *ctx, const struct iovec *iov, int iovcnt, ssize_t *len, unsigned ms);
int ei_socket_callbacks_have_writev__(ei_socket_callbacks *cbs);
#endif

ei_socket_callbacks ei_default_socket_callbacks;

#define EI_FD_AS_CTX__(FD)                                              \
    ((void *) (long) (FD))

#define EI_DFLT_CTX_TO_FD__(CTX, FD)                                    \
    ((int) (long) (CTX) < 0                                             \
     ? EBADF                                                            \
     : (*(FD) = (int) (long) (CTX), 0))

#define EI_GET_FD__(CBS, CTX, FD)                                       \
    ((CBS) == &ei_default_socket_callbacks                              \
     ? EI_DFLT_CTX_TO_FD__((CTX), FD)                                   \
     : (CBS)->get_fd((CTX), (FD)))

extern int ei_plugin_socket_impl__;

#if !defined(_REENTRANT)

#define EI_HAVE_PLUGIN_SOCKET_IMPL__                                    \
    ei_plugin_socket_impl__
#define EI_SET_HAVE_PLUGIN_SOCKET_IMPL__                                \
    ei_plugin_socket_impl__ = 1

#elif ((ETHR_HAVE___atomic_load_n & SIZEOF_INT)                         \
       && (ETHR_HAVE___atomic_store_n & SIZEOF_INT))

#define EI_HAVE_PLUGIN_SOCKET_IMPL__                                    \
    __atomic_load_n(&ei_plugin_socket_impl__, __ATOMIC_ACQUIRE)
#define EI_SET_HAVE_PLUGIN_SOCKET_IMPL__                                \
    __atomic_store_n(&ei_plugin_socket_impl__, 1, __ATOMIC_RELEASE)

#else

/* No gcc atomics; always lookup using ei_get_cbs_ctx()... */
#define EI_HAVE_PLUGIN_SOCKET_IMPL__ 0
#define EI_SET_HAVE_PLUGIN_SOCKET_IMPL__ (void) 0

#endif

#define EI_GET_CBS_CTX__(CBS, CTX, FD)                                  \
    (EI_HAVE_PLUGIN_SOCKET_IMPL__                                       \
     ? ei_get_cbs_ctx__((CBS), (CTX), (FD))                             \
     : ((FD) < 0                                                        \
        ? EBADF                                                         \
        : (*(CBS) = &ei_default_socket_callbacks,                       \
           *(CTX) = EI_FD_AS_CTX__((FD)),                               \
           0)))
/*      
 * The following uses our own TCP/IPv4 socket implementation...
 */
int ei_socket__(int *fd);
int ei_close__(int fd);
int ei_listen__(int fd, void *adr, int *len, int backlog);
int ei_accept_t__(int *fd, void *addr, int *len, unsigned ms);
int ei_connect_t__(int fd, void *addr, int len, unsigned ms);
int ei_read_fill__(int fd, char* buf, ssize_t *len);
int ei_write_fill__(int fd, const char *buf, ssize_t *len);
int ei_read_fill_t__(int fd, char* buf, ssize_t *len, unsigned ms);
int ei_write_fill_t__(int fd, const char *buf, ssize_t *len, unsigned ms);
#if defined(EI_HAVE_STRUCT_IOVEC__) && defined(HAVE_WRITEV)
int ei_writev_fill_t__(int fd, const struct iovec *iov, int iovcnt, ssize_t *len, unsigned ms);
#endif

#endif /* _EI_PORTIO_H */