hedgewars/uFLIPC.pas
author unc0rr
Sun, 22 Nov 2015 19:29:13 +0300
branchqmlfrontend
changeset 11431 ab6a6d9ebfc0
parent 11423 091149424aa4
child 11455 0c75fa9ce340
permissions -rw-r--r--
Small ui rework
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
10935
3a65fcd7c335 Move SDLNet_TCP_Send to its own thread
unc0rr
parents: 10933
diff changeset
     5
var msgFrontend, msgEngine, msgNet: TIPCMessage;
3a65fcd7c335 Move SDLNet_TCP_Send to its own thread
unc0rr
parents: 10933
diff changeset
     6
    mutFrontend, mutEngine, mutNet: PSDL_mutex;
3a65fcd7c335 Move SDLNet_TCP_Send to its own thread
unc0rr
parents: 10933
diff changeset
     7
    condFrontend, condEngine, condNet: PSDL_cond;
10410
669bfa55cd70 Some work on new IPC, built with the use of mutexes and condition variables
unc0rr
parents: 10406
diff changeset
     8
669bfa55cd70 Some work on new IPC, built with the use of mutexes and condition variables
unc0rr
parents: 10406
diff changeset
     9
procedure initIPC;
669bfa55cd70 Some work on new IPC, built with the use of mutexes and condition variables
unc0rr
parents: 10406
diff changeset
    10
procedure freeIPC;
669bfa55cd70 Some work on new IPC, built with the use of mutexes and condition variables
unc0rr
parents: 10406
diff changeset
    11
10428
7c25297720f1 More refactoring: move PoC preview getting code into flib
unc0rr
parents: 10426
diff changeset
    12
procedure ipcToEngine(s: shortstring);
10898
f373838129c2 Some futher work on flib net client part
unc0rr
parents: 10428
diff changeset
    13
procedure ipcToEngineRaw(p: pointer; len: Longword);
10420
unc0rr
parents: 10418
diff changeset
    14
//function  ipcReadFromEngine: shortstring;
unc0rr
parents: 10418
diff changeset
    15
//function  ipcCheckFromEngine: boolean;
10406
b5fd52ac760f Basic layout of frontlib, some more sdl bindings
unc0rr
parents:
diff changeset
    16
10935
3a65fcd7c335 Move SDLNet_TCP_Send to its own thread
unc0rr
parents: 10933
diff changeset
    17
procedure ipcToNet(s: shortstring);
3a65fcd7c335 Move SDLNet_TCP_Send to its own thread
unc0rr
parents: 10933
diff changeset
    18
procedure ipcToNetRaw(p: pointer; len: Longword);
3a65fcd7c335 Move SDLNet_TCP_Send to its own thread
unc0rr
parents: 10933
diff changeset
    19
10412
9a8d4efcf3fa - More flib IPC routines
unc0rr
parents: 10410
diff changeset
    20
procedure ipcToFrontend(s: shortstring);
10420
unc0rr
parents: 10418
diff changeset
    21
procedure ipcToFrontendRaw(p: pointer; len: Longword);
10412
9a8d4efcf3fa - More flib IPC routines
unc0rr
parents: 10410
diff changeset
    22
function ipcReadFromFrontend: shortstring;
9a8d4efcf3fa - More flib IPC routines
unc0rr
parents: 10410
diff changeset
    23
function ipcCheckFromFrontend: boolean;
9a8d4efcf3fa - More flib IPC routines
unc0rr
parents: 10410
diff changeset
    24
10428
7c25297720f1 More refactoring: move PoC preview getting code into flib
unc0rr
parents: 10426
diff changeset
    25
procedure registerIPCCallback(p: pointer; f: TIPCCallback);
10935
3a65fcd7c335 Move SDLNet_TCP_Send to its own thread
unc0rr
parents: 10933
diff changeset
    26
procedure registerNetCallback(p: pointer; f: TIPCCallback);
10416
1c301054694d - Remove --port command
unc0rr
parents: 10412
diff changeset
    27
10406
b5fd52ac760f Basic layout of frontlib, some more sdl bindings
unc0rr
parents:
diff changeset
    28
implementation
b5fd52ac760f Basic layout of frontlib, some more sdl bindings
unc0rr
parents:
diff changeset
    29
10935
3a65fcd7c335 Move SDLNet_TCP_Send to its own thread
unc0rr
parents: 10933
diff changeset
    30
var callbackPointerF: pointer;
3a65fcd7c335 Move SDLNet_TCP_Send to its own thread
unc0rr
parents: 10933
diff changeset
    31
    callbackFunctionF: TIPCCallback;
3a65fcd7c335 Move SDLNet_TCP_Send to its own thread
unc0rr
parents: 10933
diff changeset
    32
    callbackListenerThreadF: PSDL_Thread;
3a65fcd7c335 Move SDLNet_TCP_Send to its own thread
unc0rr
parents: 10933
diff changeset
    33
    callbackPointerN: pointer;
3a65fcd7c335 Move SDLNet_TCP_Send to its own thread
unc0rr
parents: 10933
diff changeset
    34
    callbackFunctionN: TIPCCallback;
3a65fcd7c335 Move SDLNet_TCP_Send to its own thread
unc0rr
parents: 10933
diff changeset
    35
    callbackListenerThreadN: PSDL_Thread;
10416
1c301054694d - Remove --port command
unc0rr
parents: 10412
diff changeset
    36
10420
unc0rr
parents: 10418
diff changeset
    37
procedure ipcSend(var s: TIPCMessage; var msg: TIPCMessage; mut: PSDL_mutex; cond: PSDL_cond);
10412
9a8d4efcf3fa - More flib IPC routines
unc0rr
parents: 10410
diff changeset
    38
begin
9a8d4efcf3fa - More flib IPC routines
unc0rr
parents: 10410
diff changeset
    39
    SDL_LockMutex(mut);
10418
091d2c0216c3 Move away from passing shortstrings into C code, now IPC works
unc0rr
parents: 10416
diff changeset
    40
10412
9a8d4efcf3fa - More flib IPC routines
unc0rr
parents: 10410
diff changeset
    41
    while (msg.str[0] > #0) or (msg.buf <> nil) do
9a8d4efcf3fa - More flib IPC routines
unc0rr
parents: 10410
diff changeset
    42
        SDL_CondWait(cond, mut);
9a8d4efcf3fa - More flib IPC routines
unc0rr
parents: 10410
diff changeset
    43
10420
unc0rr
parents: 10418
diff changeset
    44
    msg:= s;
10412
9a8d4efcf3fa - More flib IPC routines
unc0rr
parents: 10410
diff changeset
    45
    SDL_CondSignal(cond);
10416
1c301054694d - Remove --port command
unc0rr
parents: 10412
diff changeset
    46
    SDL_UnlockMutex(mut);
10412
9a8d4efcf3fa - More flib IPC routines
unc0rr
parents: 10410
diff changeset
    47
end;
9a8d4efcf3fa - More flib IPC routines
unc0rr
parents: 10410
diff changeset
    48
10420
unc0rr
parents: 10418
diff changeset
    49
function ipcRead(var msg: TIPCMessage; mut: PSDL_mutex; cond: PSDL_cond): TIPCMessage;
unc0rr
parents: 10418
diff changeset
    50
var tmp: pointer;
10412
9a8d4efcf3fa - More flib IPC routines
unc0rr
parents: 10410
diff changeset
    51
begin
9a8d4efcf3fa - More flib IPC routines
unc0rr
parents: 10410
diff changeset
    52
    SDL_LockMutex(mut);
9a8d4efcf3fa - More flib IPC routines
unc0rr
parents: 10410
diff changeset
    53
    while (msg.str[0] = #0) and (msg.buf = nil) do
9a8d4efcf3fa - More flib IPC routines
unc0rr
parents: 10410
diff changeset
    54
        SDL_CondWait(cond, mut);
9a8d4efcf3fa - More flib IPC routines
unc0rr
parents: 10410
diff changeset
    55
11423
091149424aa4 Handle ROOMS and ROOM ADD protocol commands
unc0rr
parents: 11408
diff changeset
    56
    if msg.buf <> nil then 
091149424aa4 Handle ROOMS and ROOM ADD protocol commands
unc0rr
parents: 11408
diff changeset
    57
// FIXME is this copying really needed, the buffer is in another thread already anyway?
10420
unc0rr
parents: 10418
diff changeset
    58
    begin
unc0rr
parents: 10418
diff changeset
    59
        tmp:= msg.buf;
unc0rr
parents: 10418
diff changeset
    60
        msg.buf:= GetMem(msg.len);
unc0rr
parents: 10418
diff changeset
    61
        Move(tmp^, msg.buf^, msg.len);
unc0rr
parents: 10418
diff changeset
    62
        FreeMem(tmp, msg.len)
unc0rr
parents: 10418
diff changeset
    63
    end;
unc0rr
parents: 10418
diff changeset
    64
unc0rr
parents: 10418
diff changeset
    65
    ipcRead:= msg;
10418
091d2c0216c3 Move away from passing shortstrings into C code, now IPC works
unc0rr
parents: 10416
diff changeset
    66
10412
9a8d4efcf3fa - More flib IPC routines
unc0rr
parents: 10410
diff changeset
    67
    msg.str[0]:= #0;
10420
unc0rr
parents: 10418
diff changeset
    68
    msg.buf:= nil;
10412
9a8d4efcf3fa - More flib IPC routines
unc0rr
parents: 10410
diff changeset
    69
9a8d4efcf3fa - More flib IPC routines
unc0rr
parents: 10410
diff changeset
    70
    SDL_CondSignal(cond);
9a8d4efcf3fa - More flib IPC routines
unc0rr
parents: 10410
diff changeset
    71
    SDL_UnlockMutex(mut)
9a8d4efcf3fa - More flib IPC routines
unc0rr
parents: 10410
diff changeset
    72
end;
9a8d4efcf3fa - More flib IPC routines
unc0rr
parents: 10410
diff changeset
    73
9a8d4efcf3fa - More flib IPC routines
unc0rr
parents: 10410
diff changeset
    74
function ipcCheck(var msg: TIPCMessage; mut: PSDL_mutex): boolean;
9a8d4efcf3fa - More flib IPC routines
unc0rr
parents: 10410
diff changeset
    75
begin
9a8d4efcf3fa - More flib IPC routines
unc0rr
parents: 10410
diff changeset
    76
    SDL_LockMutex(mut);
9a8d4efcf3fa - More flib IPC routines
unc0rr
parents: 10410
diff changeset
    77
    ipcCheck:= (msg.str[0] > #0) or (msg.buf <> nil);
9a8d4efcf3fa - More flib IPC routines
unc0rr
parents: 10410
diff changeset
    78
    SDL_UnlockMutex(mut)
9a8d4efcf3fa - More flib IPC routines
unc0rr
parents: 10410
diff changeset
    79
end;
9a8d4efcf3fa - More flib IPC routines
unc0rr
parents: 10410
diff changeset
    80
10428
7c25297720f1 More refactoring: move PoC preview getting code into flib
unc0rr
parents: 10426
diff changeset
    81
procedure ipcToEngine(s: shortstring);
10420
unc0rr
parents: 10418
diff changeset
    82
var msg: TIPCMessage;
10410
669bfa55cd70 Some work on new IPC, built with the use of mutexes and condition variables
unc0rr
parents: 10406
diff changeset
    83
begin
10428
7c25297720f1 More refactoring: move PoC preview getting code into flib
unc0rr
parents: 10426
diff changeset
    84
    msg.str:= s;
10420
unc0rr
parents: 10418
diff changeset
    85
    msg.buf:= nil;
unc0rr
parents: 10418
diff changeset
    86
    ipcSend(msg, msgEngine, mutEngine, condEngine)
10412
9a8d4efcf3fa - More flib IPC routines
unc0rr
parents: 10410
diff changeset
    87
end;
10410
669bfa55cd70 Some work on new IPC, built with the use of mutexes and condition variables
unc0rr
parents: 10406
diff changeset
    88
10412
9a8d4efcf3fa - More flib IPC routines
unc0rr
parents: 10410
diff changeset
    89
procedure ipcToFrontend(s: shortstring);
10420
unc0rr
parents: 10418
diff changeset
    90
var msg: TIPCMessage;
10412
9a8d4efcf3fa - More flib IPC routines
unc0rr
parents: 10410
diff changeset
    91
begin
10420
unc0rr
parents: 10418
diff changeset
    92
    msg.str:= s;
unc0rr
parents: 10418
diff changeset
    93
    msg.buf:= nil;
unc0rr
parents: 10418
diff changeset
    94
    ipcSend(msg, msgFrontend, mutFrontend, condFrontend)
10410
669bfa55cd70 Some work on new IPC, built with the use of mutexes and condition variables
unc0rr
parents: 10406
diff changeset
    95
end;
669bfa55cd70 Some work on new IPC, built with the use of mutexes and condition variables
unc0rr
parents: 10406
diff changeset
    96
10935
3a65fcd7c335 Move SDLNet_TCP_Send to its own thread
unc0rr
parents: 10933
diff changeset
    97
procedure ipcToNet(s: shortstring);
3a65fcd7c335 Move SDLNet_TCP_Send to its own thread
unc0rr
parents: 10933
diff changeset
    98
var msg: TIPCMessage;
3a65fcd7c335 Move SDLNet_TCP_Send to its own thread
unc0rr
parents: 10933
diff changeset
    99
begin
3a65fcd7c335 Move SDLNet_TCP_Send to its own thread
unc0rr
parents: 10933
diff changeset
   100
    msg.str:= s;
3a65fcd7c335 Move SDLNet_TCP_Send to its own thread
unc0rr
parents: 10933
diff changeset
   101
    msg.buf:= nil;
3a65fcd7c335 Move SDLNet_TCP_Send to its own thread
unc0rr
parents: 10933
diff changeset
   102
    ipcSend(msg, msgNet, mutNet, condNet)
3a65fcd7c335 Move SDLNet_TCP_Send to its own thread
unc0rr
parents: 10933
diff changeset
   103
end;
3a65fcd7c335 Move SDLNet_TCP_Send to its own thread
unc0rr
parents: 10933
diff changeset
   104
10898
f373838129c2 Some futher work on flib net client part
unc0rr
parents: 10428
diff changeset
   105
procedure ipcToEngineRaw(p: pointer; len: Longword);
f373838129c2 Some futher work on flib net client part
unc0rr
parents: 10428
diff changeset
   106
var msg: TIPCMessage;
f373838129c2 Some futher work on flib net client part
unc0rr
parents: 10428
diff changeset
   107
begin
f373838129c2 Some futher work on flib net client part
unc0rr
parents: 10428
diff changeset
   108
    msg.str[0]:= #0;
f373838129c2 Some futher work on flib net client part
unc0rr
parents: 10428
diff changeset
   109
    msg.len:= len;
f373838129c2 Some futher work on flib net client part
unc0rr
parents: 10428
diff changeset
   110
    msg.buf:= GetMem(len);
f373838129c2 Some futher work on flib net client part
unc0rr
parents: 10428
diff changeset
   111
    Move(p^, msg.buf^, len);
f373838129c2 Some futher work on flib net client part
unc0rr
parents: 10428
diff changeset
   112
    ipcSend(msg, msgEngine, mutEngine, condEngine)
f373838129c2 Some futher work on flib net client part
unc0rr
parents: 10428
diff changeset
   113
end;
f373838129c2 Some futher work on flib net client part
unc0rr
parents: 10428
diff changeset
   114
10420
unc0rr
parents: 10418
diff changeset
   115
procedure ipcToFrontendRaw(p: pointer; len: Longword);
unc0rr
parents: 10418
diff changeset
   116
var msg: TIPCMessage;
unc0rr
parents: 10418
diff changeset
   117
begin
unc0rr
parents: 10418
diff changeset
   118
    msg.str[0]:= #0;
unc0rr
parents: 10418
diff changeset
   119
    msg.len:= len;
unc0rr
parents: 10418
diff changeset
   120
    msg.buf:= GetMem(len);
unc0rr
parents: 10418
diff changeset
   121
    Move(p^, msg.buf^, len);
unc0rr
parents: 10418
diff changeset
   122
    ipcSend(msg, msgFrontend, mutFrontend, condFrontend)
unc0rr
parents: 10418
diff changeset
   123
end;
unc0rr
parents: 10418
diff changeset
   124
10935
3a65fcd7c335 Move SDLNet_TCP_Send to its own thread
unc0rr
parents: 10933
diff changeset
   125
procedure ipcToNetRaw(p: pointer; len: Longword);
3a65fcd7c335 Move SDLNet_TCP_Send to its own thread
unc0rr
parents: 10933
diff changeset
   126
var msg: TIPCMessage;
3a65fcd7c335 Move SDLNet_TCP_Send to its own thread
unc0rr
parents: 10933
diff changeset
   127
begin
3a65fcd7c335 Move SDLNet_TCP_Send to its own thread
unc0rr
parents: 10933
diff changeset
   128
    msg.str[0]:= #0;
3a65fcd7c335 Move SDLNet_TCP_Send to its own thread
unc0rr
parents: 10933
diff changeset
   129
    msg.len:= len;
3a65fcd7c335 Move SDLNet_TCP_Send to its own thread
unc0rr
parents: 10933
diff changeset
   130
    msg.buf:= GetMem(len);
3a65fcd7c335 Move SDLNet_TCP_Send to its own thread
unc0rr
parents: 10933
diff changeset
   131
    Move(p^, msg.buf^, len);
3a65fcd7c335 Move SDLNet_TCP_Send to its own thread
unc0rr
parents: 10933
diff changeset
   132
    ipcSend(msg, msgNet, mutNet, condNet)
3a65fcd7c335 Move SDLNet_TCP_Send to its own thread
unc0rr
parents: 10933
diff changeset
   133
end;
3a65fcd7c335 Move SDLNet_TCP_Send to its own thread
unc0rr
parents: 10933
diff changeset
   134
10420
unc0rr
parents: 10418
diff changeset
   135
function ipcReadFromEngine: TIPCMessage;
10410
669bfa55cd70 Some work on new IPC, built with the use of mutexes and condition variables
unc0rr
parents: 10406
diff changeset
   136
begin
10412
9a8d4efcf3fa - More flib IPC routines
unc0rr
parents: 10410
diff changeset
   137
    ipcReadFromEngine:= ipcRead(msgFrontend, mutFrontend, condFrontend)
9a8d4efcf3fa - More flib IPC routines
unc0rr
parents: 10410
diff changeset
   138
end;
10410
669bfa55cd70 Some work on new IPC, built with the use of mutexes and condition variables
unc0rr
parents: 10406
diff changeset
   139
10412
9a8d4efcf3fa - More flib IPC routines
unc0rr
parents: 10410
diff changeset
   140
function ipcReadFromFrontend: shortstring;
9a8d4efcf3fa - More flib IPC routines
unc0rr
parents: 10410
diff changeset
   141
begin
10420
unc0rr
parents: 10418
diff changeset
   142
    ipcReadFromFrontend:= ipcRead(msgEngine, mutEngine, condEngine).str
10410
669bfa55cd70 Some work on new IPC, built with the use of mutexes and condition variables
unc0rr
parents: 10406
diff changeset
   143
end;
669bfa55cd70 Some work on new IPC, built with the use of mutexes and condition variables
unc0rr
parents: 10406
diff changeset
   144
10935
3a65fcd7c335 Move SDLNet_TCP_Send to its own thread
unc0rr
parents: 10933
diff changeset
   145
function ipcReadToNet: TIPCMessage;
3a65fcd7c335 Move SDLNet_TCP_Send to its own thread
unc0rr
parents: 10933
diff changeset
   146
begin
3a65fcd7c335 Move SDLNet_TCP_Send to its own thread
unc0rr
parents: 10933
diff changeset
   147
    ipcReadToNet:= ipcRead(msgNet, mutNet, condNet)
3a65fcd7c335 Move SDLNet_TCP_Send to its own thread
unc0rr
parents: 10933
diff changeset
   148
end;
3a65fcd7c335 Move SDLNet_TCP_Send to its own thread
unc0rr
parents: 10933
diff changeset
   149
10410
669bfa55cd70 Some work on new IPC, built with the use of mutexes and condition variables
unc0rr
parents: 10406
diff changeset
   150
function ipcCheckFromEngine: boolean;
669bfa55cd70 Some work on new IPC, built with the use of mutexes and condition variables
unc0rr
parents: 10406
diff changeset
   151
begin
10412
9a8d4efcf3fa - More flib IPC routines
unc0rr
parents: 10410
diff changeset
   152
    ipcCheckFromEngine:= ipcCheck(msgFrontend, mutFrontend)
9a8d4efcf3fa - More flib IPC routines
unc0rr
parents: 10410
diff changeset
   153
end;
9a8d4efcf3fa - More flib IPC routines
unc0rr
parents: 10410
diff changeset
   154
9a8d4efcf3fa - More flib IPC routines
unc0rr
parents: 10410
diff changeset
   155
function ipcCheckFromFrontend: boolean;
9a8d4efcf3fa - More flib IPC routines
unc0rr
parents: 10410
diff changeset
   156
begin
9a8d4efcf3fa - More flib IPC routines
unc0rr
parents: 10410
diff changeset
   157
    ipcCheckFromFrontend:= ipcCheck(msgEngine, mutEngine)
10410
669bfa55cd70 Some work on new IPC, built with the use of mutexes and condition variables
unc0rr
parents: 10406
diff changeset
   158
end;
669bfa55cd70 Some work on new IPC, built with the use of mutexes and condition variables
unc0rr
parents: 10406
diff changeset
   159
10933
f1da4126a61c Some more work on flib network
unc0rr
parents: 10898
diff changeset
   160
function  engineListener(p: pointer): Longint; cdecl; export;
10420
unc0rr
parents: 10418
diff changeset
   161
var msg: TIPCMessage;
10416
1c301054694d - Remove --port command
unc0rr
parents: 10412
diff changeset
   162
begin
10933
f1da4126a61c Some more work on flib network
unc0rr
parents: 10898
diff changeset
   163
    engineListener:= 0;
10416
1c301054694d - Remove --port command
unc0rr
parents: 10412
diff changeset
   164
    repeat
10420
unc0rr
parents: 10418
diff changeset
   165
        msg:= ipcReadFromEngine();
unc0rr
parents: 10418
diff changeset
   166
        if msg.buf = nil then
10935
3a65fcd7c335 Move SDLNet_TCP_Send to its own thread
unc0rr
parents: 10933
diff changeset
   167
            callbackFunctionF(callbackPointerF, @msg.str[1], byte(msg.str[0]))
10420
unc0rr
parents: 10418
diff changeset
   168
        else
unc0rr
parents: 10418
diff changeset
   169
        begin
10935
3a65fcd7c335 Move SDLNet_TCP_Send to its own thread
unc0rr
parents: 10933
diff changeset
   170
            callbackFunctionF(callbackPointerF, msg.buf, msg.len);
3a65fcd7c335 Move SDLNet_TCP_Send to its own thread
unc0rr
parents: 10933
diff changeset
   171
            FreeMem(msg.buf, msg.len)
3a65fcd7c335 Move SDLNet_TCP_Send to its own thread
unc0rr
parents: 10933
diff changeset
   172
        end
3a65fcd7c335 Move SDLNet_TCP_Send to its own thread
unc0rr
parents: 10933
diff changeset
   173
    until false
3a65fcd7c335 Move SDLNet_TCP_Send to its own thread
unc0rr
parents: 10933
diff changeset
   174
end;
3a65fcd7c335 Move SDLNet_TCP_Send to its own thread
unc0rr
parents: 10933
diff changeset
   175
3a65fcd7c335 Move SDLNet_TCP_Send to its own thread
unc0rr
parents: 10933
diff changeset
   176
function  netListener(p: pointer): Longint; cdecl; export;
3a65fcd7c335 Move SDLNet_TCP_Send to its own thread
unc0rr
parents: 10933
diff changeset
   177
var msg: TIPCMessage;
3a65fcd7c335 Move SDLNet_TCP_Send to its own thread
unc0rr
parents: 10933
diff changeset
   178
begin
3a65fcd7c335 Move SDLNet_TCP_Send to its own thread
unc0rr
parents: 10933
diff changeset
   179
    netListener:= 0;
3a65fcd7c335 Move SDLNet_TCP_Send to its own thread
unc0rr
parents: 10933
diff changeset
   180
    repeat
3a65fcd7c335 Move SDLNet_TCP_Send to its own thread
unc0rr
parents: 10933
diff changeset
   181
        msg:= ipcReadToNet();
3a65fcd7c335 Move SDLNet_TCP_Send to its own thread
unc0rr
parents: 10933
diff changeset
   182
        if msg.buf = nil then
3a65fcd7c335 Move SDLNet_TCP_Send to its own thread
unc0rr
parents: 10933
diff changeset
   183
            callbackFunctionN(callbackPointerN, @msg.str[1], byte(msg.str[0]))
3a65fcd7c335 Move SDLNet_TCP_Send to its own thread
unc0rr
parents: 10933
diff changeset
   184
        else
3a65fcd7c335 Move SDLNet_TCP_Send to its own thread
unc0rr
parents: 10933
diff changeset
   185
        begin
3a65fcd7c335 Move SDLNet_TCP_Send to its own thread
unc0rr
parents: 10933
diff changeset
   186
            callbackFunctionN(callbackPointerN, msg.buf, msg.len);
10420
unc0rr
parents: 10418
diff changeset
   187
            FreeMem(msg.buf, msg.len)
unc0rr
parents: 10418
diff changeset
   188
        end
10416
1c301054694d - Remove --port command
unc0rr
parents: 10412
diff changeset
   189
    until false
1c301054694d - Remove --port command
unc0rr
parents: 10412
diff changeset
   190
end;
1c301054694d - Remove --port command
unc0rr
parents: 10412
diff changeset
   191
10428
7c25297720f1 More refactoring: move PoC preview getting code into flib
unc0rr
parents: 10426
diff changeset
   192
procedure registerIPCCallback(p: pointer; f: TIPCCallback);
10416
1c301054694d - Remove --port command
unc0rr
parents: 10412
diff changeset
   193
begin
10935
3a65fcd7c335 Move SDLNet_TCP_Send to its own thread
unc0rr
parents: 10933
diff changeset
   194
    callbackPointerF:= p;
3a65fcd7c335 Move SDLNet_TCP_Send to its own thread
unc0rr
parents: 10933
diff changeset
   195
    callbackFunctionF:= f;
11408
b894922d58cc Merge default (add a bunch of FIXMEs)
unc0rr
parents: 10935
diff changeset
   196
    callbackListenerThreadF:= SDL_CreateThread(@engineListener, 'engineListener', nil);
10935
3a65fcd7c335 Move SDLNet_TCP_Send to its own thread
unc0rr
parents: 10933
diff changeset
   197
end;
3a65fcd7c335 Move SDLNet_TCP_Send to its own thread
unc0rr
parents: 10933
diff changeset
   198
3a65fcd7c335 Move SDLNet_TCP_Send to its own thread
unc0rr
parents: 10933
diff changeset
   199
procedure registerNetCallback(p: pointer; f: TIPCCallback);
3a65fcd7c335 Move SDLNet_TCP_Send to its own thread
unc0rr
parents: 10933
diff changeset
   200
begin
3a65fcd7c335 Move SDLNet_TCP_Send to its own thread
unc0rr
parents: 10933
diff changeset
   201
    callbackPointerN:= p;
3a65fcd7c335 Move SDLNet_TCP_Send to its own thread
unc0rr
parents: 10933
diff changeset
   202
    callbackFunctionN:= f;
11408
b894922d58cc Merge default (add a bunch of FIXMEs)
unc0rr
parents: 10935
diff changeset
   203
    callbackListenerThreadN:= SDL_CreateThread(@netListener, 'netListener', nil);
10416
1c301054694d - Remove --port command
unc0rr
parents: 10412
diff changeset
   204
end;
1c301054694d - Remove --port command
unc0rr
parents: 10412
diff changeset
   205
10410
669bfa55cd70 Some work on new IPC, built with the use of mutexes and condition variables
unc0rr
parents: 10406
diff changeset
   206
procedure initIPC;
669bfa55cd70 Some work on new IPC, built with the use of mutexes and condition variables
unc0rr
parents: 10406
diff changeset
   207
begin
669bfa55cd70 Some work on new IPC, built with the use of mutexes and condition variables
unc0rr
parents: 10406
diff changeset
   208
    msgFrontend.str:= '';
669bfa55cd70 Some work on new IPC, built with the use of mutexes and condition variables
unc0rr
parents: 10406
diff changeset
   209
    msgFrontend.buf:= nil;
669bfa55cd70 Some work on new IPC, built with the use of mutexes and condition variables
unc0rr
parents: 10406
diff changeset
   210
    msgEngine.str:= '';
669bfa55cd70 Some work on new IPC, built with the use of mutexes and condition variables
unc0rr
parents: 10406
diff changeset
   211
    msgEngine.buf:= nil;
10935
3a65fcd7c335 Move SDLNet_TCP_Send to its own thread
unc0rr
parents: 10933
diff changeset
   212
    msgNet.str:= '';
3a65fcd7c335 Move SDLNet_TCP_Send to its own thread
unc0rr
parents: 10933
diff changeset
   213
    msgNet.buf:= nil;
10410
669bfa55cd70 Some work on new IPC, built with the use of mutexes and condition variables
unc0rr
parents: 10406
diff changeset
   214
10935
3a65fcd7c335 Move SDLNet_TCP_Send to its own thread
unc0rr
parents: 10933
diff changeset
   215
    callbackPointerF:= nil;
3a65fcd7c335 Move SDLNet_TCP_Send to its own thread
unc0rr
parents: 10933
diff changeset
   216
    callbackListenerThreadF:= nil;
10416
1c301054694d - Remove --port command
unc0rr
parents: 10412
diff changeset
   217
10410
669bfa55cd70 Some work on new IPC, built with the use of mutexes and condition variables
unc0rr
parents: 10406
diff changeset
   218
    mutFrontend:= SDL_CreateMutex;
669bfa55cd70 Some work on new IPC, built with the use of mutexes and condition variables
unc0rr
parents: 10406
diff changeset
   219
    mutEngine:= SDL_CreateMutex;
10935
3a65fcd7c335 Move SDLNet_TCP_Send to its own thread
unc0rr
parents: 10933
diff changeset
   220
    mutNet:= SDL_CreateMutex;
10410
669bfa55cd70 Some work on new IPC, built with the use of mutexes and condition variables
unc0rr
parents: 10406
diff changeset
   221
    condFrontend:= SDL_CreateCond;
669bfa55cd70 Some work on new IPC, built with the use of mutexes and condition variables
unc0rr
parents: 10406
diff changeset
   222
    condEngine:= SDL_CreateCond;
10935
3a65fcd7c335 Move SDLNet_TCP_Send to its own thread
unc0rr
parents: 10933
diff changeset
   223
    condNet:= SDL_CreateCond;
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
669bfa55cd70 Some work on new IPC, built with the use of mutexes and condition variables
unc0rr
parents: 10406
diff changeset
   226
procedure freeIPC;
669bfa55cd70 Some work on new IPC, built with the use of mutexes and condition variables
unc0rr
parents: 10406
diff changeset
   227
begin
11408
b894922d58cc Merge default (add a bunch of FIXMEs)
unc0rr
parents: 10935
diff changeset
   228
    //FIXME SDL_KillThread(callbackListenerThreadF);
b894922d58cc Merge default (add a bunch of FIXMEs)
unc0rr
parents: 10935
diff changeset
   229
    //FIXME SDL_KillThread(callbackListenerThreadN);
10410
669bfa55cd70 Some work on new IPC, built with the use of mutexes and condition variables
unc0rr
parents: 10406
diff changeset
   230
    SDL_DestroyMutex(mutFrontend);
669bfa55cd70 Some work on new IPC, built with the use of mutexes and condition variables
unc0rr
parents: 10406
diff changeset
   231
    SDL_DestroyMutex(mutEngine);
10935
3a65fcd7c335 Move SDLNet_TCP_Send to its own thread
unc0rr
parents: 10933
diff changeset
   232
    SDL_DestroyMutex(mutNet);
10410
669bfa55cd70 Some work on new IPC, built with the use of mutexes and condition variables
unc0rr
parents: 10406
diff changeset
   233
    SDL_DestroyCond(condFrontend);
669bfa55cd70 Some work on new IPC, built with the use of mutexes and condition variables
unc0rr
parents: 10406
diff changeset
   234
    SDL_DestroyCond(condEngine);
10935
3a65fcd7c335 Move SDLNet_TCP_Send to its own thread
unc0rr
parents: 10933
diff changeset
   235
    SDL_DestroyCond(condNet);
10410
669bfa55cd70 Some work on new IPC, built with the use of mutexes and condition variables
unc0rr
parents: 10406
diff changeset
   236
end;
669bfa55cd70 Some work on new IPC, built with the use of mutexes and condition variables
unc0rr
parents: 10406
diff changeset
   237
10406
b5fd52ac760f Basic layout of frontlib, some more sdl bindings
unc0rr
parents:
diff changeset
   238
end.