hedgewars/uFLNet.pas
author unc0rr
Sat, 25 Apr 2015 23:46:09 +0300
branchqmlfrontend
changeset 10900 6a805e822074
parent 10898 f373838129c2
child 10929 8ebf01f75d9f
permissions -rw-r--r--
Some hedgewars coding a week keeps doctor away
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
10896
5a74923120d5 Start network support: only setting up a connection for now
unc0rr
parents:
diff changeset
     1
unit uFLNet;
5a74923120d5 Start network support: only setting up a connection for now
unc0rr
parents:
diff changeset
     2
interface
5a74923120d5 Start network support: only setting up a connection for now
unc0rr
parents:
diff changeset
     3
5a74923120d5 Start network support: only setting up a connection for now
unc0rr
parents:
diff changeset
     4
procedure connectOfficialServer;
5a74923120d5 Start network support: only setting up a connection for now
unc0rr
parents:
diff changeset
     5
5a74923120d5 Start network support: only setting up a connection for now
unc0rr
parents:
diff changeset
     6
procedure initModule;
5a74923120d5 Start network support: only setting up a connection for now
unc0rr
parents:
diff changeset
     7
procedure freeModule;
5a74923120d5 Start network support: only setting up a connection for now
unc0rr
parents:
diff changeset
     8
5a74923120d5 Start network support: only setting up a connection for now
unc0rr
parents:
diff changeset
     9
implementation
5a74923120d5 Start network support: only setting up a connection for now
unc0rr
parents:
diff changeset
    10
uses SDLh;
5a74923120d5 Start network support: only setting up a connection for now
unc0rr
parents:
diff changeset
    11
5a74923120d5 Start network support: only setting up a connection for now
unc0rr
parents:
diff changeset
    12
var sock: PTCPSocket;
5a74923120d5 Start network support: only setting up a connection for now
unc0rr
parents:
diff changeset
    13
    fds: PSDLNet_SocketSet;
10900
6a805e822074 Some hedgewars coding a week keeps doctor away
unc0rr
parents: 10898
diff changeset
    14
    netReaderThread: PSDL_Thread;
6a805e822074 Some hedgewars coding a week keeps doctor away
unc0rr
parents: 10898
diff changeset
    15
    sockbuf: shortstring;
6a805e822074 Some hedgewars coding a week keeps doctor away
unc0rr
parents: 10898
diff changeset
    16
    sockbufpos: byte;
6a805e822074 Some hedgewars coding a week keeps doctor away
unc0rr
parents: 10898
diff changeset
    17
6a805e822074 Some hedgewars coding a week keeps doctor away
unc0rr
parents: 10898
diff changeset
    18
function getNextChar: char;
6a805e822074 Some hedgewars coding a week keeps doctor away
unc0rr
parents: 10898
diff changeset
    19
var r: byte;
6a805e822074 Some hedgewars coding a week keeps doctor away
unc0rr
parents: 10898
diff changeset
    20
begin
6a805e822074 Some hedgewars coding a week keeps doctor away
unc0rr
parents: 10898
diff changeset
    21
    if sockbufpos < byte(sockbuf[0]) then
6a805e822074 Some hedgewars coding a week keeps doctor away
unc0rr
parents: 10898
diff changeset
    22
    begin
6a805e822074 Some hedgewars coding a week keeps doctor away
unc0rr
parents: 10898
diff changeset
    23
        inc(sockbufpos);
6a805e822074 Some hedgewars coding a week keeps doctor away
unc0rr
parents: 10898
diff changeset
    24
        getNextChar:= sockbuf[sockbufpos];
6a805e822074 Some hedgewars coding a week keeps doctor away
unc0rr
parents: 10898
diff changeset
    25
    end else
6a805e822074 Some hedgewars coding a week keeps doctor away
unc0rr
parents: 10898
diff changeset
    26
    begin
6a805e822074 Some hedgewars coding a week keeps doctor away
unc0rr
parents: 10898
diff changeset
    27
        r:= SDLNet_TCP_Recv(sock, @sockbuf[1], 255);
6a805e822074 Some hedgewars coding a week keeps doctor away
unc0rr
parents: 10898
diff changeset
    28
        if r > 0 then
6a805e822074 Some hedgewars coding a week keeps doctor away
unc0rr
parents: 10898
diff changeset
    29
        begin
6a805e822074 Some hedgewars coding a week keeps doctor away
unc0rr
parents: 10898
diff changeset
    30
            sockbufpos:= 1;
6a805e822074 Some hedgewars coding a week keeps doctor away
unc0rr
parents: 10898
diff changeset
    31
            sockbuf[0]:= char(i);
6a805e822074 Some hedgewars coding a week keeps doctor away
unc0rr
parents: 10898
diff changeset
    32
            getNextChar:= sockbuf[1];
6a805e822074 Some hedgewars coding a week keeps doctor away
unc0rr
parents: 10898
diff changeset
    33
        end else
6a805e822074 Some hedgewars coding a week keeps doctor away
unc0rr
parents: 10898
diff changeset
    34
        begin
6a805e822074 Some hedgewars coding a week keeps doctor away
unc0rr
parents: 10898
diff changeset
    35
            sockbufpos:= 0;
6a805e822074 Some hedgewars coding a week keeps doctor away
unc0rr
parents: 10898
diff changeset
    36
            sockbuf[0]:= #0;
6a805e822074 Some hedgewars coding a week keeps doctor away
unc0rr
parents: 10898
diff changeset
    37
            getNextChar:= #0
6a805e822074 Some hedgewars coding a week keeps doctor away
unc0rr
parents: 10898
diff changeset
    38
        end;
6a805e822074 Some hedgewars coding a week keeps doctor away
unc0rr
parents: 10898
diff changeset
    39
end;
10898
f373838129c2 Some futher work on flib net client part
unc0rr
parents: 10896
diff changeset
    40
f373838129c2 Some futher work on flib net client part
unc0rr
parents: 10896
diff changeset
    41
function netReader(data: pointer): LongInt; cdecl; export;
f373838129c2 Some futher work on flib net client part
unc0rr
parents: 10896
diff changeset
    42
begin
f373838129c2 Some futher work on flib net client part
unc0rr
parents: 10896
diff changeset
    43
end;
10896
5a74923120d5 Start network support: only setting up a connection for now
unc0rr
parents:
diff changeset
    44
5a74923120d5 Start network support: only setting up a connection for now
unc0rr
parents:
diff changeset
    45
procedure connectOfficialServer;
5a74923120d5 Start network support: only setting up a connection for now
unc0rr
parents:
diff changeset
    46
var ipaddr: TIPAddress;
5a74923120d5 Start network support: only setting up a connection for now
unc0rr
parents:
diff changeset
    47
begin
10898
f373838129c2 Some futher work on flib net client part
unc0rr
parents: 10896
diff changeset
    48
    if sock <> nil then
10896
5a74923120d5 Start network support: only setting up a connection for now
unc0rr
parents:
diff changeset
    49
        exit;
5a74923120d5 Start network support: only setting up a connection for now
unc0rr
parents:
diff changeset
    50
5a74923120d5 Start network support: only setting up a connection for now
unc0rr
parents:
diff changeset
    51
    if SDLNet_ResolveHost(ipaddr, PChar('netserver.hedgewars.org'), 46631) = 0 then
10898
f373838129c2 Some futher work on flib net client part
unc0rr
parents: 10896
diff changeset
    52
        sock:= SDLNet_TCP_Open(ipaddr);
f373838129c2 Some futher work on flib net client part
unc0rr
parents: 10896
diff changeset
    53
10900
6a805e822074 Some hedgewars coding a week keeps doctor away
unc0rr
parents: 10898
diff changeset
    54
    sockbufpos:= 0;
6a805e822074 Some hedgewars coding a week keeps doctor away
unc0rr
parents: 10898
diff changeset
    55
    sockbuf:= '';
6a805e822074 Some hedgewars coding a week keeps doctor away
unc0rr
parents: 10898
diff changeset
    56
    netReaderThread:= SDL_CreateThread(@netReader{$IFDEF SDL2}, 'netReader'{$ENDIF}, nil);
10896
5a74923120d5 Start network support: only setting up a connection for now
unc0rr
parents:
diff changeset
    57
end;
5a74923120d5 Start network support: only setting up a connection for now
unc0rr
parents:
diff changeset
    58
5a74923120d5 Start network support: only setting up a connection for now
unc0rr
parents:
diff changeset
    59
procedure initModule;
5a74923120d5 Start network support: only setting up a connection for now
unc0rr
parents:
diff changeset
    60
begin
5a74923120d5 Start network support: only setting up a connection for now
unc0rr
parents:
diff changeset
    61
    sock:= nil;
5a74923120d5 Start network support: only setting up a connection for now
unc0rr
parents:
diff changeset
    62
5a74923120d5 Start network support: only setting up a connection for now
unc0rr
parents:
diff changeset
    63
    SDLNet_Init;
5a74923120d5 Start network support: only setting up a connection for now
unc0rr
parents:
diff changeset
    64
    fds:= SDLNet_AllocSocketSet(1);
5a74923120d5 Start network support: only setting up a connection for now
unc0rr
parents:
diff changeset
    65
end;
5a74923120d5 Start network support: only setting up a connection for now
unc0rr
parents:
diff changeset
    66
5a74923120d5 Start network support: only setting up a connection for now
unc0rr
parents:
diff changeset
    67
procedure freeModule;
5a74923120d5 Start network support: only setting up a connection for now
unc0rr
parents:
diff changeset
    68
begin
5a74923120d5 Start network support: only setting up a connection for now
unc0rr
parents:
diff changeset
    69
end;
5a74923120d5 Start network support: only setting up a connection for now
unc0rr
parents:
diff changeset
    70
5a74923120d5 Start network support: only setting up a connection for now
unc0rr
parents:
diff changeset
    71
end.