Annotation of OpenXM_contrib2/asir2000/io/tcpf_win_mini.c, Revision 1.4
1.4 ! ohara 1: /* $OpenXM: OpenXM_contrib2/asir2000/io/tcpf_win_mini.c,v 1.3 2006/09/29 09:02:49 noro Exp $ */
1.1 noro 2:
3: #include "ca.h"
4: #include "parse.h"
5: #include "com.h"
6: #include <signal.h>
7: #include <string.h>
8: #include "ox.h"
9: #include <stdlib.h>
10: #include <winsock2.h>
11: #include <process.h>
12: static int control_port,server_port;
13: int win_ox_launch();
14: void win_spawn_server(char *control_port_str,char *server_port_str);
1.3 noro 15: void win_get_rootdir();
16: int win_pop_string(Obj *);
17: void free_obj(Obj obj);
1.1 noro 18: extern int terminate;
1.3 noro 19: static char rootdir[BUFSIZ];
1.1 noro 20:
21: int win_ox_launch()
22: {
23: int cs,ss,cn,sn;
24: char control_port_str[BUFSIZ];
25: char server_port_str[BUFSIZ];
1.3 noro 26: static int launched=0;
1.1 noro 27:
28: if ( launched ) return 0;
29:
30: control_port_str[0] = 0;
31: server_port_str[0] = 0;
32: do {
33: generate_port(0,control_port_str);
34: generate_port(0,server_port_str);
35: cs = try_bind_listen(0,control_port_str);
36: if ( cs < 0 ) continue;
37: ss = try_bind_listen(0,server_port_str);
38: if ( ss < 0 ) continue;
39: win_spawn_server(control_port_str,server_port_str);
40: cs = try_accept(0,cs);
41: if ( cs < 0 ) continue;
42: ss = try_accept(0,ss);
43: if ( ss < 0 ) continue;
44: } while ( cs < 0 || ss < 0 );
45:
46: /* client mode */
47: cn = get_iofp(cs,control_port_str,0);
48: sn = get_iofp(ss,server_port_str,0);
49: /* get_iofp returns -1 if the laucher could not spawn the server */
50: if ( sn < 0 ) {
51: /* we should terminate the launcher */
52: ox_send_cmd(cn,SM_shutdown); ox_flush_stream_force(cn);
53: return -1;
54: }
55:
56: /* register server to the server list */
57: control_port = cn;
58: server_port = sn;
59: launched = 1;
60: return 0;
61: }
62:
63: void win_spawn_server(char *control_port_str,char *server_port_str)
64: {
65: char AsirExe[BUFSIZ];
66: char AsirExePath[BUFSIZ];
67: char *av[BUFSIZ];
68:
1.2 noro 69: // strcpy(rootdir,"c:\\program files\\asir");
1.3 noro 70: // sprintf(AsirExe,"%s\\bin\\engine.exe",rootdir);
71: sprintf(AsirExe,"c:\\Program Files\\asir\\bin\\engine.exe",rootdir);
1.1 noro 72: sprintf(AsirExePath,"\"%s\"",AsirExe);
73: av[0] = "ox_launch";
74: av[1] = "127.0.0.1";
75: av[2] = "0";
76: av[3] = control_port_str;
77: av[4] = server_port_str;
78: av[5] = "ox_asir_wfep";
79: av[6] = "1";
80: av[7] = AsirExePath;
81: av[8] = 0;
82:
83: _spawnv(_P_NOWAIT,AsirExe,av);
84: }
85:
1.3 noro 86: void win_get_rootdir()
1.2 noro 87: {
88: char *slash;
89:
1.4 ! ohara 90: GetModuleFileName(NULL,rootdir,BUFSIZ);
! 91: slash = strrchr(rootdir,'\\');
! 92: *slash = 0;
1.3 noro 93: slash = strrchr(rootdir,'\\');
1.2 noro 94: if ( slash ) *slash = 0;
95: }
96:
1.3 noro 97: // ret=1 : data, ret=0 : void, ret=-1 : terminate
98:
99: int win_pop_string(Obj *obj)
100: {
101: int id,serial;
102:
103: serial = ox_recv(server_port,&id,obj);
104: if ( serial < 0 ) return -1;
105:
106: if ( id == OX_DATA )
107: return 1;
108: else {
109: if ( id == OX_SYNC_BALL ) {
110: ox_send_sync(server_port);
111: ox_flush_stream_force(server_port);
112: }
113: *obj = 0;
114: return 0;
115: }
116: }
117:
118: /* ret=1 => data, ret=0 => void, ret=-1 => terminated */
119:
120: int win_pop_one(Obj *obj)
1.1 noro 121: {
122: int id,serial;
123:
1.3 noro 124: serial = ox_recv(server_port,&id,obj);
125: if ( serial < 0 ) return -1;
1.1 noro 126:
127: if ( id == OX_DATA ) {
1.3 noro 128: return 1;
1.1 noro 129: } else if ( id == OX_SYNC_BALL ) {
130: ox_send_sync(server_port);
131: ox_flush_stream_force(server_port);
1.3 noro 132: return 0;
133: } else return 0;
1.1 noro 134: }
135:
136: void win_execute_string(char *cmd)
137: {
138: struct oSTRING str;
139:
140: OID(&str) = O_STR;
141: BDY(&str) = cmd;
142: ox_send_data(server_port,&str);
143: ox_send_cmd(server_port,SM_executeStringByLocalParser);
144: ox_send_cmd(server_port,SM_popString);
145: ox_flush_stream_force(server_port);
146: }
147:
148: void ox_reset()
149: {
150: USINT t;
151: int id;
152: Obj obj;
153:
154: ox_send_cmd(control_port,SM_control_reset_connection);
155: ox_flush_stream_force(control_port);
156: ox_recv(control_port,&id,&obj); t = (USINT)obj;
157: Sleep(100);
158: ox_send_cmd(server_port,SM_nop);
159: ox_flush_stream_force(server_port);
160: }
161:
162: void ox_shutdown()
163: {
164: terminate = 1;
165: ox_send_cmd(server_port,SM_shutdown);
166: Sleep(100);
167: ox_send_cmd(control_port,SM_shutdown);
168: }
169:
1.3 noro 170: void free_obj(Obj obj)
171: {
172: NODE n,n1;
173:
174: if ( !obj ) return;
175: switch ( OID(obj) ) {
176: case O_USINT:
177: break;
178: case O_STR:
179: free(BDY((STRING)obj));
180: break;
181: case O_LIST:
182: n = BDY((LIST)obj);
183: while ( n ) {
184: free_obj(BDY(n));
185: n1 = NEXT(n);
186: free(n);
187: n = n1;
188: }
189: break;
190: case O_ERR:
191: free_obj(BDY((LIST)obj));
192: break;
193: default:
194: break;
195: }
196: free(obj);
197: }
198:
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>