File: slib_server.cc

package info (click to toggle)
speech-tools 1:2.5.0-5
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 9,848 kB
  • sloc: cpp: 67,350; ansic: 12,175; sh: 4,047; java: 3,748; makefile: 1,109; lisp: 711; perl: 501; awk: 85; xml: 9
file content (117 lines) | stat: -rw-r--r-- 2,657 bytes parent folder | download | duplicates (7)
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
/*  
 *                   COPYRIGHT (c) 1988-1994 BY                             *
 *        PARADIGM ASSOCIATES INCORPORATED, CAMBRIDGE, MASSACHUSETTS.       *
 *        See the source file SLIB.C for more information.                  *

 * Reorganization of files (Mar 1999) by Alan W Black <awb@cstr.ed.ac.uk>

 * Client/server functions

*/
#include <cstdio>
#include "EST_io_aux.h"
#include "siod.h"
#include "siodp.h"

int siod_server_socket = -1;

LISP siod_send_lisp_to_client(LISP x)
{
    // Send x to the client
    if (siod_server_socket == -1)
    {
	err("siod: not in server mode",x);
    }

    EST_String tmpfile = make_tmp_filename();
    FILE *fd;
    EST_String m = siod_sprint(x);

    if ((fd=fopen(tmpfile,"wb")) == NULL)
    {
	cerr << "siod: can't open temporary file \"" << 
	    tmpfile << "\" for client lisp return" << endl;
    }
    else
    {
	fwrite((const char *)m,sizeof(char),m.length(),fd);
	fwrite("\n",1,1,fd);
	fclose(fd);
#ifdef WIN32
	send(siod_server_socket,"LP\n",3,0);
#else
	write(siod_server_socket,"LP\n",3);
#endif
	socket_send_file(siod_server_socket,tmpfile);
	unlink(tmpfile);
    }

    return x;
}

void sock_acknowledge_error()
{
    // Called to let client know if server gets an error
    // Thanks to mcb for pointing out this omission
    
    if (siod_server_socket != -1)
#ifdef WIN32
	send(siod_server_socket,"ER\n",3,0);
#else
	write(siod_server_socket,"ER\n",3);
#endif

}
    
static void acknowledge_sock_print(LISP x)
{   // simple return "OK" -- used in server socket mode

    siod_send_lisp_to_client(x);
#ifdef WIN32
    send(siod_server_socket,"OK\n",3,0);
#else
    write(siod_server_socket,"OK\n",3);
#endif
}

static void ignore_puts(char *x)
{   
    (void)x;
}

long repl_from_socket(int fd)
{
    /* Read from given fd as stdin */
    struct repl_hooks hd;

#ifdef WIN32
    if (!SetStdHandle(STD_INPUT_HANDLE,(HANDLE)fd))
    {
	GetLastError();
	cerr << "repl_from_socket: couldn't set stdin to socket\n";
    }
#else
    dup2(fd,0);                     // make socket into stdin
    // dup2(fd,1);                     // make socket into stdout
#endif
    hd.repl_puts = ignore_puts;
    hd.repl_print = acknowledge_sock_print;
    hd.repl_eval = NULL;
#ifdef WIN32
    hd.repl_read = lreadwinsock;
#else
    hd.repl_read = NULL;
#endif
    siod_interactive = FALSE;
    siod_server_socket = fd;

    return repl_driver(1,0,&hd);
}

void init_subrs_srv(void)
{
 init_subr_1("send_client",siod_send_lisp_to_client,
 "(send_client EXPR)\n\
 Send EXPR to client.  In server mode this will send a printed form of\n\
 ESPR to the client.  It is the client's job to expect it.");
}