aboutsummaryrefslogtreecommitdiff
path: root/src/include/sockio.h
blob: aaa1c5f5ac27c419e8d96d59f2c9be9a08c901ca (plain)
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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
/*
  Copyright (C) 1991-2002, The Numerical Algorithms Group Ltd.
  All rights reserved.
  Copyright (C) 2007-2008, Gabriel Dos Reis
  All rights reserved.

  Redistribution and use in source and binary forms, with or without
  modification, are permitted provided that the following conditions are
  met:

      - Redistributions of source code must retain the above copyright
        notice, this list of conditions and the following disclaimer.

      - Redistributions in binary form must reproduce the above copyright
       notice, this list of conditions and the following disclaimer in
        the documentation and/or other materials provided with the
        distribution.

      - Neither the name of The Numerical ALgorithms Group Ltd. nor the
        names of its contributors may be used to endorse or promote products
        derived from this software without specific prior written permission.

  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
  IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
  TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
  PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
  OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

#ifndef OPENAXIOM_SOCKIO_included
#define OPENAXIOM_SOCKIO_included


#ifdef __MINGW32__
#  include <winsock2.h>
#else
#  include <sys/types.h>
#  include <sys/socket.h>
#  include <netinet/in.h>
#endif

#include "axiom-c-macros.h"
#include "open-axiom.h"

/* On Windows, a socket identifier is not a file descriptor.  It is
   represented by an integer type, but that integer type is not just
   plain int as in the Unix world.  It is an unsigned integer.
   Consequently, we abstract over that variation, using the typedef
   axiom_socket.  */

#ifdef __MINGW32__
typedef SOCKET axiom_socket;
#else
typedef int axiom_socket;
#endif


/* Close a socket communication endpoint.  */
extern void axiom_close_socket(axiom_socket);

typedef struct {
  axiom_socket socket;  /* socket number returned by "socket" call */
  int type;             /* socket type (AF_UNIX or AF_INET) */
  int purpose;          /* can be SessionManager, GraphicsServer, etc. */
  int pid;              /* process ID of connected socket */
  int frame;            /* spad interpreter frame (for interpreter windows) */
  axiom_socket remote;  /* file descriptor of remote socket */
  union {
    struct sockaddr u_addr;
    struct sockaddr_in i_addr;
  } addr;
  char *host_name;      /* name of foreign host if type == AF_INET */
} Sock;


OPENAXIOM_EXPORT int get_int(Sock*);
OPENAXIOM_EXPORT char* get_string(Sock*);
OPENAXIOM_EXPORT double get_float(Sock*);
OPENAXIOM_EXPORT Sock* connect_to_local_server(char*, int, int);
OPENAXIOM_EXPORT int sread(Sock*, char*, int, char*);
OPENAXIOM_EXPORT double plus_infinity(void);
OPENAXIOM_EXPORT double minus_infinity(void);
OPENAXIOM_EXPORT double NANQ(void);
OPENAXIOM_EXPORT void sigpipe_handler(int);
OPENAXIOM_EXPORT int wait_for_client_read(Sock*, char*, int, char*);
OPENAXIOM_EXPORT int wait_for_client_write(Sock*, char*, int, char*);
OPENAXIOM_EXPORT int swrite(Sock*, char*, int, char*);
OPENAXIOM_EXPORT int sselect(int, fd_set*, fd_set*, fd_set*, void*);
OPENAXIOM_EXPORT int fill_buf(Sock*, char*, int, char*);
OPENAXIOM_EXPORT int sock_get_int(int);
OPENAXIOM_EXPORT int get_ints(Sock*, int*, int);
OPENAXIOM_EXPORT int sock_get_ints(int, int*, int);
OPENAXIOM_EXPORT int send_int(Sock*, int);
OPENAXIOM_EXPORT int sock_send_int(int, int);
OPENAXIOM_EXPORT int send_ints(Sock*, int*, int);
OPENAXIOM_EXPORT int sock_send_ints(int, int*, int);
OPENAXIOM_EXPORT int send_string(Sock*, char*);
OPENAXIOM_EXPORT int send_string_len(Sock*, char*, int);
OPENAXIOM_EXPORT int sock_send_string(int, char*);
OPENAXIOM_EXPORT int sock_send_string_len(int, char*, int);
OPENAXIOM_EXPORT int send_strings(Sock*, char**, int);
OPENAXIOM_EXPORT int sock_send_strings(int, char**, int);
OPENAXIOM_EXPORT char* sock_get_string(int);
OPENAXIOM_EXPORT char* get_string_buf(Sock*, char*, int);
OPENAXIOM_EXPORT char* sock_get_string_buf(int, char*, int);
OPENAXIOM_EXPORT int get_strings(Sock*, char**, int);
OPENAXIOM_EXPORT int sock_get_strings(int, char**, int);
OPENAXIOM_EXPORT int send_float(Sock*, double);
OPENAXIOM_EXPORT int sock_send_float(int, double);
OPENAXIOM_EXPORT int send_sfloats(Sock*, float*, int);
OPENAXIOM_EXPORT int sock_send_sfloats(int, float*, int);
OPENAXIOM_EXPORT int send_floats(Sock*, double*, int);
OPENAXIOM_EXPORT int sock_send_floats(int, double*, int);
OPENAXIOM_EXPORT double sock_get_float(int);
OPENAXIOM_EXPORT int get_sfloats(Sock*, float*, int);
OPENAXIOM_EXPORT int sock_get_sfloats(int, float*, int);
OPENAXIOM_EXPORT int get_floats(Sock*, double*, int);
OPENAXIOM_EXPORT int sock_get_floats(int, double*, int);
OPENAXIOM_EXPORT int wait_for_client_kill(Sock*, int);
OPENAXIOM_EXPORT int sock_get_remote_fd(int);
OPENAXIOM_EXPORT int send_signal(Sock*, int);
OPENAXIOM_EXPORT int sock_send_signal(int, int);
OPENAXIOM_EXPORT int send_wakeup(Sock*);
OPENAXIOM_EXPORT int sock_send_wakeup(int);
OPENAXIOM_EXPORT Sock* connect_to_local_server_new(char*, int, int);
OPENAXIOM_EXPORT void remote_stdio(Sock*);
OPENAXIOM_EXPORT void init_purpose_table(void);
OPENAXIOM_EXPORT int make_server_number(void);
OPENAXIOM_EXPORT void close_socket(axiom_socket, char*);
OPENAXIOM_EXPORT int make_server_name(char*, char*);
OPENAXIOM_EXPORT int open_server(char*);
OPENAXIOM_EXPORT int accept_connection(Sock*);
OPENAXIOM_EXPORT void get_socket_type(Sock*);
OPENAXIOM_EXPORT int sock_accept_connection(int);
OPENAXIOM_EXPORT void redirect_stdio(Sock*);
OPENAXIOM_EXPORT void init_socks(void);
OPENAXIOM_EXPORT int server_switch(void);
OPENAXIOM_EXPORT void flush_stdout(void);
OPENAXIOM_EXPORT void print_line(char*);


#define MaxClients      150

/* possible socket types (purpose) */

#define SessionManager  1
#define ViewportServer  2
#define MenuServer      3
#define SessionIO       4
#define BaloonServer    5
#define InterpWindow    6
#define KillSpad        7
#define DebugWindow     8  /* used for nagman */
#define Forker          9
#define AV              10 /*Simon's algebraic viewer */

#define Acknowledge     255

/* Timeout value for connection to remote socket */

#define Forever 0

/* Socket name for local AXIOM server and session manager */

#define SpadServer              "/tmp/.d"
#define SessionServer           "/tmp/.s"
#define SessionIOName           "/tmp/.i"
#define MenuServerName          "/tmp/.h"
#define ForkServerName          "/tmp/.f"


#define MASK_SIZE       (NBBY*sizeof(fd_set))


/* table of dedicated socket types */

OPENAXIOM_EXPORT extern Sock *purpose_table[];
OPENAXIOM_EXPORT extern Sock server[];
OPENAXIOM_EXPORT extern Sock clients[];
OPENAXIOM_EXPORT extern fd_set socket_mask;
OPENAXIOM_EXPORT extern fd_set server_mask;


#endif /* OPENAXIOM_SOCKIO_included */