-
Notifications
You must be signed in to change notification settings - Fork 0
/
rubyholdem.cpp
534 lines (448 loc) · 13.5 KB
/
rubyholdem.cpp
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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
/*
Copyright 2009 Time
This file is part of RubyHoldem.
RubyHoldem is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
RubyHoldem is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with RubyHoldem. If not, see <http://www.gnu.org/licenses/>.
*/
//#define DEBUG_THREADS
#define _CRT_SECURE_NO_DEPRECATE
#ifdef __WINE__
#undef _WIN32
#include <ruby.h>
#define _WIN32
#else
#include <ruby.h>
#endif
#include <stdarg.h>
#include <windef.h>
#include <winbase.h>
#ifndef RSTRING_PTR
# define RSTRING_PTR(s) (RSTRING(s)->ptr)
# define RSTRING_LEN(s) (RSTRING(s)->len)
#endif
#define RH_NO_EVENT 0
#define RH_PROCESS_MESSAGE 1
#define RH_EXIT 2
void rh_log(const char *format, ...)
{
#ifdef __WINE__
va_list args;
va_start(args, format);
vprintf(format, args);
#else
int size = 0;
char text[100];
memset(text, 0, sizeof(text));
va_list args;
va_start(args, format);
#if 1
FILE *f = fopen("rubyholdem.log", "at");
vfprintf(f, format, args);
fclose(f);
#else
size = vsnprintf(text, sizeof(text) - 1, format, args);
MessageBox(NULL, text, "RubyHoldem message", 0);
#endif
#endif // __WINE__
}
struct holdem_player {
char m_name[16] ; //player name if known
double m_balance ; //player balance
double m_currentbet ; //player current bet
unsigned char m_cards[2] ; //player cards
unsigned char m_name_known : 1 ; //0=no 1=yes
unsigned char m_balance_known : 1 ; //0=no 1=yes
unsigned char m_fillerbits : 6 ; //filler bits
unsigned char m_fillerbyte ; //filler bytes
};
struct holdem_state {
char m_title[64] ; //table title
double m_pot[10] ; //total in each pot
unsigned char m_cards[5] ; //common cards
unsigned char m_is_playing : 1 ; //0=sitting-out, 1=sitting-in
unsigned char m_is_posting : 1 ; //0=autopost-off, 1=autopost-on
unsigned char m_fillerbits : 6 ; //filler bits
unsigned char m_fillerbyte ; //filler byte
unsigned char m_dealer_chair ; //0-9
holdem_player m_player[10] ; //player records
};
typedef VALUE (*rh_function)(ANYARGS);
typedef double (*pfgws_t)(int chair, const char* name, bool& error);
pfgws_t get_openholdem_symbol;
HANDLE rh_event_job;
HANDLE rh_event_done;
HANDLE rh_event_symbol_request;
HANDLE rh_event_symbol_request_done;
HANDLE rh_thread;
int rh_event_type = RH_NO_EVENT;
const char *rh_message;
const void *rh_param;
double rh_result;
int rh_symbol_chair;
const char *rh_symbol_name;
bool rh_symbol_error;
double rh_symbol_result;
extern "C" double __declspec(dllexport) process_message( const char* message, const void* param );
extern "C" BOOL APIENTRY DllMain(HANDLE module, DWORD reason, LPVOID reserved);
VALUE rh_module;
VALUE rh_safe_call_failed(VALUE arg, VALUE exception) {
char *description = (char*)arg;
char *message = RSTRING_PTR(rb_funcall(exception, rb_intern("message"), 0));
VALUE klass = rb_funcall(exception, rb_intern("class"), 0);
char *klass_name = RSTRING_PTR(rb_funcall(klass, rb_intern("name"), 0));
VALUE backtrace = rb_funcall(exception, rb_intern("backtrace"), 0);
char *backtrace_lines = RSTRING_PTR(rb_funcall(backtrace, rb_intern("join"), 1, rb_str_new2("\n")));
rh_log(
"Unhandled Ruby exception occured in %s\n"
"%s (%s)\n"
"%s\n",
description,
message, klass_name,
backtrace_lines
);
return rb_float_new(0.0);
}
VALUE rh_safe_call(const char *description, rh_function process_function, VALUE arg)
{
return rb_rescue2(
process_function, arg,
(rh_function)rh_safe_call_failed, (VALUE)description,
rb_eException, (VALUE)0
);
}
VALUE rh_process_query(VALUE arg)
{
VALUE result;
result = rb_funcall(rh_module, rb_intern("process_query"), 1, arg);
return rb_funcall(result, rb_intern("to_f"), 0);
}
VALUE rh_process_state(VALUE arg)
{
VALUE result;
result = rb_funcall(rh_module, rb_intern("process_state"), 1, arg);
return rb_funcall(result, rb_intern("to_f"), 0);
}
VALUE rh_process_event(VALUE arg)
{
VALUE result;
result = rb_funcall(rh_module, rb_intern("process_event"), 1, arg);
return rb_funcall(result, rb_intern("to_f"), 0);
}
void rh_emit_symbol_request()
{
DWORD dwWaitResult;
if (!SetEvent(rh_event_symbol_request))
{
rh_log("SetEvent failed (%d)\n", GetLastError());
return;
}
dwWaitResult = WaitForSingleObject(rh_event_symbol_request_done, INFINITE);
switch (dwWaitResult)
{
// Event object was signaled
case WAIT_OBJECT_0:
break;
// An error occurred
default:
rh_log("RubyHoldem thread: Wait error (%d)\n", GetLastError());
}
}
VALUE rh_get_symbol(VALUE self, VALUE chair, VALUE name)
{
bool error;
double result;
char *name_ptr = RSTRING_PTR(name);
#ifdef DEBUG_THREADS
rh_log("Thread %d getting symbol %s\n", GetCurrentThreadId(), name_ptr);
#endif
if (!get_openholdem_symbol)
rb_raise(rb_eRuntimeError, "Unable to get symbol: pfgws not received");
rh_symbol_chair = NUM2INT(chair);
rh_symbol_name = name_ptr;
rh_emit_symbol_request();
result = rh_symbol_result;
error = rh_symbol_error;
if (error)
rb_raise(rb_eRuntimeError, "OpenHoldem returned an error while getting symbol \"%s\"", name_ptr);
#ifdef DEBUG_THREADS
rh_log("Thread %d finished getting symbol %s\n", GetCurrentThreadId(), name_ptr);
#endif
return rb_float_new(result);
}
void process_symbol_request()
{
rh_symbol_result = get_openholdem_symbol(rh_symbol_chair, rh_symbol_name, rh_symbol_error);
if (!SetEvent(rh_event_symbol_request_done))
{
rh_log("SetEvent failed (%d)\n", GetLastError());
return;
}
}
VALUE rh_init_module()
{
get_openholdem_symbol = NULL;
rh_module = rb_define_module("OpenHoldem");
rb_define_module_function(rh_module, "get_symbol", (rh_function)rh_get_symbol, 2);
return rb_eval_string("load File.join(Dir.pwd, 'rubyholdem.rb')");
}
void rh_job_done()
{
#ifdef DEBUG_THREADS
rh_log("Thread %d sets job to done\n", GetCurrentThreadId());
#endif
rh_event_type = RH_NO_EVENT;
if (!SetEvent(rh_event_done))
{
rh_log("SetEvent failed (%d)\n", GetLastError());
return;
}
}
void rh_start_job(int type)
{
DWORD dwWaitResult;
HANDLE events[2];
int job_done = 0;
if (rh_event_type != RH_NO_EVENT)
{
rh_log("Starting a job while another one is running!\n");
}
rh_event_type = type;
if (!SetEvent(rh_event_job))
{
rh_log("SetEvent failed (%d)\n", GetLastError());
return;
}
#ifdef DEBUG_THREADS
rh_log("Thread %d waiting for job done\n", GetCurrentThreadId());
#endif
events[0] = rh_event_done;
events[1] = rh_event_symbol_request;
while (!job_done)
{
dwWaitResult = WaitForMultipleObjects(2, events, FALSE, INFINITE);
switch (dwWaitResult)
{
// rh_event_done was signaled
case WAIT_OBJECT_0:
#ifdef DEBUG_THREADS
rh_log("Thread %d: job done\n", GetCurrentThreadId());
#endif
if (rh_event_type != RH_NO_EVENT)
{
rh_log("Job done event was signaled but job doesn't look done!\n");
}
//ResetEvent(rh_event_done);
job_done = 1;
break;
// rh_event_symbol_request was signaled
case WAIT_OBJECT_0 + 1:
#ifdef DEBUG_THREADS
rh_log("Thread %d: symbol request\n", GetCurrentThreadId());
#endif
process_symbol_request();
break;
// An error occurred
default:
rh_log("Error waiting for job done: Wait error (%d)\n", GetLastError());
return;
}
}
}
double process_message( const char* message, const void* param )
{
if (message == NULL || param == NULL)
return 0;
#ifdef DEBUG_THREADS
rh_log("process_message in thread %d\n", GetCurrentThreadId());
#endif
rh_message = message;
rh_param = param;
rh_start_job(RH_PROCESS_MESSAGE);
return rh_result;
}
void rh_process_message()
{
const char *message = rh_message;
const void *param = rh_param;
VALUE result = Qnil;
#ifdef DEBUG_THREADS
rh_log("rh_process_message in thread %d (message: %s)\n", GetCurrentThreadId(), message);
#endif
if (strcmp(message, "query") == 0)
result = rh_safe_call("rh_process_query", (rh_function)rh_process_query, rb_str_new2((char*)param));
else if (strcmp(message, "state") == 0)
result = rh_safe_call("rh_process_state", (rh_function)rh_process_state, rb_str_new((char*)param, sizeof(struct holdem_state)));
else if (strcmp(message, "event") == 0)
result = rh_safe_call("rh_process_event", (rh_function)rh_process_event, rb_str_new2((char*)param));
else if (strcmp(message,"pfgws") == 0) {
rh_log("pfgws received\n");
get_openholdem_symbol = (pfgws_t)param;
rh_result = 0.0;
}
if (TYPE(result) == T_FLOAT)
rh_result = NUM2DBL(result);
else
rh_result = 0.0;
}
DWORD WINAPI rh_thread_proc(LPVOID lpParam)
{
DWORD dwWaitResult;
int must_exit = 0;
#ifdef DEBUG_THREADS
rh_log("RubyHoldem thread %d starting\n", GetCurrentThreadId());
#endif
ruby_init();
rh_safe_call("rh_load_init_script", (rh_function)rh_init_module, Qnil);
while (!must_exit)
{
#ifdef DEBUG_THREADS
rh_log("RubyHoldem thread %d waiting for event...\n", GetCurrentThreadId());
#endif
dwWaitResult = WaitForSingleObject(rh_event_job, INFINITE);
switch (dwWaitResult)
{
// Event object was signaled
case WAIT_OBJECT_0:
#ifdef DEBUG_THREADS
rh_log("Event occured in RubyHoldem thread %d\n", GetCurrentThreadId());
#endif
switch(rh_event_type)
{
case RH_EXIT:
#ifdef DEBUG_THREADS
rh_log("Event is RH_EXIT\n");
#endif
must_exit = 1;
break;
case RH_PROCESS_MESSAGE:
#ifdef DEBUG_THREADS
rh_log("Event is RH_PROCESS_MESSAGE\n");
#endif
rh_process_message();
break;
}
break;
// An error occurred
default:
rh_log("RubyHoldem thread: Wait error (%d)\n", GetLastError());
return 0;
}
rh_job_done();
}
ruby_finalize();
#ifdef DEBUG_THREADS
rh_log("RubyHoldem thread %d finished\n", GetCurrentThreadId());
#endif
return 1;
}
void rh_init()
{
DWORD dwThreadID;
rh_log("rh_init();\n");
#ifdef DEBUG_THREADS
rh_log("rh_init in thread %d\n", GetCurrentThreadId());
#endif
rh_event_job = CreateEvent(
NULL, // default security attributes
FALSE, // auto-reset event
FALSE, // initial state is nonsignaled
NULL // object name
);
if (rh_event_job == NULL)
{
rh_log("CreateEvent for job failed (%d)\n", GetLastError());
return;
}
rh_event_done = CreateEvent(
NULL, // default security attributes
FALSE, // auto-reset event
FALSE, // initial state is nonsignaled
NULL // object name
);
if (rh_event_done == NULL)
{
rh_log("CreateEvent for done failed (%d)\n", GetLastError());
return;
}
rh_event_symbol_request = CreateEvent(
NULL, // default security attributes
FALSE, // auto-reset event
FALSE, // initial state is nonsignaled
NULL // object name
);
if (rh_event_symbol_request == NULL)
{
rh_log("CreateEvent for symbol request failed (%d)\n", GetLastError());
return;
}
rh_event_symbol_request_done = CreateEvent(
NULL, // default security attributes
FALSE, // auto-reset event
FALSE, // initial state is nonsignaled
NULL // object name
);
if (rh_event_symbol_request_done == NULL)
{
rh_log("CreateEvent for symbol request done failed (%d)\n", GetLastError());
return;
}
rh_thread = CreateThread(
NULL, // default security
0, // default stack size
rh_thread_proc, // name of the thread function
NULL, // no thread parameters
0, // default startup flags
&dwThreadID);
if (rh_thread == NULL)
{
rh_log("CreateThread failed (%d)\n", GetLastError());
}
}
void rh_finalize()
{
//DWORD dwWaitResult;
rh_log("rh_finalize();\n");
#ifdef DEBUG_THREADS
rh_log("rh_finalize in thread %d\n", GetCurrentThreadId());
#endif
rh_start_job(RH_EXIT);
#if 0 // disabled, doesn't work but the thread exits, don't know why
rh_log("Waiting for RubyHoldem thread in thread %d\n", GetCurrentThreadId());
dwWaitResult = WaitForSingleObject(rh_thread, INFINITE);
switch (dwWaitResult)
{
case WAIT_OBJECT_0:
rh_log("RubyHoldem thread exited\n");
break;
// An error occurred
default:
rh_log("WaitForMultipleObjects failed (%d)\n", GetLastError());
return;
}
#endif
}
BOOL APIENTRY DllMain(HANDLE module, DWORD reason, LPVOID reserved)
{
switch (reason)
{
case DLL_PROCESS_ATTACH:
rh_init();
break;
case DLL_THREAD_ATTACH:
break;
case DLL_THREAD_DETACH:
break;
case DLL_PROCESS_DETACH:
rh_finalize();
break;
}
return TRUE;
}