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