Edinburgh Speech Tools  2.1-release
slib_server.cc
Go to the documentation of this file.
1 /*
2  * COPYRIGHT (c) 1988-1994 BY *
3  * PARADIGM ASSOCIATES INCORPORATED, CAMBRIDGE, MASSACHUSETTS. *
4  * See the source file SLIB.C for more information. *
5 
6  * Reorganization of files (Mar 1999) by Alan W Black <awb@cstr.ed.ac.uk>
7 
8  * Client/server functions
9 
10 */
11 #include <cstdio>
12 #include "EST_io_aux.h"
13 #include "siod.h"
14 #include "siodp.h"
15 
16 using namespace std;
17 
19 
21 {
22  int check_write;
23  // Send x to the client
24  if (siod_server_socket == -1)
25  {
26  err("siod: not in server mode",x);
27  }
28 
29  EST_String tmpfile = make_tmp_filename();
30  FILE *fd;
31  EST_String m = siod_sprint(x);
32 
33  if ((fd=fopen(tmpfile,"wb")) == NULL)
34  {
35  cerr << "siod: can't open temporary file \"" <<
36  tmpfile << "\" for client lisp return" << endl;
37  }
38  else
39  {
40  fwrite((const char *)m,sizeof(char),m.length(),fd);
41  fwrite("\n",1,1,fd);
42  fclose(fd);
43 #ifdef WIN32
44  check_write = send(siod_server_socket,"LP\n",3,0);
45 #else
46  check_write = write(siod_server_socket,"LP\n",3);
47 #endif
48  if (check_write != 3)
49  cerr << "Could not send LP to socket" << endl;
51  unlink(tmpfile);
52  }
53 
54  return x;
55 }
56 
58 {
59  // Called to let client know if server gets an error
60  // Thanks to mcb for pointing out this omission
61  int i;
62  if (siod_server_socket != -1) {
63 #ifdef WIN32
64  i = send(siod_server_socket,"ER\n",3,0);
65 #else
66  i = write(siod_server_socket,"ER\n",3);
67 #endif
68  if (i!=3)
69  cerr << "Could not acknowledge error to client." << endl;
70  }
71 }
72 
73 static void acknowledge_sock_print(LISP x)
74 { // simple return "OK" -- used in server socket mode
75  int i=0;
77 #ifdef WIN32
78  i = send(siod_server_socket,"OK\n",3,0);
79 #else
80 
81  i = write(siod_server_socket,"OK\n",3);
82 #endif
83  if (i!=3)
84  cerr << "Could not write acknowledge" << endl;
85 }
86 
87 static void ignore_puts(char *x)
88 {
89  (void)x;
90 }
91 
92 long repl_from_socket(int fd)
93 {
94  /* Read from given fd as stdin */
95  struct repl_hooks hd;
96 
97 #ifdef WIN32
98  if (!SetStdHandle(STD_INPUT_HANDLE,(HANDLE)fd))
99  {
100  GetLastError();
101  cerr << "repl_from_socket: couldn't set stdin to socket\n";
102  }
103 #else
104  dup2(fd,0); // make socket into stdin
105  // dup2(fd,1); // make socket into stdout
106 #endif
107  hd.repl_puts = ignore_puts;
108  hd.repl_print = acknowledge_sock_print;
109  hd.repl_eval = NULL;
110 #ifdef WIN32
111  hd.repl_read = lreadwinsock;
112 #else
113  hd.repl_read = NULL;
114 #endif
116  siod_server_socket = fd;
117 
118  return repl_driver(1,0,&hd);
119 }
120 
121 void init_subrs_srv(void)
122 {
124  "(send_client EXPR)\n\
125  Send EXPR to client. In server mode this will send a printed form of\n\
126  ESPR to the client. It is the client's job to expect it.");
127 }
LISP(* repl_eval)(LISP)
Definition: siod_defs.h:196
Utility IO Function header file.
void init_subrs_srv(void)
Definition: slib_server.cc:121
LISP siod_send_lisp_to_client(LISP x)
Definition: slib_server.cc:20
EST_String make_tmp_filename()
Make a unique temporary filename.
Definition: util_io.cc:56
void(* repl_print)(LISP)
Definition: siod_defs.h:197
int socket_send_file(SOCKET_FD fd, const EST_String &filename)
Definition: filetrans.cc:112
void err(const char *message, LISP x) EST_NORETURN
Definition: slib.cc:608
void(* repl_puts)(char *)
Definition: siod_defs.h:194
EST_String siod_sprint(LISP exp)
Definition: slib_file.cc:208
int siod_server_socket
Definition: slib_server.cc:18
int siod_interactive
Definition: slib.cc:164
#define FALSE
Definition: EST_bool.h:119
NULL
Definition: EST_WFST.cc:55
void init_subr_1(const char *name, LISP(*fcn)(LISP), const char *doc)
Definition: slib.cc:898
long repl_from_socket(int fd)
Definition: slib_server.cc:92
size_t length(void) const
Length of string ({not} length of underlying chunk)
Definition: EST_String.h:231
long repl_driver(long want_sigint, long want_init, struct repl_hooks *)
Definition: slib.cc:329
void sock_acknowledge_error()
Definition: slib_server.cc:57
LISP(* repl_read)(void)
Definition: siod_defs.h:195