hedgewars/uFLIPC.pas
author unc0rr
Sat, 25 Apr 2015 23:46:09 +0300
branchqmlfrontend
changeset 10900 6a805e822074
parent 10898 f373838129c2
child 10933 f1da4126a61c
permissions -rw-r--r--
Some hedgewars coding a week keeps doctor away
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
var msgFrontend, msgEngine: TIPCMessage;
669bfa55cd70 Some work on new IPC, built with the use of mutexes and condition variables
unc0rr
parents: 10406
diff changeset
     6
    mutFrontend, mutEngine: PSDL_mutex;
669bfa55cd70 Some work on new IPC, built with the use of mutexes and condition variables
unc0rr
parents: 10406
diff changeset
     7
    condFrontend, condEngine: PSDL_cond;
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
10412
9a8d4efcf3fa - More flib IPC routines
unc0rr
parents: 10410
diff changeset
    17
procedure ipcToFrontend(s: shortstring);
10420
unc0rr
parents: 10418
diff changeset
    18
procedure ipcToFrontendRaw(p: pointer; len: Longword);
10412
9a8d4efcf3fa - More flib IPC routines
unc0rr
parents: 10410
diff changeset
    19
function ipcReadFromFrontend: shortstring;
9a8d4efcf3fa - More flib IPC routines
unc0rr
parents: 10410
diff changeset
    20
function ipcCheckFromFrontend: boolean;
9a8d4efcf3fa - More flib IPC routines
unc0rr
parents: 10410
diff changeset
    21
10428
7c25297720f1 More refactoring: move PoC preview getting code into flib
unc0rr
parents: 10426
diff changeset
    22
procedure registerIPCCallback(p: pointer; f: TIPCCallback);
10416
1c301054694d - Remove --port command
unc0rr
parents: 10412
diff changeset
    23
10406
b5fd52ac760f Basic layout of frontlib, some more sdl bindings
unc0rr
parents:
diff changeset
    24
implementation
b5fd52ac760f Basic layout of frontlib, some more sdl bindings
unc0rr
parents:
diff changeset
    25
10416
1c301054694d - Remove --port command
unc0rr
parents: 10412
diff changeset
    26
var callbackPointer: pointer;
1c301054694d - Remove --port command
unc0rr
parents: 10412
diff changeset
    27
    callbackFunction: TIPCCallback;
1c301054694d - Remove --port command
unc0rr
parents: 10412
diff changeset
    28
    callbackListenerThread: PSDL_Thread;
1c301054694d - Remove --port command
unc0rr
parents: 10412
diff changeset
    29
10420
unc0rr
parents: 10418
diff changeset
    30
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
    31
begin
9a8d4efcf3fa - More flib IPC routines
unc0rr
parents: 10410
diff changeset
    32
    SDL_LockMutex(mut);
10418
091d2c0216c3 Move away from passing shortstrings into C code, now IPC works
unc0rr
parents: 10416
diff changeset
    33
10412
9a8d4efcf3fa - More flib IPC routines
unc0rr
parents: 10410
diff changeset
    34
    while (msg.str[0] > #0) or (msg.buf <> nil) do
9a8d4efcf3fa - More flib IPC routines
unc0rr
parents: 10410
diff changeset
    35
        SDL_CondWait(cond, mut);
9a8d4efcf3fa - More flib IPC routines
unc0rr
parents: 10410
diff changeset
    36
10420
unc0rr
parents: 10418
diff changeset
    37
    msg:= s;
10412
9a8d4efcf3fa - More flib IPC routines
unc0rr
parents: 10410
diff changeset
    38
    SDL_CondSignal(cond);
10416
1c301054694d - Remove --port command
unc0rr
parents: 10412
diff changeset
    39
    SDL_UnlockMutex(mut);
10412
9a8d4efcf3fa - More flib IPC routines
unc0rr
parents: 10410
diff changeset
    40
end;
9a8d4efcf3fa - More flib IPC routines
unc0rr
parents: 10410
diff changeset
    41
10420
unc0rr
parents: 10418
diff changeset
    42
function ipcRead(var msg: TIPCMessage; mut: PSDL_mutex; cond: PSDL_cond): TIPCMessage;
unc0rr
parents: 10418
diff changeset
    43
var tmp: pointer;
10412
9a8d4efcf3fa - More flib IPC routines
unc0rr
parents: 10410
diff changeset
    44
begin
9a8d4efcf3fa - More flib IPC routines
unc0rr
parents: 10410
diff changeset
    45
    SDL_LockMutex(mut);
9a8d4efcf3fa - More flib IPC routines
unc0rr
parents: 10410
diff changeset
    46
    while (msg.str[0] = #0) and (msg.buf = nil) do
9a8d4efcf3fa - More flib IPC routines
unc0rr
parents: 10410
diff changeset
    47
        SDL_CondWait(cond, mut);
9a8d4efcf3fa - More flib IPC routines
unc0rr
parents: 10410
diff changeset
    48
10420
unc0rr
parents: 10418
diff changeset
    49
    if msg.buf <> nil then
unc0rr
parents: 10418
diff changeset
    50
    begin
unc0rr
parents: 10418
diff changeset
    51
        tmp:= msg.buf;
unc0rr
parents: 10418
diff changeset
    52
        msg.buf:= GetMem(msg.len);
unc0rr
parents: 10418
diff changeset
    53
        Move(tmp^, msg.buf^, msg.len);
unc0rr
parents: 10418
diff changeset
    54
        FreeMem(tmp, msg.len)
unc0rr
parents: 10418
diff changeset
    55
    end;
unc0rr
parents: 10418
diff changeset
    56
unc0rr
parents: 10418
diff changeset
    57
    ipcRead:= msg;
10418
091d2c0216c3 Move away from passing shortstrings into C code, now IPC works
unc0rr
parents: 10416
diff changeset
    58
10412
9a8d4efcf3fa - More flib IPC routines
unc0rr
parents: 10410
diff changeset
    59
    msg.str[0]:= #0;
10420
unc0rr
parents: 10418
diff changeset
    60
    msg.buf:= nil;
10412
9a8d4efcf3fa - More flib IPC routines
unc0rr
parents: 10410
diff changeset
    61
9a8d4efcf3fa - More flib IPC routines
unc0rr
parents: 10410
diff changeset
    62
    SDL_CondSignal(cond);
9a8d4efcf3fa - More flib IPC routines
unc0rr
parents: 10410
diff changeset
    63
    SDL_UnlockMutex(mut)
9a8d4efcf3fa - More flib IPC routines
unc0rr
parents: 10410
diff changeset
    64
end;
9a8d4efcf3fa - More flib IPC routines
unc0rr
parents: 10410
diff changeset
    65
9a8d4efcf3fa - More flib IPC routines
unc0rr
parents: 10410
diff changeset
    66
function ipcCheck(var msg: TIPCMessage; mut: PSDL_mutex): boolean;
9a8d4efcf3fa - More flib IPC routines
unc0rr
parents: 10410
diff changeset
    67
begin
9a8d4efcf3fa - More flib IPC routines
unc0rr
parents: 10410
diff changeset
    68
    SDL_LockMutex(mut);
9a8d4efcf3fa - More flib IPC routines
unc0rr
parents: 10410
diff changeset
    69
    ipcCheck:= (msg.str[0] > #0) or (msg.buf <> nil);
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
10428
7c25297720f1 More refactoring: move PoC preview getting code into flib
unc0rr
parents: 10426
diff changeset
    73
procedure ipcToEngine(s: shortstring);
10420
unc0rr
parents: 10418
diff changeset
    74
var msg: TIPCMessage;
10410
669bfa55cd70 Some work on new IPC, built with the use of mutexes and condition variables
unc0rr
parents: 10406
diff changeset
    75
begin
10428
7c25297720f1 More refactoring: move PoC preview getting code into flib
unc0rr
parents: 10426
diff changeset
    76
    msg.str:= s;
10420
unc0rr
parents: 10418
diff changeset
    77
    msg.buf:= nil;
unc0rr
parents: 10418
diff changeset
    78
    ipcSend(msg, msgEngine, mutEngine, condEngine)
10412
9a8d4efcf3fa - More flib IPC routines
unc0rr
parents: 10410
diff changeset
    79
end;
10410
669bfa55cd70 Some work on new IPC, built with the use of mutexes and condition variables
unc0rr
parents: 10406
diff changeset
    80
10412
9a8d4efcf3fa - More flib IPC routines
unc0rr
parents: 10410
diff changeset
    81
procedure ipcToFrontend(s: shortstring);
10420
unc0rr
parents: 10418
diff changeset
    82
var msg: TIPCMessage;
10412
9a8d4efcf3fa - More flib IPC routines
unc0rr
parents: 10410
diff changeset
    83
begin
10420
unc0rr
parents: 10418
diff changeset
    84
    msg.str:= s;
unc0rr
parents: 10418
diff changeset
    85
    msg.buf:= nil;
unc0rr
parents: 10418
diff changeset
    86
    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
    87
end;
669bfa55cd70 Some work on new IPC, built with the use of mutexes and condition variables
unc0rr
parents: 10406
diff changeset
    88
10898
f373838129c2 Some futher work on flib net client part
unc0rr
parents: 10428
diff changeset
    89
procedure ipcToEngineRaw(p: pointer; len: Longword);
f373838129c2 Some futher work on flib net client part
unc0rr
parents: 10428
diff changeset
    90
var msg: TIPCMessage;
f373838129c2 Some futher work on flib net client part
unc0rr
parents: 10428
diff changeset
    91
begin
f373838129c2 Some futher work on flib net client part
unc0rr
parents: 10428
diff changeset
    92
    msg.str[0]:= #0;
f373838129c2 Some futher work on flib net client part
unc0rr
parents: 10428
diff changeset
    93
    msg.len:= len;
f373838129c2 Some futher work on flib net client part
unc0rr
parents: 10428
diff changeset
    94
    msg.buf:= GetMem(len);
f373838129c2 Some futher work on flib net client part
unc0rr
parents: 10428
diff changeset
    95
    Move(p^, msg.buf^, len);
f373838129c2 Some futher work on flib net client part
unc0rr
parents: 10428
diff changeset
    96
    ipcSend(msg, msgEngine, mutEngine, condEngine)
f373838129c2 Some futher work on flib net client part
unc0rr
parents: 10428
diff changeset
    97
end;
f373838129c2 Some futher work on flib net client part
unc0rr
parents: 10428
diff changeset
    98
10420
unc0rr
parents: 10418
diff changeset
    99
procedure ipcToFrontendRaw(p: pointer; len: Longword);
unc0rr
parents: 10418
diff changeset
   100
var msg: TIPCMessage;
unc0rr
parents: 10418
diff changeset
   101
begin
unc0rr
parents: 10418
diff changeset
   102
    msg.str[0]:= #0;
unc0rr
parents: 10418
diff changeset
   103
    msg.len:= len;
unc0rr
parents: 10418
diff changeset
   104
    msg.buf:= GetMem(len);
unc0rr
parents: 10418
diff changeset
   105
    Move(p^, msg.buf^, len);
unc0rr
parents: 10418
diff changeset
   106
    ipcSend(msg, msgFrontend, mutFrontend, condFrontend)
unc0rr
parents: 10418
diff changeset
   107
end;
unc0rr
parents: 10418
diff changeset
   108
unc0rr
parents: 10418
diff changeset
   109
function ipcReadFromEngine: TIPCMessage;
10410
669bfa55cd70 Some work on new IPC, built with the use of mutexes and condition variables
unc0rr
parents: 10406
diff changeset
   110
begin
10412
9a8d4efcf3fa - More flib IPC routines
unc0rr
parents: 10410
diff changeset
   111
    ipcReadFromEngine:= ipcRead(msgFrontend, mutFrontend, condFrontend)
9a8d4efcf3fa - More flib IPC routines
unc0rr
parents: 10410
diff changeset
   112
end;
10410
669bfa55cd70 Some work on new IPC, built with the use of mutexes and condition variables
unc0rr
parents: 10406
diff changeset
   113
10412
9a8d4efcf3fa - More flib IPC routines
unc0rr
parents: 10410
diff changeset
   114
function ipcReadFromFrontend: shortstring;
9a8d4efcf3fa - More flib IPC routines
unc0rr
parents: 10410
diff changeset
   115
begin
10420
unc0rr
parents: 10418
diff changeset
   116
    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
   117
end;
669bfa55cd70 Some work on new IPC, built with the use of mutexes and condition variables
unc0rr
parents: 10406
diff changeset
   118
669bfa55cd70 Some work on new IPC, built with the use of mutexes and condition variables
unc0rr
parents: 10406
diff changeset
   119
function ipcCheckFromEngine: boolean;
669bfa55cd70 Some work on new IPC, built with the use of mutexes and condition variables
unc0rr
parents: 10406
diff changeset
   120
begin
10412
9a8d4efcf3fa - More flib IPC routines
unc0rr
parents: 10410
diff changeset
   121
    ipcCheckFromEngine:= ipcCheck(msgFrontend, mutFrontend)
9a8d4efcf3fa - More flib IPC routines
unc0rr
parents: 10410
diff changeset
   122
end;
9a8d4efcf3fa - More flib IPC routines
unc0rr
parents: 10410
diff changeset
   123
9a8d4efcf3fa - More flib IPC routines
unc0rr
parents: 10410
diff changeset
   124
function ipcCheckFromFrontend: boolean;
9a8d4efcf3fa - More flib IPC routines
unc0rr
parents: 10410
diff changeset
   125
begin
9a8d4efcf3fa - More flib IPC routines
unc0rr
parents: 10410
diff changeset
   126
    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
   127
end;
669bfa55cd70 Some work on new IPC, built with the use of mutexes and condition variables
unc0rr
parents: 10406
diff changeset
   128
10416
1c301054694d - Remove --port command
unc0rr
parents: 10412
diff changeset
   129
function  listener(p: pointer): Longint; cdecl; export;
10420
unc0rr
parents: 10418
diff changeset
   130
var msg: TIPCMessage;
10416
1c301054694d - Remove --port command
unc0rr
parents: 10412
diff changeset
   131
begin
1c301054694d - Remove --port command
unc0rr
parents: 10412
diff changeset
   132
    listener:= 0;
1c301054694d - Remove --port command
unc0rr
parents: 10412
diff changeset
   133
    repeat
10420
unc0rr
parents: 10418
diff changeset
   134
        msg:= ipcReadFromEngine();
unc0rr
parents: 10418
diff changeset
   135
        if msg.buf = nil then
unc0rr
parents: 10418
diff changeset
   136
            callbackFunction(callbackPointer, @msg.str[1], byte(msg.str[0]))
unc0rr
parents: 10418
diff changeset
   137
        else
unc0rr
parents: 10418
diff changeset
   138
        begin
unc0rr
parents: 10418
diff changeset
   139
            callbackFunction(callbackPointer, msg.buf, msg.len);
unc0rr
parents: 10418
diff changeset
   140
            FreeMem(msg.buf, msg.len)
unc0rr
parents: 10418
diff changeset
   141
        end
10416
1c301054694d - Remove --port command
unc0rr
parents: 10412
diff changeset
   142
    until false
1c301054694d - Remove --port command
unc0rr
parents: 10412
diff changeset
   143
end;
1c301054694d - Remove --port command
unc0rr
parents: 10412
diff changeset
   144
10428
7c25297720f1 More refactoring: move PoC preview getting code into flib
unc0rr
parents: 10426
diff changeset
   145
procedure registerIPCCallback(p: pointer; f: TIPCCallback);
10416
1c301054694d - Remove --port command
unc0rr
parents: 10412
diff changeset
   146
begin
1c301054694d - Remove --port command
unc0rr
parents: 10412
diff changeset
   147
    callbackPointer:= p;
1c301054694d - Remove --port command
unc0rr
parents: 10412
diff changeset
   148
    callbackFunction:= f;
1c301054694d - Remove --port command
unc0rr
parents: 10412
diff changeset
   149
    callbackListenerThread:= SDL_CreateThread(@listener{$IFDEF SDL2}, 'ipcListener'{$ENDIF}, nil);
1c301054694d - Remove --port command
unc0rr
parents: 10412
diff changeset
   150
end;
1c301054694d - Remove --port command
unc0rr
parents: 10412
diff changeset
   151
10410
669bfa55cd70 Some work on new IPC, built with the use of mutexes and condition variables
unc0rr
parents: 10406
diff changeset
   152
procedure initIPC;
669bfa55cd70 Some work on new IPC, built with the use of mutexes and condition variables
unc0rr
parents: 10406
diff changeset
   153
begin
669bfa55cd70 Some work on new IPC, built with the use of mutexes and condition variables
unc0rr
parents: 10406
diff changeset
   154
    msgFrontend.str:= '';
669bfa55cd70 Some work on new IPC, built with the use of mutexes and condition variables
unc0rr
parents: 10406
diff changeset
   155
    msgFrontend.buf:= nil;
669bfa55cd70 Some work on new IPC, built with the use of mutexes and condition variables
unc0rr
parents: 10406
diff changeset
   156
    msgEngine.str:= '';
669bfa55cd70 Some work on new IPC, built with the use of mutexes and condition variables
unc0rr
parents: 10406
diff changeset
   157
    msgEngine.buf:= nil;
669bfa55cd70 Some work on new IPC, built with the use of mutexes and condition variables
unc0rr
parents: 10406
diff changeset
   158
10416
1c301054694d - Remove --port command
unc0rr
parents: 10412
diff changeset
   159
    callbackPointer:= nil;
1c301054694d - Remove --port command
unc0rr
parents: 10412
diff changeset
   160
    callbackListenerThread:= nil;
1c301054694d - Remove --port command
unc0rr
parents: 10412
diff changeset
   161
10410
669bfa55cd70 Some work on new IPC, built with the use of mutexes and condition variables
unc0rr
parents: 10406
diff changeset
   162
    mutFrontend:= SDL_CreateMutex;
669bfa55cd70 Some work on new IPC, built with the use of mutexes and condition variables
unc0rr
parents: 10406
diff changeset
   163
    mutEngine:= SDL_CreateMutex;
669bfa55cd70 Some work on new IPC, built with the use of mutexes and condition variables
unc0rr
parents: 10406
diff changeset
   164
    condFrontend:= SDL_CreateCond;
669bfa55cd70 Some work on new IPC, built with the use of mutexes and condition variables
unc0rr
parents: 10406
diff changeset
   165
    condEngine:= SDL_CreateCond;
669bfa55cd70 Some work on new IPC, built with the use of mutexes and condition variables
unc0rr
parents: 10406
diff changeset
   166
end;
669bfa55cd70 Some work on new IPC, built with the use of mutexes and condition variables
unc0rr
parents: 10406
diff changeset
   167
669bfa55cd70 Some work on new IPC, built with the use of mutexes and condition variables
unc0rr
parents: 10406
diff changeset
   168
procedure freeIPC;
669bfa55cd70 Some work on new IPC, built with the use of mutexes and condition variables
unc0rr
parents: 10406
diff changeset
   169
begin
10416
1c301054694d - Remove --port command
unc0rr
parents: 10412
diff changeset
   170
    SDL_KillThread(callbackListenerThread);
10410
669bfa55cd70 Some work on new IPC, built with the use of mutexes and condition variables
unc0rr
parents: 10406
diff changeset
   171
    SDL_DestroyMutex(mutFrontend);
669bfa55cd70 Some work on new IPC, built with the use of mutexes and condition variables
unc0rr
parents: 10406
diff changeset
   172
    SDL_DestroyMutex(mutEngine);
669bfa55cd70 Some work on new IPC, built with the use of mutexes and condition variables
unc0rr
parents: 10406
diff changeset
   173
    SDL_DestroyCond(condFrontend);
669bfa55cd70 Some work on new IPC, built with the use of mutexes and condition variables
unc0rr
parents: 10406
diff changeset
   174
    SDL_DestroyCond(condEngine);
669bfa55cd70 Some work on new IPC, built with the use of mutexes and condition variables
unc0rr
parents: 10406
diff changeset
   175
end;
669bfa55cd70 Some work on new IPC, built with the use of mutexes and condition variables
unc0rr
parents: 10406
diff changeset
   176
10406
b5fd52ac760f Basic layout of frontlib, some more sdl bindings
unc0rr
parents:
diff changeset
   177
end.