Annotation of OpenXM_contrib2/asir2000/io/tcpf_win_mini.c, Revision 1.5
1.5 ! noro 1: /* $OpenXM: OpenXM_contrib2/asir2000/io/tcpf_win_mini.c,v 1.4 2013/08/20 14:22:33 ohara 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: {
1.5 ! noro 23: int cs,ss,cn,sn;
! 24: char control_port_str[BUFSIZ];
! 25: char server_port_str[BUFSIZ];
! 26: static int launched=0;
! 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;
1.1 noro 61: }
62:
63: void win_spawn_server(char *control_port_str,char *server_port_str)
64: {
1.5 ! noro 65: char AsirExe[BUFSIZ];
! 66: char AsirExePath[BUFSIZ];
! 67: char *av[BUFSIZ];
! 68:
! 69: // strcpy(rootdir,"c:\\program files\\asir");
! 70: // sprintf(AsirExe,"%s\\bin\\engine.exe",rootdir);
! 71: sprintf(AsirExe,"c:\\Program Files\\asir\\bin\\engine.exe",rootdir);
! 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;
1.1 noro 82:
1.5 ! noro 83: _spawnv(_P_NOWAIT,AsirExe,av);
1.1 noro 84: }
85:
1.3 noro 86: void win_get_rootdir()
1.2 noro 87: {
1.5 ! noro 88: char *slash;
1.2 noro 89:
1.5 ! noro 90: GetModuleFileName(NULL,rootdir,BUFSIZ);
! 91: slash = strrchr(rootdir,'\\');
! 92: *slash = 0;
! 93: slash = strrchr(rootdir,'\\');
! 94: if ( slash ) *slash = 0;
1.2 noro 95: }
96:
1.3 noro 97: // ret=1 : data, ret=0 : void, ret=-1 : terminate
98:
99: int win_pop_string(Obj *obj)
100: {
1.5 ! noro 101: int id,serial;
1.3 noro 102:
1.5 ! noro 103: serial = ox_recv(server_port,&id,obj);
! 104: if ( serial < 0 ) return -1;
1.3 noro 105:
1.5 ! noro 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: }
1.3 noro 116: }
117:
118: /* ret=1 => data, ret=0 => void, ret=-1 => terminated */
119:
120: int win_pop_one(Obj *obj)
1.1 noro 121: {
1.5 ! noro 122: int id,serial;
1.1 noro 123:
1.5 ! noro 124: serial = ox_recv(server_port,&id,obj);
! 125: if ( serial < 0 ) return -1;
1.1 noro 126:
1.5 ! noro 127: if ( id == OX_DATA ) {
! 128: return 1;
! 129: } else if ( id == OX_SYNC_BALL ) {
! 130: ox_send_sync(server_port);
! 131: ox_flush_stream_force(server_port);
! 132: return 0;
! 133: } else return 0;
1.1 noro 134: }
135:
136: void win_execute_string(char *cmd)
137: {
1.5 ! noro 138: struct oSTRING str;
1.1 noro 139:
1.5 ! noro 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);
1.1 noro 146: }
147:
148: void ox_reset()
149: {
1.5 ! noro 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);
1.1 noro 160: }
161:
162: void ox_shutdown()
163: {
1.5 ! noro 164: terminate = 1;
! 165: ox_send_cmd(server_port,SM_shutdown);
! 166: Sleep(100);
! 167: ox_send_cmd(control_port,SM_shutdown);
1.1 noro 168: }
169:
1.3 noro 170: void free_obj(Obj obj)
171: {
1.5 ! noro 172: NODE n,n1;
1.3 noro 173:
1.5 ! noro 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);
1.3 noro 197: }
198:
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>