-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathhoxSession.cpp
More file actions
923 lines (770 loc) · 28.1 KB
/
Copy pathhoxSession.cpp
File metadata and controls
923 lines (770 loc) · 28.1 KB
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
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
//
// C++ Implementation: hoxSession
//
// Description: The Session of a Player.
//
// Author: Huy Phan <hphan@hphan-hp>, (C) 2008-2009
//
// Created: 04/15/2009
//
#include "hoxSession.h"
#include "hoxSessionMgr.h"
#include "hoxPlayer.h"
#include "hoxTable.h"
#include "hoxLog.h"
#include "hoxSocketAPI.h"
#include "hoxExcept.h"
#include "hoxUtil.h"
#include "hoxFileMgr.h"
#include <sstream>
// =========================================================================
//
// hoxSession
//
// =========================================================================
hoxSession::hoxSession( const std::string& id,
hoxSessionType type,
st_netfd_t nfd,
hoxPlayer_SPtr pPlayer )
: _id( id )
, _type( type )
, _state( hoxSESSION_STATE_ACTIVE )
, _nfd( nfd )
, _player( pPlayer )
{
const char* FNAME = "hoxSession::hoxSession";
hoxLog(LOG_INFO, "%s: (%s:%s) ENTER.", FNAME, _id.c_str(), _player->getId().c_str());
this->updateTimeStamp();
}
hoxSession::~hoxSession()
{
const char* FNAME = "hoxSession::~hoxSession";
hoxLog(LOG_DEBUG, "%s: (%s:%s) ENTER.", FNAME, _id.c_str(), _player->getId().c_str());
}
void
hoxSession::handleFirstRequest( const hoxRequest_SPtr& firstRequest )
{
const char* FNAME = "hoxSession::handleFirstRequest";
hoxLog(LOG_DEBUG, "%s: (%s:%s) ENTER.", FNAME, _id.c_str(), _player->getId().c_str());
hoxResponse_SPtr pResponse;
this->handleRequest( firstRequest, pResponse );
if ( pResponse )
{
if ( hoxRC_OK != this->writeResponse( pResponse ) )
{
this->onDisconnected();
throw hoxError(hoxRC_ERR, "Failed to write outgoing response");
}
}
}
void
hoxSession::runEventLoop()
{
const char* FNAME = "hoxSession::runEventLoop";
hoxLog(LOG_DEBUG, "%s: (%s:%s) ENTER.", FNAME, _id.c_str(), _player->getId().c_str());
while ( _state == hoxSESSION_STATE_ACTIVE )
{
hoxRequest_SPtr pRequest;
if ( hoxRC_OK != this->readRequest( pRequest ) )
{
this->onDisconnected();
break;
}
hoxResponse_SPtr pResponse;
this->handleRequest( pRequest, pResponse );
if ( pResponse )
{
(void) this->writeResponse( pResponse );
}
st_sleep( ST_UTIME_NO_WAIT ); // Yield so that others can run.
}
}
hoxResult
hoxSession::readRequest( hoxRequest_SPtr& pRequest )
{
const char* FNAME = "hoxSession::readRequest";
hoxResult result;
std::string sRequest;
result = hoxSocketAPI::read_line( _nfd, sRequest, PERSIST_READ_TIMEOUT );
if ( result == hoxRC_EINTR ) /* Check for interrupt condition. */
{
hoxLog(LOG_INFO, "%s: (%s:%s) Empty request due to thread-interrupt.",
FNAME, _id.c_str(), _player->getId().c_str());
return hoxRC_EINTR;
}
else if ( sRequest.empty() ) /* Check for socket-close condition. */
{
hoxLog(LOG_DEBUG, "%s: (%s:%s) Empty request due to socket-close.",
FNAME, _id.c_str(), _player->getId().c_str());
return hoxRC_CLOSED;
}
else if ( result != hoxRC_OK )
{
hoxLog(LOG_INFO, "%s: (%s:%s) Failed to read request (rc=%d).",
FNAME, _id.c_str(), _player->getId().c_str(), result);
return hoxRC_ERR;
}
hoxLog(LOG_DEBUG, "%s: (%s:%s) Request: [%s]",
FNAME, _id.c_str(), _player->getId().c_str(), sRequest.c_str());
pRequest.reset( new hoxRequest( sRequest ) );
if ( ! pRequest->isValid() )
{
hoxLog(LOG_INFO, "%s: Request [%s] is invalid.", FNAME, sRequest.c_str());
return hoxRC_NOT_VALID;
}
return hoxRC_OK;
}
void
hoxSession::markForShutdown()
{
const char* FNAME = "hoxSession::markForShutdown";
hoxLog(LOG_DEBUG, "%s: (%s:%s) ENTER.", FNAME, _id.c_str(), _player->getId().c_str());
_state = hoxSESSION_STATE_SHUTDOWN;
}
void
hoxSession::onDeleted()
{
const char* FNAME = "hoxSession::onDeleted";
hoxLog(LOG_DEBUG, "%s: (%s:%s) ENTER.", FNAME, _id.c_str(), _player->getId().c_str());
_player->clearSession();
}
void
hoxSession::handleShutdown()
{
const char* FNAME = "hoxSession::handleShutdown";
hoxLog(LOG_INFO, "%s: (%s:%s) ENTER.", FNAME, _id.c_str(), _player->getId().c_str());
_player->leaveAllTables();
_state = hoxSESSION_STATE_SHUTDOWN;
/* Inform others about the event. */
hoxResponse_SPtr pResponse = hoxResponse::create_event_LOGOUT( _player );
hoxSessionMgr::getInstance()->postEventToAll( pResponse,
shared_from_this() /* excluded */ );
if ( _nfd != NULL )
{
this->closeIO();
}
}
void
hoxSession::handleRequest( const hoxRequest_SPtr& pRequest,
hoxResponse_SPtr& pResponse )
{
const char* FNAME = "hoxSession::handleRequest";
this->updateTimeStamp(); // Keep the session alive.
const hoxRequestType requestType = pRequest->getType();
try
{
switch( requestType )
{
case hoxREQUEST_LOGIN: handle_LOGIN( pRequest, pResponse ); break;
case hoxREQUEST_LOGOUT: handle_LOGOUT( pRequest, pResponse ); break;
case hoxREQUEST_LIST: handle_LIST( pRequest, pResponse ); break;
case hoxREQUEST_NEW: handle_NEW( pRequest, pResponse ); break;
case hoxREQUEST_JOIN: handle_JOIN( pRequest, pResponse ); break;
case hoxREQUEST_LEAVE: handle_LEAVE( pRequest, pResponse ); break;
case hoxREQUEST_UPDATE: handle_UPDATE( pRequest, pResponse ); break;
case hoxREQUEST_MSG: handle_MSG( pRequest, pResponse ); break;
case hoxREQUEST_PING: handle_PING( pRequest, pResponse ); break;
case hoxREQUEST_MOVE: handle_MOVE( pRequest, pResponse ); break;
case hoxREQUEST_RESIGN: handle_RESIGN( pRequest, pResponse ); break;
case hoxREQUEST_DRAW: handle_DRAW( pRequest, pResponse ); break;
case hoxREQUEST_RESET: handle_RESET( pRequest, pResponse ); break;
case hoxREQUEST_INVITE: handle_INVITE( pRequest, pResponse ); break;
case hoxREQUEST_PLAYER_INFO: handle_PLAYER_INFO( pRequest, pResponse ); break;
case hoxREQUEST_POLL: /* Handle this request below... */ break;
default: throw hoxError(hoxRC_NOT_SUPPORTED, "Unsupported Request");
}
}
catch( hoxTableError error )
{
pResponse.reset( new hoxResponse( requestType, error.code() ) );
pResponse->setTid( error.tid() );
pResponse->setContent( error.what() + std::string("\n") );
hoxLog(LOG_WARN, "%s: Table-Error caught [%s].", FNAME, error.toString().c_str());
}
catch( hoxError error )
{
pResponse.reset( new hoxResponse( requestType, error.code() ) );
pResponse->setContent( error.what() + std::string("\n") );
hoxLog(LOG_WARN, "%s: Error caught [%s].", FNAME, error.toString().c_str());
}
/* Write a response if any (some requests do not have any response). */
if ( pResponse )
{
this->addResponse( pResponse );
pResponse.reset();
}
}
void
hoxSession::handle_LOGIN( const hoxRequest_SPtr& pRequest,
hoxResponse_SPtr& pResponse )
{
hoxPlayer_SPtr player = this->getPlayer();
/* !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
* Since session-ID is secret to the Player, only the Player
* should know about the ID.
* !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
*/
// ... Notify others about the new online Player.
hoxResponse_SPtr pResponseWithoutID =
hoxResponse::create_event_LOGIN( player->getId(), player->getScore() );
hoxSessionMgr::getInstance()->postEventToAll( pResponseWithoutID,
shared_from_this() /* excluded */ );
// ... Only the Player knows his session-ID.
pResponse = hoxResponse::create_event_LOGIN( player->getId(), player->getScore(),
this->getId() );
player->onNewEvent( pResponse );
// Inform the Player about the list of existing online Players.
const std::string sEventContent = hoxSessionMgr::getInstance()->buildEvent_I_PLAYERS();
pResponse = hoxResponse::create_event_I_PLAYERS( sEventContent );
player->onNewEvent( pResponse );
// If the Player was disconnected, attempt to resume playing.
player->resumePlayingIfNeeded();
pResponse.reset(); // Return "nothing".
}
void
hoxSession::handle_LOGOUT( const hoxRequest_SPtr& pRequest,
hoxResponse_SPtr& pResponse )
{
this->markForShutdown();
}
void
hoxSession::handle_LIST( const hoxRequest_SPtr& pRequest,
hoxResponse_SPtr& pResponse )
{
hoxTableList tables;
hoxTableMgr::getInstance()->getTables( tables );
pResponse = hoxResponse::create_event_LIST( tables );
}
void
hoxSession::handle_NEW( const hoxRequest_SPtr& pRequest,
hoxResponse_SPtr& pResponse )
{
std::ostringstream outStream;
hoxPlayer_SPtr player = this->getPlayer();
const std::string itimes = pRequest->getParam("itimes");
const hoxTimeInfo initialTime = hoxUtil::stringToTimeInfo( itimes );
const std::string sRequestColor = pRequest->getParam("color");
/* Get the requested color (Red, Black, or Observer). */
hoxColor requestColor = hoxUtil::stringToColor( sRequestColor );
if ( requestColor == hoxCOLOR_UNKNOWN )
requestColor = hoxCOLOR_RED; // Default: Play RED
hoxTable_SPtr pNewTable =
hoxTableMgr::getInstance()->createTable( initialTime );
if ( ! pNewTable )
{
throw hoxError(hoxRC_NOT_FOUND, "Table cannot be created");
}
player->joinTableAs( pNewTable, requestColor );
pResponse.reset(); // Return "nothing".
}
void
hoxSession::handle_JOIN( const hoxRequest_SPtr& pRequest,
hoxResponse_SPtr& pResponse )
{
hoxTable_SPtr pTable;
hoxPlayer_SPtr player = this->getPlayer();
const std::string tableId = pRequest->getParam("tid");
const std::string sRequestColor = pRequest->getParam("color");
hoxColor requestColor = hoxCOLOR_UNKNOWN;
/* Get the requested color (Red, Black, or Observer). */
requestColor = hoxUtil::stringToColor( sRequestColor );
if ( requestColor == hoxCOLOR_UNKNOWN )
{
requestColor = hoxCOLOR_NONE; // Default: Observer
}
/* Find the Table. */
pTable = hoxTableMgr::getInstance()->findTable(tableId);
if ( ! pTable )
{
throw hoxError(hoxRC_NOT_FOUND, "Table not found");
}
player->joinTableAs( pTable, requestColor );
pResponse.reset(); // Return "nothing".
}
void
hoxSession::handle_LEAVE( const hoxRequest_SPtr& pRequest,
hoxResponse_SPtr& pResponse )
{
const std::string tableId = pRequest->getParam("tid");
hoxTable_SPtr pTable = hoxTableMgr::getInstance()->findTable(tableId);
if ( ! pTable )
{
throw hoxError(hoxRC_NOT_FOUND, "Table not found");
}
_player->leaveTable( pTable );
/* Trigger the table-clean routine. */
hoxTableMgr::getInstance()->runCleanup();
pResponse.reset(); // Return "nothing".
}
void
hoxSession::handle_UPDATE( const hoxRequest_SPtr& pRequest,
hoxResponse_SPtr& pResponse )
{
hoxPlayer_SPtr player = this->getPlayer();
const std::string tableId = pRequest->getParam("tid");
const bool bRatedGame = ( pRequest->getParam("rated") == "1" );
const hoxTimeInfo initialTime =
hoxUtil::stringToTimeInfo( pRequest->getParam("itimes") );
hoxResult result = player->updateTable( tableId,
bRatedGame, initialTime );
if ( result != hoxRC_OK )
{
throw hoxError(hoxRC_ERR, "Update (Table) not OK");
}
pResponse.reset(); // Return "nothing".
}
void
hoxSession::handle_MSG( const hoxRequest_SPtr& pRequest,
hoxResponse_SPtr& pResponse )
{
hoxPlayer_SPtr player = this->getPlayer();
/* There are two cases:
* (1) The message targets a given Table (all players in it).
* We call the message a "table" message.
* (2) The message targets only a specific player.
* We call the message a "private" message.
*/
const std::string tableId = pRequest->getParam("tid");
const std::string otherId = pRequest->getParam("oid");
const std::string message = pRequest->getParam("msg");
const hoxResponse_SPtr pMessage =
hoxResponse::create_event_MSG( player, message, tableId );
/* Case (1): "table" message. */
if ( ! tableId.empty() )
{
hoxTable_SPtr pTable = hoxTableMgr::getInstance()->findTable(tableId);
if ( ! pTable )
{
throw hoxError(hoxRC_NOT_FOUND, "Table not found");
}
pTable->onMessage_FromPlayer( player, pMessage );
}
/* Case (2): "private" message. */
else
{
hoxSession_SPtr foundSession =
hoxSessionMgr::getInstance()->findSession( otherId );
if ( ! foundSession )
{
throw hoxError(hoxRC_NOT_FOUND, "Player not found");
}
hoxPlayer_SPtr otherPlayer = foundSession->getPlayer();
otherPlayer->onNewEvent( pMessage );
}
/* For the message sender, return nothing. */
pResponse.reset();
}
void
hoxSession::handle_PING( const hoxRequest_SPtr& pRequest,
hoxResponse_SPtr& pResponse )
{
pResponse = hoxResponse::create_event_PING();
}
void
hoxSession::handle_MOVE( const hoxRequest_SPtr& pRequest,
hoxResponse_SPtr& pResponse )
{
const std::string tableId = pRequest->getParam("tid");
const std::string sMove = pRequest->getParam("move");
_player->doMove( tableId, sMove );
pResponse.reset(); // Return "nothing".
}
void
hoxSession::handle_RESIGN( const hoxRequest_SPtr& pRequest,
hoxResponse_SPtr& pResponse )
{
const std::string tableId = pRequest->getParam("tid");
_player->offerResign( tableId );
pResponse.reset(); // Return "nothing".
}
void
hoxSession::handle_DRAW( const hoxRequest_SPtr& pRequest,
hoxResponse_SPtr& pResponse )
{
const std::string tableId = pRequest->getParam("tid");
_player->offerDraw( tableId );
pResponse.reset(); // Return "nothing".
}
void
hoxSession::handle_RESET( const hoxRequest_SPtr& pRequest,
hoxResponse_SPtr& pResponse )
{
const std::string tableId = pRequest->getParam("tid");
_player->resetTable( tableId );
pResponse.reset(); // Return "nothing".
}
void
hoxSession::handle_INVITE( const hoxRequest_SPtr& pRequest,
hoxResponse_SPtr& pResponse )
{
hoxPlayer_SPtr player = this->getPlayer();
const std::string sInviterId = player->getId();
const int nInviterScore = player->getScore();
const std::string sInviteeId = pRequest->getParam("oid");
const std::string tableId = pRequest->getParam("tid"); // OPTIONAL!
/* Lookup the invitee using session-manager.
* NOTE: Currently, the server supports inviting "online" players.
*/
hoxSession_SPtr foundSession =
hoxSessionMgr::getInstance()->findSession( sInviteeId );
if ( ! foundSession )
{
throw hoxError(hoxRC_NOT_FOUND, "Invitee not found");
}
hoxPlayer_SPtr inviteePlayer = foundSession->getPlayer();
/* Simply forward the request to the invitee. */
hoxResponse_SPtr pInvitation =
hoxResponse::create_event_INVITE( sInviterId, nInviterScore,
sInviteeId, tableId );
inviteePlayer->onNewEvent( pInvitation );
/* For the inviter, return nothing. */
pResponse.reset();
}
void
hoxSession::handle_PLAYER_INFO( const hoxRequest_SPtr& pRequest,
hoxResponse_SPtr& pResponse )
{
const std::string tableId = pRequest->getParam("tid");
const std::string infoPlayerId = pRequest->getParam("oid");
/* Lookup the "info" Player using session-manager.
* NOTE: Currently, the server supports looking up "online" players.
*/
hoxSession_SPtr foundSession =
hoxSessionMgr::getInstance()->findSession( infoPlayerId );
if ( ! foundSession )
{
throw hoxError(hoxRC_NOT_FOUND, "Player not found");
}
hoxPlayer_SPtr infoPlayer = foundSession->getPlayer();
/* Return:
* The requested Player's Info.
*/
pResponse = hoxResponse::create_event_PLAYER_INFO( infoPlayer,
tableId );
}
// =========================================================================
//
// hoxPersistentSession
//
// =========================================================================
/**
* Handle write Thread.
*/
void*
_handle_write( void *arg )
{
hoxPersistentSession* pSession = (hoxPersistentSession*) arg;
pSession->handleWrite();
return NULL;
}
hoxPersistentSession::hoxPersistentSession( const std::string& id,
st_netfd_t nfd,
hoxPlayer_SPtr pPlayer,
hoxSessionType type /* = hoxSESSION_TYPE_PERSISTENT */ )
: hoxSession( id,
type,
nfd,
pPlayer )
, _readThread( NULL )
, _writeThread( NULL )
, _writeCond( NULL )
{
_readThread = st_thread_self();
_writeCond = st_cond_new();
_writeThread = st_thread_create( _handle_write, (void *) this,
1 /* joinable */, 0 /* stack-size */ );
if ( _writeThread == NULL )
{
hoxLog(LOG_SYS_WARN, "%s: Failed to create WRITE thread.", __FUNCTION__);
_state = hoxSESSION_STATE_SHUTDOWN; // Signal a "bad" session.
return;
}
}
hoxPersistentSession::~hoxPersistentSession()
{
st_cond_destroy( _writeCond );
}
bool
hoxPersistentSession::resumeConnection( st_netfd_t nfd,
hoxClientType clientType )
{
const char* FNAME = "hoxPersistentSession::resumeConnection";
const bool okToResume = ( clientType == hoxCLIENT_TYPE_HOXCHESS );
if ( ! okToResume )
{
return false;
}
// Close the existing connection, if any.
if ( _nfd != NULL )
{
hoxLog(LOG_INFO, "%s: (%s:%s) Force to close existing connection.", FNAME, _id.c_str(), _player->getId().c_str());
_state = hoxSESSION_STATE_DISCONNECT;
if (_readThread != NULL)
{
hoxLog(LOG_INFO, "%s: (%s:%s) Interrupt the READ thread...", FNAME, _id.c_str(), _player->getId().c_str());
st_thread_interrupt( _readThread );
// NOTE: We cannot use "st_thread_join()" on the READ write because
// it will lead to deadlock with two threads (READ and WRITE)
// waiting for each other.
const int maxWaitTimes = 5; // in seconds.
for (int i = 0; i < maxWaitTimes; ++i)
{
st_sleep(1); // Wait 1 second at a time.
if (_readThread == NULL) break;
}
hoxLog(LOG_INFO, "%s: (%s:%s) Done closing the READ thread (closed=%d)",
FNAME, _id.c_str(), _player->getId().c_str(), (_readThread == NULL ? 1 : 0));
}
this->closeIO();
}
_nfd = nfd;
_readThread = st_thread_self();
_responseList.clear(); // NOTE: Remove old events.
_state = hoxSESSION_STATE_ACTIVE;
_writeThread = st_thread_create( _handle_write, (void *) this,
1 /* joinable */, 0 /* stack-size */ );
if ( _writeThread == NULL )
{
hoxLog(LOG_SYS_WARN, "%s: Failed to create WRITE thread.", __FUNCTION__);
_state = hoxSESSION_STATE_SHUTDOWN; // Signal a "bad" session.
return false;
}
return true;
}
void
hoxPersistentSession::addResponse( const hoxResponse_SPtr& response )
{
if ( _state == hoxSESSION_STATE_ACTIVE )
{
_responseList.push_back( response ); // Make a copy...
}
st_cond_signal( _writeCond );
}
void
hoxPersistentSession::handleWrite()
{
const char* FNAME = "hoxPersistentSession::handleWrite";
while ( _state == hoxSESSION_STATE_ACTIVE )
{
if ( _responseList.empty() )
{
st_cond_wait( _writeCond );
continue; // NOTE: Double-check one more time.
}
hoxResponse_SPtr response( _responseList.front() );
_responseList.pop_front();
if ( hoxRC_OK != this->writeResponse( response ) )
{
continue; // NOTE: Still allow to continue.
}
/* Keep alive to avoid being disconnected if the Player is only
* observing the game(s).
*/
this->updateTimeStamp();
st_sleep( ST_UTIME_NO_WAIT ); // yield so that others can run.
}
/* Cleanup the WRITE connection. */
_writeThread = NULL;
hoxLog(LOG_DEBUG, "%s: (%s) Closed WRITE connection.", FNAME, _id.c_str());
}
hoxResult
hoxPersistentSession::writeResponse( const hoxResponse_SPtr& response )
{
const std::string sResp = response->toString();
return hoxSocketAPI::write_data( _nfd, sResp );
}
void
hoxPersistentSession::onDisconnected()
{
const char* FNAME = "hoxPersistentSession::onDisconnected";
hoxLog(LOG_DEBUG, "%s: (%s:%s) ENTER.", FNAME, _id.c_str(), _player->getId().c_str());
_state = hoxSESSION_STATE_DISCONNECT;
this->closeIO();
}
void
hoxPersistentSession::closeIO()
{
const char* FNAME = "hoxPersistentSession::closeIO";
hoxLog(LOG_DEBUG, "%s: Closing READ connection...", FNAME);
_readThread = NULL;
if ( _writeThread != NULL )
{
st_cond_signal( _writeCond );
if ( 0 != st_thread_join( _writeThread, NULL ) )
{
hoxLog(LOG_SYS_WARN, "%s: Failed waiting for WRITE thread to end.", FNAME);
}
}
_nfd = NULL;
}
hoxResponse_SPtr
hoxPersistentSession::getPendingEvents()
{
hoxResponse_SPtr pResponse; // No event!
return pResponse;
}
// =========================================================================
//
// hoxFlashSession
//
// =========================================================================
void
hoxFlashSession::handleFirstRequest( const hoxRequest_SPtr& firstRequest )
{
const char* FNAME = "hoxFlashSession::handleFirstRequest";
hoxResult result;
/* Consume the NULL terminator. */
std::string sData;
result = hoxSocketAPI::read_nbytes( _nfd, 1, sData /* NO TIME OUT */ );
if ( result != hoxRC_OK
|| sData.size() != 1 || sData.at(0) != '\0' )
{
hoxLog(LOG_INFO, "%s: (%s:%s) NULL terminator not found.",
FNAME, _id.c_str(), _player->getId().c_str());
return;
}
hoxSession::handleFirstRequest( firstRequest );
}
hoxResult
hoxFlashSession::readRequest( hoxRequest_SPtr& pRequest )
{
const char* FNAME = "hoxFlashSession::readRequest";
hoxResult result = hoxSession::readRequest( pRequest );
if ( result != hoxRC_OK )
{
return result;
}
/* Consume the NULL terminator. */
std::string sData;
result = hoxSocketAPI::read_nbytes( _nfd, 1, sData /* NO TIME OUT */ );
if ( result != hoxRC_OK
|| sData.size() != 1 || sData.at(0) != '\0' )
{
hoxLog(LOG_DEBUG, "%s: (%s:%s) NULL terminator not found.",
FNAME, _id.c_str(), _player->getId().c_str());
return hoxRC_CLOSED;
}
return hoxRC_OK;
}
hoxResult
hoxFlashSession::writeResponse( const hoxResponse_SPtr& response )
{
std::string sResp = response->toString();
sResp += '\0';
return hoxSocketAPI::write_data( _nfd, sResp );
}
// =========================================================================
//
// hoxPollingSession
//
// =========================================================================
void
hoxPollingSession::addResponse( const hoxResponse_SPtr& response )
{
_responseList.push_back( response ); // Make a copy...
}
hoxResponse_SPtr
hoxPollingSession::getPendingEvents()
{
hoxResponse_SPtr pResponse = hoxResponse::create_event_POLL( _responseList );
_responseList.clear();
return pResponse;
}
void
hoxPollingSession::handleRequest( const hoxRequest_SPtr& pRequest,
hoxResponse_SPtr& pResponse )
{
hoxSession::handleRequest( pRequest, pResponse );
/* For HTTP requests, return pending events now. */
pResponse = this->getPendingEvents();
}
hoxResult
hoxPollingSession::readRequest( hoxRequest_SPtr& pRequest )
{
const char* FNAME = "hoxPollingSession::readRequest";
hoxResult result;
std::string sRequest;
hoxLog(LOG_DEBUG, "%s: (%s:%s) ENTER.", FNAME, _id.c_str(), _player->getId().c_str());
result = hoxSocketAPI::read_line( _nfd, sRequest, PERSIST_READ_TIMEOUT );
if ( sRequest.empty() ) /* Check for socket-close condition. */
{
hoxLog(LOG_DEBUG, "%s: (%s:%s) Empty request due to socket-close.",
FNAME, _id.c_str(), _player->getId().c_str());
return hoxRC_CLOSED;
}
else if ( result != hoxRC_OK )
{
hoxLog(LOG_INFO, "%s: (%s:%s) Failed to read request.",
FNAME, _id.c_str(), _player->getId().c_str());
return hoxRC_ERR;
}
hoxLog(LOG_DEBUG, "%s: (%s:%s) Request: [%s]",
FNAME, _id.c_str(), _player->getId().c_str(), sRequest.c_str());
/* Check if this is a HTTP (GET/POST) request. */
hoxHttpRequest httpRequest;
if ( sRequest.find("GET") == 0 || sRequest.find("POST") == 0 )
{
httpRequest.parseURI( sRequest );
result = httpRequest.continueReadRequest( _nfd );
if ( result != hoxRC_OK )
{
return result;
}
if ( httpRequest.method == "GET" )
{
hoxPollingSession::handle_http_GET( _nfd, httpRequest );
return hoxRC_HANDLED;
}
else // ... POST method, the 'real' request is in the body.
{
sRequest = httpRequest.body;
hoxUtil::trimLast( sRequest, '\n' );
}
}
/* Parse the request. */
pRequest.reset( new hoxRequest( sRequest ) );
if ( ! pRequest->isValid() )
{
hoxLog(LOG_INFO, "%s: Request [%s] is invalid.", FNAME, sRequest.c_str());
return hoxRC_NOT_VALID;
}
return hoxRC_OK;
}
hoxResult
hoxPollingSession::writeResponse( const hoxResponse_SPtr& response )
{
const char* FNAME = "hoxPollingSession::writeResponse";
hoxLog(LOG_DEBUG, "%s: (%s:%s) ENTER.", FNAME, _id.c_str(), _player->getId().c_str());
const std::string sResp =
hoxPollingSession::build_http_response( response->toString() );
return hoxSocketAPI::write_data( _nfd, sResp );
}
/**
* Build the HTTP response to send back to the client.
*/
std::string
hoxPollingSession::build_http_response( const std::string& sResponseContent,
const std::string sContentType /* = "text/html" */ )
{
std::ostringstream outStream;
outStream << "HTTP/1.1 200 OK" << "\r\n"
<< "Date: " << hoxUtil::getHttpDate() << "\r\n"
<< "Server: games.playxiangqi.com v1.0" << "\r\n"
<< "Content-Length: " << sResponseContent.size() << "\r\n"
<< "Content-Type: " << sContentType << "; charset=UTF-8" << "\r\n"
//<< "Connection: close" << "\r\n"
<< "\r\n"
<< sResponseContent;
return outStream.str();
}
void
hoxPollingSession::handle_http_GET( st_netfd_t nfd,
const hoxHttpRequest& httpRequest )
{
hoxFile_SPtr pFile = hoxFileMgr::getInstance()->getFile( httpRequest.path );
hoxASSERT_MSG( pFile, "File pointer must have been set");
const std::string sResp =
hoxPollingSession::build_http_response( pFile->m_sContent, pFile->m_sType );
(void) hoxSocketAPI::write_data( nfd, sResp );
}
/******************* END OF FILE *********************************************/