hedgewars/uFLIPC.pas
author unc0rr
Thu, 17 Dec 2015 00:51:20 +0300
branchqmlfrontend
changeset 11464 30397f91571c
parent 11459 3c5d99013baf
child 11465 9f2fb0031ef0
permissions -rw-r--r--
Fix a ton of bugs in engine instances queue and ipc
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
10406
b5fd52ac760f Basic layout of frontlib, some more sdl bindings
unc0rr
parents:
diff changeset
     1
unit uFLIPC;
10410
669bfa55cd70 Some work on new IPC, built with the use of mutexes and condition variables
unc0rr
parents: 10406
diff changeset
     2
interface
10418
091d2c0216c3 Move away from passing shortstrings into C code, now IPC works
unc0rr
parents: 10416
diff changeset
     3
uses SDLh, uFLTypes;
10406
b5fd52ac760f Basic layout of frontlib, some more sdl bindings
unc0rr
parents:
diff changeset
     4
10410
669bfa55cd70 Some work on new IPC, built with the use of mutexes and condition variables
unc0rr
parents: 10406
diff changeset
     5
procedure initIPC;
669bfa55cd70 Some work on new IPC, built with the use of mutexes and condition variables
unc0rr
parents: 10406
diff changeset
     6
procedure freeIPC;
669bfa55cd70 Some work on new IPC, built with the use of mutexes and condition variables
unc0rr
parents: 10406
diff changeset
     7
10428
7c25297720f1 More refactoring: move PoC preview getting code into flib
unc0rr
parents: 10426
diff changeset
     8
procedure ipcToEngine(s: shortstring);
10898
f373838129c2 Some futher work on flib net client part
unc0rr
parents: 10428
diff changeset
     9
procedure ipcToEngineRaw(p: pointer; len: Longword);
11457
78860824b5a5 Introduce barriers between messages to different engine instances
unc0rr
parents: 11456
diff changeset
    10
procedure ipcSetEngineBarrier();
78860824b5a5 Introduce barriers between messages to different engine instances
unc0rr
parents: 11456
diff changeset
    11
procedure ipcRemoveBarrierFromEngineQueue();
10420
unc0rr
parents: 10418
diff changeset
    12
//function  ipcReadFromEngine: shortstring;
unc0rr
parents: 10418
diff changeset
    13
//function  ipcCheckFromEngine: boolean;
10406
b5fd52ac760f Basic layout of frontlib, some more sdl bindings
unc0rr
parents:
diff changeset
    14
10935
3a65fcd7c335 Move SDLNet_TCP_Send to its own thread
unc0rr
parents: 10933
diff changeset
    15
procedure ipcToNet(s: shortstring);
3a65fcd7c335 Move SDLNet_TCP_Send to its own thread
unc0rr
parents: 10933
diff changeset
    16
procedure ipcToNetRaw(p: pointer; len: Longword);
3a65fcd7c335 Move SDLNet_TCP_Send to its own thread
unc0rr
parents: 10933
diff changeset
    17
10412
9a8d4efcf3fa - More flib IPC routines
unc0rr
parents: 10410
diff changeset
    18
procedure ipcToFrontend(s: shortstring);
10420
unc0rr
parents: 10418
diff changeset
    19
procedure ipcToFrontendRaw(p: pointer; len: Longword);
11459
3c5d99013baf - Improve IPC handling in engine
unc0rr
parents: 11457
diff changeset
    20
function ipcReadFromFrontend: TIPCMessage;
10412
9a8d4efcf3fa - More flib IPC routines
unc0rr
parents: 10410
diff changeset
    21
function ipcCheckFromFrontend: boolean;
9a8d4efcf3fa - More flib IPC routines
unc0rr
parents: 10410
diff changeset
    22
10428
7c25297720f1 More refactoring: move PoC preview getting code into flib
unc0rr
parents: 10426
diff changeset
    23
procedure registerIPCCallback(p: pointer; f: TIPCCallback);
10935
3a65fcd7c335 Move SDLNet_TCP_Send to its own thread
unc0rr
parents: 10933
diff changeset
    24
procedure registerNetCallback(p: pointer; f: TIPCCallback);
10416
1c301054694d - Remove --port command
unc0rr
parents: 10412
diff changeset
    25
10406
b5fd52ac760f Basic layout of frontlib, some more sdl bindings
unc0rr
parents:
diff changeset
    26
implementation
b5fd52ac760f Basic layout of frontlib, some more sdl bindings
unc0rr
parents:
diff changeset
    27
10935
3a65fcd7c335 Move SDLNet_TCP_Send to its own thread
unc0rr
parents: 10933
diff changeset
    28
var callbackPointerF: pointer;
3a65fcd7c335 Move SDLNet_TCP_Send to its own thread
unc0rr
parents: 10933
diff changeset
    29
    callbackFunctionF: TIPCCallback;
3a65fcd7c335 Move SDLNet_TCP_Send to its own thread
unc0rr
parents: 10933
diff changeset
    30
    callbackListenerThreadF: PSDL_Thread;
3a65fcd7c335 Move SDLNet_TCP_Send to its own thread
unc0rr
parents: 10933
diff changeset
    31
    callbackPointerN: pointer;
3a65fcd7c335 Move SDLNet_TCP_Send to its own thread
unc0rr
parents: 10933
diff changeset
    32
    callbackFunctionN: TIPCCallback;
3a65fcd7c335 Move SDLNet_TCP_Send to its own thread
unc0rr
parents: 10933
diff changeset
    33
    callbackListenerThreadN: PSDL_Thread;
11455
0c75fa9ce340 - Use queues instead of single buffer to communicate between threads
unc0rr
parents: 11423
diff changeset
    34
    queueFrontend, queueEngine, queueNet: PIPCQueue;
10416
1c301054694d - Remove --port command
unc0rr
parents: 10412
diff changeset
    35
11455
0c75fa9ce340 - Use queues instead of single buffer to communicate between threads
unc0rr
parents: 11423
diff changeset
    36
procedure ipcSend(var s: TIPCMessage; queue: PIPCQueue);
0c75fa9ce340 - Use queues instead of single buffer to communicate between threads
unc0rr
parents: 11423
diff changeset
    37
var pmsg: PIPCMessage;
10412
9a8d4efcf3fa - More flib IPC routines
unc0rr
parents: 10410
diff changeset
    38
begin
11455
0c75fa9ce340 - Use queues instead of single buffer to communicate between threads
unc0rr
parents: 11423
diff changeset
    39
    SDL_LockMutex(queue^.mut);
0c75fa9ce340 - Use queues instead of single buffer to communicate between threads
unc0rr
parents: 11423
diff changeset
    40
0c75fa9ce340 - Use queues instead of single buffer to communicate between threads
unc0rr
parents: 11423
diff changeset
    41
    s.next:= nil;
11457
78860824b5a5 Introduce barriers between messages to different engine instances
unc0rr
parents: 11456
diff changeset
    42
    s.barrier:= 0;
10418
091d2c0216c3 Move away from passing shortstrings into C code, now IPC works
unc0rr
parents: 10416
diff changeset
    43
11464
30397f91571c Fix a ton of bugs in engine instances queue and ipc
unc0rr
parents: 11459
diff changeset
    44
    if (queue^.msg.next = nil) and (queue^.msg.str[0] = #0) and (queue^.msg.buf = nil) and (queue^.msg.barrier = 0) then
11455
0c75fa9ce340 - Use queues instead of single buffer to communicate between threads
unc0rr
parents: 11423
diff changeset
    45
    begin
0c75fa9ce340 - Use queues instead of single buffer to communicate between threads
unc0rr
parents: 11423
diff changeset
    46
        queue^.msg:= s;
0c75fa9ce340 - Use queues instead of single buffer to communicate between threads
unc0rr
parents: 11423
diff changeset
    47
    end else
0c75fa9ce340 - Use queues instead of single buffer to communicate between threads
unc0rr
parents: 11423
diff changeset
    48
    begin
0c75fa9ce340 - Use queues instead of single buffer to communicate between threads
unc0rr
parents: 11423
diff changeset
    49
        new(pmsg);
0c75fa9ce340 - Use queues instead of single buffer to communicate between threads
unc0rr
parents: 11423
diff changeset
    50
        pmsg^:= s;
0c75fa9ce340 - Use queues instead of single buffer to communicate between threads
unc0rr
parents: 11423
diff changeset
    51
        queue^.last^.next:= pmsg;
0c75fa9ce340 - Use queues instead of single buffer to communicate between threads
unc0rr
parents: 11423
diff changeset
    52
        queue^.last:= pmsg;
0c75fa9ce340 - Use queues instead of single buffer to communicate between threads
unc0rr
parents: 11423
diff changeset
    53
    end;
0c75fa9ce340 - Use queues instead of single buffer to communicate between threads
unc0rr
parents: 11423
diff changeset
    54
    SDL_CondSignal(queue^.cond);
0c75fa9ce340 - Use queues instead of single buffer to communicate between threads
unc0rr
parents: 11423
diff changeset
    55
    SDL_UnlockMutex(queue^.mut);
10412
9a8d4efcf3fa - More flib IPC routines
unc0rr
parents: 10410
diff changeset
    56
end;
9a8d4efcf3fa - More flib IPC routines
unc0rr
parents: 10410
diff changeset
    57
11455
0c75fa9ce340 - Use queues instead of single buffer to communicate between threads
unc0rr
parents: 11423
diff changeset
    58
function ipcRead(queue: PIPCQueue): TIPCMessage;
0c75fa9ce340 - Use queues instead of single buffer to communicate between threads
unc0rr
parents: 11423
diff changeset
    59
var pmsg: PIPCMessage;
10412
9a8d4efcf3fa - More flib IPC routines
unc0rr
parents: 10410
diff changeset
    60
begin
11455
0c75fa9ce340 - Use queues instead of single buffer to communicate between threads
unc0rr
parents: 11423
diff changeset
    61
    SDL_LockMutex(queue^.mut);
11457
78860824b5a5 Introduce barriers between messages to different engine instances
unc0rr
parents: 11456
diff changeset
    62
    while ((queue^.msg.str[0] = #0) and (queue^.msg.buf = nil))
78860824b5a5 Introduce barriers between messages to different engine instances
unc0rr
parents: 11456
diff changeset
    63
            and ((queue^.msg.barrier > 0) or (queue^.msg.next = nil) or ((queue^.msg.next^.barrier > 0) and (queue^.msg.next^.str[0] = #0) and (queue^.msg.next^.buf = nil))) do
11455
0c75fa9ce340 - Use queues instead of single buffer to communicate between threads
unc0rr
parents: 11423
diff changeset
    64
        SDL_CondWait(queue^.cond, queue^.mut);
10412
9a8d4efcf3fa - More flib IPC routines
unc0rr
parents: 10410
diff changeset
    65
11455
0c75fa9ce340 - Use queues instead of single buffer to communicate between threads
unc0rr
parents: 11423
diff changeset
    66
    if (queue^.msg.str[0] <> #0) or (queue^.msg.buf <> nil) then
0c75fa9ce340 - Use queues instead of single buffer to communicate between threads
unc0rr
parents: 11423
diff changeset
    67
        begin
0c75fa9ce340 - Use queues instead of single buffer to communicate between threads
unc0rr
parents: 11423
diff changeset
    68
            ipcRead:= queue^.msg;
0c75fa9ce340 - Use queues instead of single buffer to communicate between threads
unc0rr
parents: 11423
diff changeset
    69
            queue^.msg.str[0]:= #0;
0c75fa9ce340 - Use queues instead of single buffer to communicate between threads
unc0rr
parents: 11423
diff changeset
    70
            queue^.msg.buf:= nil;
0c75fa9ce340 - Use queues instead of single buffer to communicate between threads
unc0rr
parents: 11423
diff changeset
    71
        end else
0c75fa9ce340 - Use queues instead of single buffer to communicate between threads
unc0rr
parents: 11423
diff changeset
    72
        begin
0c75fa9ce340 - Use queues instead of single buffer to communicate between threads
unc0rr
parents: 11423
diff changeset
    73
            pmsg:= queue^.msg.next;
0c75fa9ce340 - Use queues instead of single buffer to communicate between threads
unc0rr
parents: 11423
diff changeset
    74
            ipcRead:= pmsg^;
11457
78860824b5a5 Introduce barriers between messages to different engine instances
unc0rr
parents: 11456
diff changeset
    75
            if pmsg^.barrier > 0 then
78860824b5a5 Introduce barriers between messages to different engine instances
unc0rr
parents: 11456
diff changeset
    76
            begin
78860824b5a5 Introduce barriers between messages to different engine instances
unc0rr
parents: 11456
diff changeset
    77
                pmsg^.str[0]:= #0;
78860824b5a5 Introduce barriers between messages to different engine instances
unc0rr
parents: 11456
diff changeset
    78
                pmsg^.buf:= nil
78860824b5a5 Introduce barriers between messages to different engine instances
unc0rr
parents: 11456
diff changeset
    79
            end else
78860824b5a5 Introduce barriers between messages to different engine instances
unc0rr
parents: 11456
diff changeset
    80
            begin
78860824b5a5 Introduce barriers between messages to different engine instances
unc0rr
parents: 11456
diff changeset
    81
                queue^.msg.next:= pmsg^.next;
78860824b5a5 Introduce barriers between messages to different engine instances
unc0rr
parents: 11456
diff changeset
    82
                if queue^.msg.next = nil then queue^.last:= @queue^.msg;
78860824b5a5 Introduce barriers between messages to different engine instances
unc0rr
parents: 11456
diff changeset
    83
                dispose(pmsg)
78860824b5a5 Introduce barriers between messages to different engine instances
unc0rr
parents: 11456
diff changeset
    84
            end
11455
0c75fa9ce340 - Use queues instead of single buffer to communicate between threads
unc0rr
parents: 11423
diff changeset
    85
        end;
10420
unc0rr
parents: 10418
diff changeset
    86
11455
0c75fa9ce340 - Use queues instead of single buffer to communicate between threads
unc0rr
parents: 11423
diff changeset
    87
    SDL_UnlockMutex(queue^.mut)
10412
9a8d4efcf3fa - More flib IPC routines
unc0rr
parents: 10410
diff changeset
    88
end;
9a8d4efcf3fa - More flib IPC routines
unc0rr
parents: 10410
diff changeset
    89
11455
0c75fa9ce340 - Use queues instead of single buffer to communicate between threads
unc0rr
parents: 11423
diff changeset
    90
function ipcCheck(queue: PIPCQueue): boolean;
10412
9a8d4efcf3fa - More flib IPC routines
unc0rr
parents: 10410
diff changeset
    91
begin
11455
0c75fa9ce340 - Use queues instead of single buffer to communicate between threads
unc0rr
parents: 11423
diff changeset
    92
    SDL_LockMutex(queue^.mut);
11457
78860824b5a5 Introduce barriers between messages to different engine instances
unc0rr
parents: 11456
diff changeset
    93
    ipcCheck:= (queue^.msg.str[0] > #0) or (queue^.msg.buf <> nil) or
78860824b5a5 Introduce barriers between messages to different engine instances
unc0rr
parents: 11456
diff changeset
    94
               ((queue^.msg.barrier = 0) and (queue^.msg.next <> nil) and ((queue^.msg.next^.barrier = 0) or (queue^.msg.next^.str[0] <> #0) or (queue^.msg.next^.buf <> nil)));
11455
0c75fa9ce340 - Use queues instead of single buffer to communicate between threads
unc0rr
parents: 11423
diff changeset
    95
    SDL_UnlockMutex(queue^.mut)
10412
9a8d4efcf3fa - More flib IPC routines
unc0rr
parents: 10410
diff changeset
    96
end;
9a8d4efcf3fa - More flib IPC routines
unc0rr
parents: 10410
diff changeset
    97
10428
7c25297720f1 More refactoring: move PoC preview getting code into flib
unc0rr
parents: 10426
diff changeset
    98
procedure ipcToEngine(s: shortstring);
10420
unc0rr
parents: 10418
diff changeset
    99
var msg: TIPCMessage;
10410
669bfa55cd70 Some work on new IPC, built with the use of mutexes and condition variables
unc0rr
parents: 10406
diff changeset
   100
begin
10428
7c25297720f1 More refactoring: move PoC preview getting code into flib
unc0rr
parents: 10426
diff changeset
   101
    msg.str:= s;
10420
unc0rr
parents: 10418
diff changeset
   102
    msg.buf:= nil;
11455
0c75fa9ce340 - Use queues instead of single buffer to communicate between threads
unc0rr
parents: 11423
diff changeset
   103
    ipcSend(msg, queueEngine)
10412
9a8d4efcf3fa - More flib IPC routines
unc0rr
parents: 10410
diff changeset
   104
end;
10410
669bfa55cd70 Some work on new IPC, built with the use of mutexes and condition variables
unc0rr
parents: 10406
diff changeset
   105
10412
9a8d4efcf3fa - More flib IPC routines
unc0rr
parents: 10410
diff changeset
   106
procedure ipcToFrontend(s: shortstring);
10420
unc0rr
parents: 10418
diff changeset
   107
var msg: TIPCMessage;
10412
9a8d4efcf3fa - More flib IPC routines
unc0rr
parents: 10410
diff changeset
   108
begin
10420
unc0rr
parents: 10418
diff changeset
   109
    msg.str:= s;
unc0rr
parents: 10418
diff changeset
   110
    msg.buf:= nil;
11455
0c75fa9ce340 - Use queues instead of single buffer to communicate between threads
unc0rr
parents: 11423
diff changeset
   111
    ipcSend(msg, queueFrontend)
10410
669bfa55cd70 Some work on new IPC, built with the use of mutexes and condition variables
unc0rr
parents: 10406
diff changeset
   112
end;
669bfa55cd70 Some work on new IPC, built with the use of mutexes and condition variables
unc0rr
parents: 10406
diff changeset
   113
11457
78860824b5a5 Introduce barriers between messages to different engine instances
unc0rr
parents: 11456
diff changeset
   114
procedure ipcSetEngineBarrier();
78860824b5a5 Introduce barriers between messages to different engine instances
unc0rr
parents: 11456
diff changeset
   115
begin
78860824b5a5 Introduce barriers between messages to different engine instances
unc0rr
parents: 11456
diff changeset
   116
    SDL_LockMutex(queueEngine^.mut);
78860824b5a5 Introduce barriers between messages to different engine instances
unc0rr
parents: 11456
diff changeset
   117
78860824b5a5 Introduce barriers between messages to different engine instances
unc0rr
parents: 11456
diff changeset
   118
    inc(queueEngine^.last^.barrier);
78860824b5a5 Introduce barriers between messages to different engine instances
unc0rr
parents: 11456
diff changeset
   119
78860824b5a5 Introduce barriers between messages to different engine instances
unc0rr
parents: 11456
diff changeset
   120
    SDL_UnlockMutex(queueEngine^.mut);
78860824b5a5 Introduce barriers between messages to different engine instances
unc0rr
parents: 11456
diff changeset
   121
end;
78860824b5a5 Introduce barriers between messages to different engine instances
unc0rr
parents: 11456
diff changeset
   122
78860824b5a5 Introduce barriers between messages to different engine instances
unc0rr
parents: 11456
diff changeset
   123
procedure ipcRemoveBarrierFromEngineQueue();
11456
6e9b12864856 Start work on running engine in network game
unc0rr
parents: 11455
diff changeset
   124
var pmsg, t: PIPCMessage;
6e9b12864856 Start work on running engine in network game
unc0rr
parents: 11455
diff changeset
   125
    q: PIPCQueue;
6e9b12864856 Start work on running engine in network game
unc0rr
parents: 11455
diff changeset
   126
begin
6e9b12864856 Start work on running engine in network game
unc0rr
parents: 11455
diff changeset
   127
    q:= queueEngine;
6e9b12864856 Start work on running engine in network game
unc0rr
parents: 11455
diff changeset
   128
6e9b12864856 Start work on running engine in network game
unc0rr
parents: 11455
diff changeset
   129
    SDL_LockMutex(q^.mut);
6e9b12864856 Start work on running engine in network game
unc0rr
parents: 11455
diff changeset
   130
6e9b12864856 Start work on running engine in network game
unc0rr
parents: 11455
diff changeset
   131
    pmsg:= @q^.msg;
11464
30397f91571c Fix a ton of bugs in engine instances queue and ipc
unc0rr
parents: 11459
diff changeset
   132
write('    ipcRemoveBarrierFromEngineQueue: ');
11456
6e9b12864856 Start work on running engine in network game
unc0rr
parents: 11455
diff changeset
   133
    while pmsg <> nil do
6e9b12864856 Start work on running engine in network game
unc0rr
parents: 11455
diff changeset
   134
    begin
11464
30397f91571c Fix a ton of bugs in engine instances queue and ipc
unc0rr
parents: 11459
diff changeset
   135
        write('.');
11456
6e9b12864856 Start work on running engine in network game
unc0rr
parents: 11455
diff changeset
   136
        t:= pmsg^.next;
11457
78860824b5a5 Introduce barriers between messages to different engine instances
unc0rr
parents: 11456
diff changeset
   137
        q^.msg.next:= t;
11456
6e9b12864856 Start work on running engine in network game
unc0rr
parents: 11455
diff changeset
   138
11464
30397f91571c Fix a ton of bugs in engine instances queue and ipc
unc0rr
parents: 11459
diff changeset
   139
        pmsg^.str[0]:= #0;
11456
6e9b12864856 Start work on running engine in network game
unc0rr
parents: 11455
diff changeset
   140
        if pmsg^.buf <> nil then
11464
30397f91571c Fix a ton of bugs in engine instances queue and ipc
unc0rr
parents: 11459
diff changeset
   141
        begin
11456
6e9b12864856 Start work on running engine in network game
unc0rr
parents: 11455
diff changeset
   142
            FreeMem(pmsg^.buf, pmsg^.len);
11464
30397f91571c Fix a ton of bugs in engine instances queue and ipc
unc0rr
parents: 11459
diff changeset
   143
            pmsg^.buf:= nil
30397f91571c Fix a ton of bugs in engine instances queue and ipc
unc0rr
parents: 11459
diff changeset
   144
        end;
11456
6e9b12864856 Start work on running engine in network game
unc0rr
parents: 11455
diff changeset
   145
6e9b12864856 Start work on running engine in network game
unc0rr
parents: 11455
diff changeset
   146
        if pmsg <> @q^.msg then
11457
78860824b5a5 Introduce barriers between messages to different engine instances
unc0rr
parents: 11456
diff changeset
   147
            if pmsg^.barrier = 0 then
78860824b5a5 Introduce barriers between messages to different engine instances
unc0rr
parents: 11456
diff changeset
   148
                dispose(pmsg)
78860824b5a5 Introduce barriers between messages to different engine instances
unc0rr
parents: 11456
diff changeset
   149
            else
78860824b5a5 Introduce barriers between messages to different engine instances
unc0rr
parents: 11456
diff changeset
   150
            if pmsg^.barrier = 1 then
78860824b5a5 Introduce barriers between messages to different engine instances
unc0rr
parents: 11456
diff changeset
   151
            begin
78860824b5a5 Introduce barriers between messages to different engine instances
unc0rr
parents: 11456
diff changeset
   152
                dispose(pmsg);
78860824b5a5 Introduce barriers between messages to different engine instances
unc0rr
parents: 11456
diff changeset
   153
                t:= nil
78860824b5a5 Introduce barriers between messages to different engine instances
unc0rr
parents: 11456
diff changeset
   154
            end else
78860824b5a5 Introduce barriers between messages to different engine instances
unc0rr
parents: 11456
diff changeset
   155
            begin
78860824b5a5 Introduce barriers between messages to different engine instances
unc0rr
parents: 11456
diff changeset
   156
                dec(pmsg^.barrier);
78860824b5a5 Introduce barriers between messages to different engine instances
unc0rr
parents: 11456
diff changeset
   157
                q^.msg.next:= pmsg;
78860824b5a5 Introduce barriers between messages to different engine instances
unc0rr
parents: 11456
diff changeset
   158
                t:= nil
78860824b5a5 Introduce barriers between messages to different engine instances
unc0rr
parents: 11456
diff changeset
   159
            end
78860824b5a5 Introduce barriers between messages to different engine instances
unc0rr
parents: 11456
diff changeset
   160
        else
78860824b5a5 Introduce barriers between messages to different engine instances
unc0rr
parents: 11456
diff changeset
   161
            if pmsg^.barrier > 0 then 
78860824b5a5 Introduce barriers between messages to different engine instances
unc0rr
parents: 11456
diff changeset
   162
            begin
78860824b5a5 Introduce barriers between messages to different engine instances
unc0rr
parents: 11456
diff changeset
   163
                dec(pmsg^.barrier);
78860824b5a5 Introduce barriers between messages to different engine instances
unc0rr
parents: 11456
diff changeset
   164
                t:= nil
78860824b5a5 Introduce barriers between messages to different engine instances
unc0rr
parents: 11456
diff changeset
   165
            end;
78860824b5a5 Introduce barriers between messages to different engine instances
unc0rr
parents: 11456
diff changeset
   166
11456
6e9b12864856 Start work on running engine in network game
unc0rr
parents: 11455
diff changeset
   167
        pmsg:= t
6e9b12864856 Start work on running engine in network game
unc0rr
parents: 11455
diff changeset
   168
    end;
11464
30397f91571c Fix a ton of bugs in engine instances queue and ipc
unc0rr
parents: 11459
diff changeset
   169
writeln;
30397f91571c Fix a ton of bugs in engine instances queue and ipc
unc0rr
parents: 11459
diff changeset
   170
    if q^.msg.next = nil then q^.last:= @q^.msg;
11456
6e9b12864856 Start work on running engine in network game
unc0rr
parents: 11455
diff changeset
   171
6e9b12864856 Start work on running engine in network game
unc0rr
parents: 11455
diff changeset
   172
    q^.msg.str[0]:= #0;
6e9b12864856 Start work on running engine in network game
unc0rr
parents: 11455
diff changeset
   173
    q^.msg.buf:= nil;
6e9b12864856 Start work on running engine in network game
unc0rr
parents: 11455
diff changeset
   174
6e9b12864856 Start work on running engine in network game
unc0rr
parents: 11455
diff changeset
   175
    SDL_UnlockMutex(q^.mut);
6e9b12864856 Start work on running engine in network game
unc0rr
parents: 11455
diff changeset
   176
end;
6e9b12864856 Start work on running engine in network game
unc0rr
parents: 11455
diff changeset
   177
10935
3a65fcd7c335 Move SDLNet_TCP_Send to its own thread
unc0rr
parents: 10933
diff changeset
   178
procedure ipcToNet(s: shortstring);
3a65fcd7c335 Move SDLNet_TCP_Send to its own thread
unc0rr
parents: 10933
diff changeset
   179
var msg: TIPCMessage;
3a65fcd7c335 Move SDLNet_TCP_Send to its own thread
unc0rr
parents: 10933
diff changeset
   180
begin
3a65fcd7c335 Move SDLNet_TCP_Send to its own thread
unc0rr
parents: 10933
diff changeset
   181
    msg.str:= s;
3a65fcd7c335 Move SDLNet_TCP_Send to its own thread
unc0rr
parents: 10933
diff changeset
   182
    msg.buf:= nil;
11455
0c75fa9ce340 - Use queues instead of single buffer to communicate between threads
unc0rr
parents: 11423
diff changeset
   183
    ipcSend(msg, queueNet)
10935
3a65fcd7c335 Move SDLNet_TCP_Send to its own thread
unc0rr
parents: 10933
diff changeset
   184
end;
3a65fcd7c335 Move SDLNet_TCP_Send to its own thread
unc0rr
parents: 10933
diff changeset
   185
10898
f373838129c2 Some futher work on flib net client part
unc0rr
parents: 10428
diff changeset
   186
procedure ipcToEngineRaw(p: pointer; len: Longword);
f373838129c2 Some futher work on flib net client part
unc0rr
parents: 10428
diff changeset
   187
var msg: TIPCMessage;
f373838129c2 Some futher work on flib net client part
unc0rr
parents: 10428
diff changeset
   188
begin
f373838129c2 Some futher work on flib net client part
unc0rr
parents: 10428
diff changeset
   189
    msg.str[0]:= #0;
f373838129c2 Some futher work on flib net client part
unc0rr
parents: 10428
diff changeset
   190
    msg.len:= len;
f373838129c2 Some futher work on flib net client part
unc0rr
parents: 10428
diff changeset
   191
    msg.buf:= GetMem(len);
f373838129c2 Some futher work on flib net client part
unc0rr
parents: 10428
diff changeset
   192
    Move(p^, msg.buf^, len);
11455
0c75fa9ce340 - Use queues instead of single buffer to communicate between threads
unc0rr
parents: 11423
diff changeset
   193
    ipcSend(msg, queueEngine)
10898
f373838129c2 Some futher work on flib net client part
unc0rr
parents: 10428
diff changeset
   194
end;
f373838129c2 Some futher work on flib net client part
unc0rr
parents: 10428
diff changeset
   195
10420
unc0rr
parents: 10418
diff changeset
   196
procedure ipcToFrontendRaw(p: pointer; len: Longword);
unc0rr
parents: 10418
diff changeset
   197
var msg: TIPCMessage;
unc0rr
parents: 10418
diff changeset
   198
begin
unc0rr
parents: 10418
diff changeset
   199
    msg.str[0]:= #0;
unc0rr
parents: 10418
diff changeset
   200
    msg.len:= len;
unc0rr
parents: 10418
diff changeset
   201
    msg.buf:= GetMem(len);
unc0rr
parents: 10418
diff changeset
   202
    Move(p^, msg.buf^, len);
11455
0c75fa9ce340 - Use queues instead of single buffer to communicate between threads
unc0rr
parents: 11423
diff changeset
   203
    ipcSend(msg, queueFrontend)
10420
unc0rr
parents: 10418
diff changeset
   204
end;
unc0rr
parents: 10418
diff changeset
   205
10935
3a65fcd7c335 Move SDLNet_TCP_Send to its own thread
unc0rr
parents: 10933
diff changeset
   206
procedure ipcToNetRaw(p: pointer; len: Longword);
3a65fcd7c335 Move SDLNet_TCP_Send to its own thread
unc0rr
parents: 10933
diff changeset
   207
var msg: TIPCMessage;
3a65fcd7c335 Move SDLNet_TCP_Send to its own thread
unc0rr
parents: 10933
diff changeset
   208
begin
3a65fcd7c335 Move SDLNet_TCP_Send to its own thread
unc0rr
parents: 10933
diff changeset
   209
    msg.str[0]:= #0;
3a65fcd7c335 Move SDLNet_TCP_Send to its own thread
unc0rr
parents: 10933
diff changeset
   210
    msg.len:= len;
3a65fcd7c335 Move SDLNet_TCP_Send to its own thread
unc0rr
parents: 10933
diff changeset
   211
    msg.buf:= GetMem(len);
3a65fcd7c335 Move SDLNet_TCP_Send to its own thread
unc0rr
parents: 10933
diff changeset
   212
    Move(p^, msg.buf^, len);
11455
0c75fa9ce340 - Use queues instead of single buffer to communicate between threads
unc0rr
parents: 11423
diff changeset
   213
    ipcSend(msg, queueNet)
10935
3a65fcd7c335 Move SDLNet_TCP_Send to its own thread
unc0rr
parents: 10933
diff changeset
   214
end;
3a65fcd7c335 Move SDLNet_TCP_Send to its own thread
unc0rr
parents: 10933
diff changeset
   215
10420
unc0rr
parents: 10418
diff changeset
   216
function ipcReadFromEngine: TIPCMessage;
10410
669bfa55cd70 Some work on new IPC, built with the use of mutexes and condition variables
unc0rr
parents: 10406
diff changeset
   217
begin
11455
0c75fa9ce340 - Use queues instead of single buffer to communicate between threads
unc0rr
parents: 11423
diff changeset
   218
    ipcReadFromEngine:= ipcRead(queueFrontend)
10412
9a8d4efcf3fa - More flib IPC routines
unc0rr
parents: 10410
diff changeset
   219
end;
10410
669bfa55cd70 Some work on new IPC, built with the use of mutexes and condition variables
unc0rr
parents: 10406
diff changeset
   220
11459
3c5d99013baf - Improve IPC handling in engine
unc0rr
parents: 11457
diff changeset
   221
function ipcReadFromFrontend: TIPCMessage;
10412
9a8d4efcf3fa - More flib IPC routines
unc0rr
parents: 10410
diff changeset
   222
begin
11459
3c5d99013baf - Improve IPC handling in engine
unc0rr
parents: 11457
diff changeset
   223
    ipcReadFromFrontend:= ipcRead(queueEngine)
10410
669bfa55cd70 Some work on new IPC, built with the use of mutexes and condition variables
unc0rr
parents: 10406
diff changeset
   224
end;
669bfa55cd70 Some work on new IPC, built with the use of mutexes and condition variables
unc0rr
parents: 10406
diff changeset
   225
10935
3a65fcd7c335 Move SDLNet_TCP_Send to its own thread
unc0rr
parents: 10933
diff changeset
   226
function ipcReadToNet: TIPCMessage;
3a65fcd7c335 Move SDLNet_TCP_Send to its own thread
unc0rr
parents: 10933
diff changeset
   227
begin
11455
0c75fa9ce340 - Use queues instead of single buffer to communicate between threads
unc0rr
parents: 11423
diff changeset
   228
    ipcReadToNet:= ipcRead(queueNet)
10935
3a65fcd7c335 Move SDLNet_TCP_Send to its own thread
unc0rr
parents: 10933
diff changeset
   229
end;
3a65fcd7c335 Move SDLNet_TCP_Send to its own thread
unc0rr
parents: 10933
diff changeset
   230
10410
669bfa55cd70 Some work on new IPC, built with the use of mutexes and condition variables
unc0rr
parents: 10406
diff changeset
   231
function ipcCheckFromEngine: boolean;
669bfa55cd70 Some work on new IPC, built with the use of mutexes and condition variables
unc0rr
parents: 10406
diff changeset
   232
begin
11455
0c75fa9ce340 - Use queues instead of single buffer to communicate between threads
unc0rr
parents: 11423
diff changeset
   233
    ipcCheckFromEngine:= ipcCheck(queueFrontend)
10412
9a8d4efcf3fa - More flib IPC routines
unc0rr
parents: 10410
diff changeset
   234
end;
9a8d4efcf3fa - More flib IPC routines
unc0rr
parents: 10410
diff changeset
   235
9a8d4efcf3fa - More flib IPC routines
unc0rr
parents: 10410
diff changeset
   236
function ipcCheckFromFrontend: boolean;
9a8d4efcf3fa - More flib IPC routines
unc0rr
parents: 10410
diff changeset
   237
begin
11455
0c75fa9ce340 - Use queues instead of single buffer to communicate between threads
unc0rr
parents: 11423
diff changeset
   238
    ipcCheckFromFrontend:= ipcCheck(queueEngine)
10410
669bfa55cd70 Some work on new IPC, built with the use of mutexes and condition variables
unc0rr
parents: 10406
diff changeset
   239
end;
669bfa55cd70 Some work on new IPC, built with the use of mutexes and condition variables
unc0rr
parents: 10406
diff changeset
   240
10933
f1da4126a61c Some more work on flib network
unc0rr
parents: 10898
diff changeset
   241
function  engineListener(p: pointer): Longint; cdecl; export;
10420
unc0rr
parents: 10418
diff changeset
   242
var msg: TIPCMessage;
10416
1c301054694d - Remove --port command
unc0rr
parents: 10412
diff changeset
   243
begin
10933
f1da4126a61c Some more work on flib network
unc0rr
parents: 10898
diff changeset
   244
    engineListener:= 0;
10416
1c301054694d - Remove --port command
unc0rr
parents: 10412
diff changeset
   245
    repeat
10420
unc0rr
parents: 10418
diff changeset
   246
        msg:= ipcReadFromEngine();
unc0rr
parents: 10418
diff changeset
   247
        if msg.buf = nil then
10935
3a65fcd7c335 Move SDLNet_TCP_Send to its own thread
unc0rr
parents: 10933
diff changeset
   248
            callbackFunctionF(callbackPointerF, @msg.str[1], byte(msg.str[0]))
10420
unc0rr
parents: 10418
diff changeset
   249
        else
unc0rr
parents: 10418
diff changeset
   250
        begin
10935
3a65fcd7c335 Move SDLNet_TCP_Send to its own thread
unc0rr
parents: 10933
diff changeset
   251
            callbackFunctionF(callbackPointerF, msg.buf, msg.len);
3a65fcd7c335 Move SDLNet_TCP_Send to its own thread
unc0rr
parents: 10933
diff changeset
   252
            FreeMem(msg.buf, msg.len)
3a65fcd7c335 Move SDLNet_TCP_Send to its own thread
unc0rr
parents: 10933
diff changeset
   253
        end
3a65fcd7c335 Move SDLNet_TCP_Send to its own thread
unc0rr
parents: 10933
diff changeset
   254
    until false
3a65fcd7c335 Move SDLNet_TCP_Send to its own thread
unc0rr
parents: 10933
diff changeset
   255
end;
3a65fcd7c335 Move SDLNet_TCP_Send to its own thread
unc0rr
parents: 10933
diff changeset
   256
3a65fcd7c335 Move SDLNet_TCP_Send to its own thread
unc0rr
parents: 10933
diff changeset
   257
function  netListener(p: pointer): Longint; cdecl; export;
3a65fcd7c335 Move SDLNet_TCP_Send to its own thread
unc0rr
parents: 10933
diff changeset
   258
var msg: TIPCMessage;
3a65fcd7c335 Move SDLNet_TCP_Send to its own thread
unc0rr
parents: 10933
diff changeset
   259
begin
3a65fcd7c335 Move SDLNet_TCP_Send to its own thread
unc0rr
parents: 10933
diff changeset
   260
    netListener:= 0;
3a65fcd7c335 Move SDLNet_TCP_Send to its own thread
unc0rr
parents: 10933
diff changeset
   261
    repeat
3a65fcd7c335 Move SDLNet_TCP_Send to its own thread
unc0rr
parents: 10933
diff changeset
   262
        msg:= ipcReadToNet();
3a65fcd7c335 Move SDLNet_TCP_Send to its own thread
unc0rr
parents: 10933
diff changeset
   263
        if msg.buf = nil then
3a65fcd7c335 Move SDLNet_TCP_Send to its own thread
unc0rr
parents: 10933
diff changeset
   264
            callbackFunctionN(callbackPointerN, @msg.str[1], byte(msg.str[0]))
3a65fcd7c335 Move SDLNet_TCP_Send to its own thread
unc0rr
parents: 10933
diff changeset
   265
        else
3a65fcd7c335 Move SDLNet_TCP_Send to its own thread
unc0rr
parents: 10933
diff changeset
   266
        begin
3a65fcd7c335 Move SDLNet_TCP_Send to its own thread
unc0rr
parents: 10933
diff changeset
   267
            callbackFunctionN(callbackPointerN, msg.buf, msg.len);
10420
unc0rr
parents: 10418
diff changeset
   268
            FreeMem(msg.buf, msg.len)
unc0rr
parents: 10418
diff changeset
   269
        end
10416
1c301054694d - Remove --port command
unc0rr
parents: 10412
diff changeset
   270
    until false
1c301054694d - Remove --port command
unc0rr
parents: 10412
diff changeset
   271
end;
1c301054694d - Remove --port command
unc0rr
parents: 10412
diff changeset
   272
10428
7c25297720f1 More refactoring: move PoC preview getting code into flib
unc0rr
parents: 10426
diff changeset
   273
procedure registerIPCCallback(p: pointer; f: TIPCCallback);
10416
1c301054694d - Remove --port command
unc0rr
parents: 10412
diff changeset
   274
begin
10935
3a65fcd7c335 Move SDLNet_TCP_Send to its own thread
unc0rr
parents: 10933
diff changeset
   275
    callbackPointerF:= p;
3a65fcd7c335 Move SDLNet_TCP_Send to its own thread
unc0rr
parents: 10933
diff changeset
   276
    callbackFunctionF:= f;
11408
b894922d58cc Merge default (add a bunch of FIXMEs)
unc0rr
parents: 10935
diff changeset
   277
    callbackListenerThreadF:= SDL_CreateThread(@engineListener, 'engineListener', nil);
10935
3a65fcd7c335 Move SDLNet_TCP_Send to its own thread
unc0rr
parents: 10933
diff changeset
   278
end;
3a65fcd7c335 Move SDLNet_TCP_Send to its own thread
unc0rr
parents: 10933
diff changeset
   279
3a65fcd7c335 Move SDLNet_TCP_Send to its own thread
unc0rr
parents: 10933
diff changeset
   280
procedure registerNetCallback(p: pointer; f: TIPCCallback);
3a65fcd7c335 Move SDLNet_TCP_Send to its own thread
unc0rr
parents: 10933
diff changeset
   281
begin
3a65fcd7c335 Move SDLNet_TCP_Send to its own thread
unc0rr
parents: 10933
diff changeset
   282
    callbackPointerN:= p;
3a65fcd7c335 Move SDLNet_TCP_Send to its own thread
unc0rr
parents: 10933
diff changeset
   283
    callbackFunctionN:= f;
11408
b894922d58cc Merge default (add a bunch of FIXMEs)
unc0rr
parents: 10935
diff changeset
   284
    callbackListenerThreadN:= SDL_CreateThread(@netListener, 'netListener', nil);
10416
1c301054694d - Remove --port command
unc0rr
parents: 10412
diff changeset
   285
end;
1c301054694d - Remove --port command
unc0rr
parents: 10412
diff changeset
   286
11455
0c75fa9ce340 - Use queues instead of single buffer to communicate between threads
unc0rr
parents: 11423
diff changeset
   287
function createQueue: PIPCQueue;
0c75fa9ce340 - Use queues instead of single buffer to communicate between threads
unc0rr
parents: 11423
diff changeset
   288
var q: PIPCQueue;
0c75fa9ce340 - Use queues instead of single buffer to communicate between threads
unc0rr
parents: 11423
diff changeset
   289
begin
0c75fa9ce340 - Use queues instead of single buffer to communicate between threads
unc0rr
parents: 11423
diff changeset
   290
    new(q);
0c75fa9ce340 - Use queues instead of single buffer to communicate between threads
unc0rr
parents: 11423
diff changeset
   291
    q^.msg.str:= '';
0c75fa9ce340 - Use queues instead of single buffer to communicate between threads
unc0rr
parents: 11423
diff changeset
   292
    q^.msg.buf:= nil;
0c75fa9ce340 - Use queues instead of single buffer to communicate between threads
unc0rr
parents: 11423
diff changeset
   293
    q^.mut:= SDL_CreateMutex;
0c75fa9ce340 - Use queues instead of single buffer to communicate between threads
unc0rr
parents: 11423
diff changeset
   294
    q^.cond:= SDL_CreateCond;
0c75fa9ce340 - Use queues instead of single buffer to communicate between threads
unc0rr
parents: 11423
diff changeset
   295
    q^.msg.next:= nil;
0c75fa9ce340 - Use queues instead of single buffer to communicate between threads
unc0rr
parents: 11423
diff changeset
   296
    q^.last:= @q^.msg;
0c75fa9ce340 - Use queues instead of single buffer to communicate between threads
unc0rr
parents: 11423
diff changeset
   297
    createQueue:= q
0c75fa9ce340 - Use queues instead of single buffer to communicate between threads
unc0rr
parents: 11423
diff changeset
   298
end;
0c75fa9ce340 - Use queues instead of single buffer to communicate between threads
unc0rr
parents: 11423
diff changeset
   299
0c75fa9ce340 - Use queues instead of single buffer to communicate between threads
unc0rr
parents: 11423
diff changeset
   300
procedure destroyQueue(queue: PIPCQueue);
0c75fa9ce340 - Use queues instead of single buffer to communicate between threads
unc0rr
parents: 11423
diff changeset
   301
begin
0c75fa9ce340 - Use queues instead of single buffer to communicate between threads
unc0rr
parents: 11423
diff changeset
   302
    SDL_DestroyCond(queue^.cond);
0c75fa9ce340 - Use queues instead of single buffer to communicate between threads
unc0rr
parents: 11423
diff changeset
   303
    SDL_DestroyMutex(queue^.mut);
0c75fa9ce340 - Use queues instead of single buffer to communicate between threads
unc0rr
parents: 11423
diff changeset
   304
    dispose(queue);
0c75fa9ce340 - Use queues instead of single buffer to communicate between threads
unc0rr
parents: 11423
diff changeset
   305
end;
0c75fa9ce340 - Use queues instead of single buffer to communicate between threads
unc0rr
parents: 11423
diff changeset
   306
10410
669bfa55cd70 Some work on new IPC, built with the use of mutexes and condition variables
unc0rr
parents: 10406
diff changeset
   307
procedure initIPC;
669bfa55cd70 Some work on new IPC, built with the use of mutexes and condition variables
unc0rr
parents: 10406
diff changeset
   308
begin
11455
0c75fa9ce340 - Use queues instead of single buffer to communicate between threads
unc0rr
parents: 11423
diff changeset
   309
    queueFrontend:= createQueue;
0c75fa9ce340 - Use queues instead of single buffer to communicate between threads
unc0rr
parents: 11423
diff changeset
   310
    queueEngine:= createQueue;
0c75fa9ce340 - Use queues instead of single buffer to communicate between threads
unc0rr
parents: 11423
diff changeset
   311
    queueNet:= createQueue;
10410
669bfa55cd70 Some work on new IPC, built with the use of mutexes and condition variables
unc0rr
parents: 10406
diff changeset
   312
10935
3a65fcd7c335 Move SDLNet_TCP_Send to its own thread
unc0rr
parents: 10933
diff changeset
   313
    callbackPointerF:= nil;
3a65fcd7c335 Move SDLNet_TCP_Send to its own thread
unc0rr
parents: 10933
diff changeset
   314
    callbackListenerThreadF:= nil;
10410
669bfa55cd70 Some work on new IPC, built with the use of mutexes and condition variables
unc0rr
parents: 10406
diff changeset
   315
end;
669bfa55cd70 Some work on new IPC, built with the use of mutexes and condition variables
unc0rr
parents: 10406
diff changeset
   316
669bfa55cd70 Some work on new IPC, built with the use of mutexes and condition variables
unc0rr
parents: 10406
diff changeset
   317
procedure freeIPC;
669bfa55cd70 Some work on new IPC, built with the use of mutexes and condition variables
unc0rr
parents: 10406
diff changeset
   318
begin
11408
b894922d58cc Merge default (add a bunch of FIXMEs)
unc0rr
parents: 10935
diff changeset
   319
    //FIXME SDL_KillThread(callbackListenerThreadF);
b894922d58cc Merge default (add a bunch of FIXMEs)
unc0rr
parents: 10935
diff changeset
   320
    //FIXME SDL_KillThread(callbackListenerThreadN);
11455
0c75fa9ce340 - Use queues instead of single buffer to communicate between threads
unc0rr
parents: 11423
diff changeset
   321
    destroyQueue(queueFrontend);
0c75fa9ce340 - Use queues instead of single buffer to communicate between threads
unc0rr
parents: 11423
diff changeset
   322
    destroyQueue(queueEngine);
0c75fa9ce340 - Use queues instead of single buffer to communicate between threads
unc0rr
parents: 11423
diff changeset
   323
    destroyQueue(queueNet);
10410
669bfa55cd70 Some work on new IPC, built with the use of mutexes and condition variables
unc0rr
parents: 10406
diff changeset
   324
end;
669bfa55cd70 Some work on new IPC, built with the use of mutexes and condition variables
unc0rr
parents: 10406
diff changeset
   325
10406
b5fd52ac760f Basic layout of frontlib, some more sdl bindings
unc0rr
parents:
diff changeset
   326
end.