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