Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Download

open-axiom repository from github

24005 views
1
/*
2
Copyright (C) 1991-2002, The Numerical Algorithms Group Ltd.
3
All rights reserved.
4
Copyright (C) 2007-2013, Gabriel Dos Reis
5
All rights reserved.
6
7
Redistribution and use in source and binary forms, with or without
8
modification, are permitted provided that the following conditions are
9
met:
10
11
- Redistributions of source code must retain the above copyright
12
notice, this list of conditions and the following disclaimer.
13
14
- Redistributions in binary form must reproduce the above copyright
15
notice, this list of conditions and the following disclaimer in
16
the documentation and/or other materials provided with the
17
distribution.
18
19
- Neither the name of The Numerical ALgorithms Group Ltd. nor the
20
names of its contributors may be used to endorse or promote products
21
derived from this software without specific prior written permission.
22
23
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
24
IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
25
TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
26
PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
27
OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
28
EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
29
PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
30
PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
31
LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
32
NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
33
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34
*/
35
36
#ifndef OPENAXIOM_SOCKIO_included
37
#define OPENAXIOM_SOCKIO_included
38
39
#include <stddef.h>
40
41
#ifdef __WIN32__
42
# include <winsock2.h>
43
# define OPENAXIOM_INVALID_SOCKET INVALID_SOCKET
44
#else
45
# include <sys/types.h>
46
# include <sys/socket.h>
47
# include <netinet/in.h>
48
# include <arpa/inet.h>
49
# include <sys/un.h>
50
# define OPENAXIOM_INVALID_SOCKET (-1)
51
#endif
52
53
#include "openaxiom-c-macros.h"
54
#include "open-axiom.h"
55
56
namespace OpenAxiom {
57
58
/* On Windows, a socket identifier is not a file descriptor. It is
59
represented by an integer type, but that integer type is not just
60
plain int as in the Unix world. It is an unsigned integer.
61
Consequently, we abstract over that variation, using the typedef
62
axiom_socket. */
63
64
#ifdef __WIN32__
65
typedef SOCKET openaxiom_socket;
66
typedef HANDLE openaxiom_filedesc;
67
#else
68
typedef int openaxiom_socket;
69
typedef int openaxiom_filedesc;
70
#endif
71
typedef int openaxiom_port;
72
73
struct openaxiom_sio {
74
openaxiom_socket socket; /* descriptor of this socket I/O endpoint. */
75
int type; /* socket type (AF_UNIX or AF_INET) */
76
int purpose; /* can be SessionManager, GraphicsServer, etc. */
77
int pid; /* process ID of connected socket */
78
int frame; /* spad interpreter frame. */
79
openaxiom_socket remote; /* descriptor of socket at the other endpoint. */
80
union {
81
struct sockaddr u_addr;
82
struct sockaddr_in i_addr;
83
} addr;
84
char *host_name; /* name of foreign host if type == AF_INET */
85
int nbytes_pending; /* pending bytes for read. */
86
};
87
88
89
90
OPENAXIOM_C_EXPORT openaxiom_filedesc
91
oa_open_local_client_stream_socket(const char*);
92
OPENAXIOM_C_EXPORT int oa_inet_pton(const char*, int, Byte*);
93
OPENAXIOM_C_EXPORT int oa_get_host_address(const char*, int, Byte*);
94
OPENAXIOM_C_EXPORT int oa_open_local_server_stream_socket(const char*);
95
OPENAXIOM_C_EXPORT openaxiom_socket
96
oa_connect_ip_port_stream(const Byte*, int, openaxiom_port);
97
98
OPENAXIOM_C_EXPORT int oa_socket_write(openaxiom_socket, const Byte*, int);
99
OPENAXIOM_C_EXPORT int oa_socket_write_byte(openaxiom_socket, Byte);
100
101
OPENAXIOM_C_EXPORT int oa_socket_read(openaxiom_socket, Byte*, int);
102
OPENAXIOM_C_EXPORT int oa_socket_read_byte(openaxiom_socket);
103
104
OPENAXIOM_C_EXPORT void oa_close_socket(openaxiom_socket);
105
106
OPENAXIOM_C_EXPORT int
107
oa_filedesc_write(openaxiom_filedesc, const Byte*, int);
108
OPENAXIOM_C_EXPORT int
109
oa_filedesc_read(openaxiom_filedesc, Byte*, int);
110
OPENAXIOM_C_EXPORT int oa_filedesc_close(openaxiom_filedesc);
111
112
OPENAXIOM_C_EXPORT int sread(openaxiom_sio*, Byte*, int, const char*);
113
OPENAXIOM_C_EXPORT int swrite(openaxiom_sio*, const Byte*, int,
114
const char*);
115
116
OPENAXIOM_C_EXPORT int wait_for_client_read(openaxiom_sio*, Byte*,
117
int, const char*);
118
OPENAXIOM_C_EXPORT int wait_for_client_write(openaxiom_sio*,
119
const Byte*, int,
120
const char*);
121
122
OPENAXIOM_C_EXPORT int make_server_name(char*, const char*);
123
OPENAXIOM_C_EXPORT int make_server_number();
124
OPENAXIOM_C_EXPORT openaxiom_sio* connect_to_local_server(const char*, int, int);
125
OPENAXIOM_C_EXPORT int open_server(const char*);
126
OPENAXIOM_C_EXPORT int accept_connection();
127
OPENAXIOM_C_EXPORT int sselect(int, fd_set*, fd_set*, fd_set*, void*);
128
OPENAXIOM_C_EXPORT void close_socket(openaxiom_socket, const char*);
129
130
OPENAXIOM_C_EXPORT int get_int(openaxiom_sio*);
131
OPENAXIOM_C_EXPORT double get_float(openaxiom_sio*);
132
OPENAXIOM_C_EXPORT double sock_get_float(int);
133
OPENAXIOM_C_EXPORT int get_sfloats(openaxiom_sio*, float*, int);
134
OPENAXIOM_C_EXPORT char* get_string(openaxiom_sio*);
135
OPENAXIOM_C_EXPORT void sigpipe_handler(int);
136
OPENAXIOM_C_EXPORT int fill_buf(openaxiom_sio*, Byte*, int,
137
const char*);
138
OPENAXIOM_C_EXPORT int sock_get_int(int);
139
OPENAXIOM_C_EXPORT int get_ints(openaxiom_sio*, int*, int);
140
OPENAXIOM_C_EXPORT int sock_get_ints(int, int*, int);
141
OPENAXIOM_C_EXPORT int send_int(openaxiom_sio*, int);
142
OPENAXIOM_C_EXPORT int sock_send_int(int, int);
143
OPENAXIOM_C_EXPORT int send_ints(openaxiom_sio*, const int*, int);
144
OPENAXIOM_C_EXPORT int sock_send_ints(int, const int*, int);
145
OPENAXIOM_C_EXPORT int send_string(openaxiom_sio*, const char*);
146
OPENAXIOM_C_EXPORT int send_string_len(openaxiom_sio*, const char*, int);
147
OPENAXIOM_C_EXPORT int sock_send_string(int, const char*);
148
OPENAXIOM_C_EXPORT int sock_send_string_len(int, const char*, int);
149
OPENAXIOM_C_EXPORT int send_strings(openaxiom_sio*, const char**, int);
150
OPENAXIOM_C_EXPORT int sock_send_strings(int, const char**, int);
151
OPENAXIOM_C_EXPORT char* sock_get_string(int);
152
OPENAXIOM_C_EXPORT char* get_string_buf(openaxiom_sio*, char*, int);
153
OPENAXIOM_C_EXPORT char* sock_get_string_buf(int, char*, int);
154
OPENAXIOM_C_EXPORT int get_strings(openaxiom_sio*, char**, int);
155
OPENAXIOM_C_EXPORT int sock_get_strings(int, char**, int);
156
OPENAXIOM_C_EXPORT int send_float(openaxiom_sio*, double);
157
OPENAXIOM_C_EXPORT int sock_send_float(int, double);
158
OPENAXIOM_C_EXPORT int send_sfloats(openaxiom_sio*, const float*, int);
159
OPENAXIOM_C_EXPORT int sock_send_sfloats(int, const float*, int);
160
OPENAXIOM_C_EXPORT int send_floats(openaxiom_sio*, const double*, int);
161
OPENAXIOM_C_EXPORT int sock_send_floats(int, const double*, int);
162
OPENAXIOM_C_EXPORT int sock_get_sfloats(int, float*, int);
163
OPENAXIOM_C_EXPORT int get_floats(openaxiom_sio*, double*, int);
164
OPENAXIOM_C_EXPORT int sock_get_floats(int, double*, int);
165
OPENAXIOM_C_EXPORT int wait_for_client_kill(openaxiom_sio*, int);
166
OPENAXIOM_C_EXPORT int sock_get_remote_fd(int);
167
OPENAXIOM_C_EXPORT int send_signal(openaxiom_sio*, int);
168
OPENAXIOM_C_EXPORT int sock_send_signal(int, int);
169
OPENAXIOM_C_EXPORT int send_wakeup(openaxiom_sio*);
170
OPENAXIOM_C_EXPORT int sock_send_wakeup(int);
171
OPENAXIOM_C_EXPORT void remote_stdio(openaxiom_sio*);
172
OPENAXIOM_C_EXPORT void get_socket_type(openaxiom_sio*);
173
OPENAXIOM_C_EXPORT int sock_accept_connection(int);
174
OPENAXIOM_C_EXPORT int server_switch();
175
176
#define MaxClients 150
177
178
/* possible socket types (purpose) */
179
180
#define SessionManager 1
181
#define ViewportServer 2
182
#define MenuServer 3
183
#define SessionIO 4
184
#define InterpWindow 5
185
#define KillSpad 6
186
187
#define Acknowledge 255
188
189
/* Timeout value for connection to remote socket */
190
191
#define Forever 0
192
193
/* Socket name for local AXIOM server and session manager */
194
195
#define SpadServer "/tmp/.d"
196
#define SessionServer "/tmp/.s"
197
#define SessionIOName "/tmp/.i"
198
#define MenuServerName "/tmp/.h"
199
#define ForkServerName "/tmp/.f"
200
201
202
#define MASK_SIZE (NBBY*sizeof(fd_set))
203
204
205
/* table of dedicated socket types */
206
207
OPENAXIOM_C_EXPORT openaxiom_sio *purpose_table[];
208
OPENAXIOM_C_EXPORT openaxiom_sio server;
209
OPENAXIOM_C_EXPORT openaxiom_sio clients[];
210
OPENAXIOM_C_EXPORT fd_set socket_mask;
211
OPENAXIOM_C_EXPORT fd_set server_mask;
212
213
}
214
215
#endif /* OPENAXIOM_SOCKIO_included */
216
217