forked from ulthiel/polyglot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathengine.c
112 lines (86 loc) · 2.62 KB
/
engine.c
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
// engine.c
// includes
#include <stdlib.h>
#include <stdarg.h>
#include <string.h>
#include <errno.h>
#include "engine.h"
#include "option.h"
#include "pipex.h"
#include "util.h"
// variables
engine_t Engine[1];
// functions
// set_affinity()
void set_affinity(engine_t *engine, int value){
pipex_set_affinity(engine->pipex,value);
}
// engine_set_nice_value()
void engine_set_nice_value(engine_t *engine, int value){
pipex_set_priority(engine->pipex,value);
}
// engine_send_queue()
void engine_send_queue(engine_t * engine, const char *format, ...) {
char buf[FormatBufferSize];
CONSTRUCT_ARG_STRING(format,buf);
pipex_write(engine->pipex,buf);
}
// engine_send()
void engine_send(engine_t * engine, const char *format, ...) {
char buf[FormatBufferSize];
CONSTRUCT_ARG_STRING(format,buf);
pipex_writeln(engine->pipex,buf);
}
// engine_close()
void engine_close(engine_t * engine){
char string[StringSize];
int elapsed_time;
int ret;
int close_timeout=500;
my_log("POLYGLOT Closing engine.\n");
pipex_send_eof(engine->pipex);
// TODO: Timeout
elapsed_time=0;
while (!engine_eof(engine) && (elapsed_time<close_timeout)) {
ret=engine_get_non_blocking(engine,string);
if(!ret && !engine_eof(engine)){
my_log("POLYGLOT Engine does not reply. Sleeping %dms.\n", WAIT_GRANULARITY);
my_sleep(WAIT_GRANULARITY);
elapsed_time+=WAIT_GRANULARITY;
}
}
if(elapsed_time>=close_timeout){
my_log("POLYGLOT Waited more than %dms. Moving on.\n",close_timeout);
}
pipex_exit(engine->pipex,200);
}
// engine_open()
void engine_open(engine_t * engine){
int affinity= -1;
pipex_open(engine->pipex,
"Engine",
option_get_string(Option,"EngineDir"),
option_get_string(Option,"EngineCommand"));
if(pipex_active(engine->pipex)){
//play with affinity (bad idea)
sscanf(option_get_string(Option,"Affinity"), "%x", &affinity);
if(affinity!=-1) set_affinity(engine,affinity); //AAA
// set a low priority
if (option_get_bool(Option,"UseNice")){
my_log("POLYGLOT Adjust Engine Piority\n");
engine_set_nice_value(engine, option_get_int(Option,"NiceValue"));
}
}
}
bool engine_active(engine_t *engine){
return pipex_active(engine->pipex);
}
bool engine_eof(engine_t *engine){
return pipex_eof(engine->pipex);
}
bool engine_get_non_blocking(engine_t * engine, char *string){
return pipex_readln_nb(engine->pipex,string);
}
void engine_get(engine_t * engine, char *string){
pipex_readln(engine->pipex,string);
}