hedgewars/uNet.pas
author unc0rr
Tue, 20 Jun 2006 21:18:49 +0000
changeset 66 9643d75baf1e
parent 4 bcbd7adb4e4b
permissions -rw-r--r--
Many AI improvements, bots do think in separate thread
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
4
bcbd7adb4e4b - set svn:eol-style to native
unc0rr
parents: 1
diff changeset
     1
(*
bcbd7adb4e4b - set svn:eol-style to native
unc0rr
parents: 1
diff changeset
     2
 * Hedgewars, a worms-like game
bcbd7adb4e4b - set svn:eol-style to native
unc0rr
parents: 1
diff changeset
     3
 * Copyright (c) 2004, 2005 Andrey Korotaev <unC0Rr@gmail.com>
bcbd7adb4e4b - set svn:eol-style to native
unc0rr
parents: 1
diff changeset
     4
 *
bcbd7adb4e4b - set svn:eol-style to native
unc0rr
parents: 1
diff changeset
     5
 * Distributed under the terms of the BSD-modified licence:
bcbd7adb4e4b - set svn:eol-style to native
unc0rr
parents: 1
diff changeset
     6
 *
bcbd7adb4e4b - set svn:eol-style to native
unc0rr
parents: 1
diff changeset
     7
 * Permission is hereby granted, free of charge, to any person obtaining a copy
bcbd7adb4e4b - set svn:eol-style to native
unc0rr
parents: 1
diff changeset
     8
 * of this software and associated documentation files (the "Software"), to deal
bcbd7adb4e4b - set svn:eol-style to native
unc0rr
parents: 1
diff changeset
     9
 * with the Software without restriction, including without limitation the
bcbd7adb4e4b - set svn:eol-style to native
unc0rr
parents: 1
diff changeset
    10
 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
bcbd7adb4e4b - set svn:eol-style to native
unc0rr
parents: 1
diff changeset
    11
 * sell copies of the Software, and to permit persons to whom the Software is
bcbd7adb4e4b - set svn:eol-style to native
unc0rr
parents: 1
diff changeset
    12
 * furnished to do so, subject to the following conditions:
bcbd7adb4e4b - set svn:eol-style to native
unc0rr
parents: 1
diff changeset
    13
 *
bcbd7adb4e4b - set svn:eol-style to native
unc0rr
parents: 1
diff changeset
    14
 * 1. Redistributions of source code must retain the above copyright notice,
bcbd7adb4e4b - set svn:eol-style to native
unc0rr
parents: 1
diff changeset
    15
 *    this list of conditions and the following disclaimer.
bcbd7adb4e4b - set svn:eol-style to native
unc0rr
parents: 1
diff changeset
    16
 * 2. Redistributions in binary form must reproduce the above copyright notice,
bcbd7adb4e4b - set svn:eol-style to native
unc0rr
parents: 1
diff changeset
    17
 *    this list of conditions and the following disclaimer in the documentation
bcbd7adb4e4b - set svn:eol-style to native
unc0rr
parents: 1
diff changeset
    18
 *    and/or other materials provided with the distribution.
bcbd7adb4e4b - set svn:eol-style to native
unc0rr
parents: 1
diff changeset
    19
 * 3. The name of the author may not be used to endorse or promote products
bcbd7adb4e4b - set svn:eol-style to native
unc0rr
parents: 1
diff changeset
    20
 *    derived from this software without specific prior written permission.
bcbd7adb4e4b - set svn:eol-style to native
unc0rr
parents: 1
diff changeset
    21
 *
bcbd7adb4e4b - set svn:eol-style to native
unc0rr
parents: 1
diff changeset
    22
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
bcbd7adb4e4b - set svn:eol-style to native
unc0rr
parents: 1
diff changeset
    23
 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
bcbd7adb4e4b - set svn:eol-style to native
unc0rr
parents: 1
diff changeset
    24
 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
bcbd7adb4e4b - set svn:eol-style to native
unc0rr
parents: 1
diff changeset
    25
 * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
bcbd7adb4e4b - set svn:eol-style to native
unc0rr
parents: 1
diff changeset
    26
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
bcbd7adb4e4b - set svn:eol-style to native
unc0rr
parents: 1
diff changeset
    27
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
bcbd7adb4e4b - set svn:eol-style to native
unc0rr
parents: 1
diff changeset
    28
 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
bcbd7adb4e4b - set svn:eol-style to native
unc0rr
parents: 1
diff changeset
    29
 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
bcbd7adb4e4b - set svn:eol-style to native
unc0rr
parents: 1
diff changeset
    30
 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
bcbd7adb4e4b - set svn:eol-style to native
unc0rr
parents: 1
diff changeset
    31
 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
bcbd7adb4e4b - set svn:eol-style to native
unc0rr
parents: 1
diff changeset
    32
 *)
bcbd7adb4e4b - set svn:eol-style to native
unc0rr
parents: 1
diff changeset
    33
bcbd7adb4e4b - set svn:eol-style to native
unc0rr
parents: 1
diff changeset
    34
unit uNet;
bcbd7adb4e4b - set svn:eol-style to native
unc0rr
parents: 1
diff changeset
    35
interface
bcbd7adb4e4b - set svn:eol-style to native
unc0rr
parents: 1
diff changeset
    36
uses WinSock, Messages;
bcbd7adb4e4b - set svn:eol-style to native
unc0rr
parents: 1
diff changeset
    37
const
bcbd7adb4e4b - set svn:eol-style to native
unc0rr
parents: 1
diff changeset
    38
      IN_NET_PORT  = 46632;
bcbd7adb4e4b - set svn:eol-style to native
unc0rr
parents: 1
diff changeset
    39
      WM_ASYNC_NETEVENT = WM_USER + 7;
bcbd7adb4e4b - set svn:eol-style to native
unc0rr
parents: 1
diff changeset
    40
bcbd7adb4e4b - set svn:eol-style to native
unc0rr
parents: 1
diff changeset
    41
type TCommandHandler = procedure (s: shortstring);
bcbd7adb4e4b - set svn:eol-style to native
unc0rr
parents: 1
diff changeset
    42
bcbd7adb4e4b - set svn:eol-style to native
unc0rr
parents: 1
diff changeset
    43
procedure SplitStream2Commands(var ss: string; Handler: TCommandHandler);
bcbd7adb4e4b - set svn:eol-style to native
unc0rr
parents: 1
diff changeset
    44
procedure SendSock(Socket: TSocket; s: shortstring);
bcbd7adb4e4b - set svn:eol-style to native
unc0rr
parents: 1
diff changeset
    45
procedure InitServer;
bcbd7adb4e4b - set svn:eol-style to native
unc0rr
parents: 1
diff changeset
    46
procedure NetSockEvent(sock, lParam: Longword);
bcbd7adb4e4b - set svn:eol-style to native
unc0rr
parents: 1
diff changeset
    47
bcbd7adb4e4b - set svn:eol-style to native
unc0rr
parents: 1
diff changeset
    48
var hNetListenSockTCP: TSocket = INVALID_SOCKET;
bcbd7adb4e4b - set svn:eol-style to native
unc0rr
parents: 1
diff changeset
    49
bcbd7adb4e4b - set svn:eol-style to native
unc0rr
parents: 1
diff changeset
    50
implementation
bcbd7adb4e4b - set svn:eol-style to native
unc0rr
parents: 1
diff changeset
    51
uses uServerMisc, uPlayers;
bcbd7adb4e4b - set svn:eol-style to native
unc0rr
parents: 1
diff changeset
    52
bcbd7adb4e4b - set svn:eol-style to native
unc0rr
parents: 1
diff changeset
    53
procedure SplitStream2Commands(var ss: string; Handler: TCommandHandler);
bcbd7adb4e4b - set svn:eol-style to native
unc0rr
parents: 1
diff changeset
    54
var s: shortstring;
bcbd7adb4e4b - set svn:eol-style to native
unc0rr
parents: 1
diff changeset
    55
begin
bcbd7adb4e4b - set svn:eol-style to native
unc0rr
parents: 1
diff changeset
    56
while (Length(ss) > 1)and(Length(ss) > byte(ss[1])) do
bcbd7adb4e4b - set svn:eol-style to native
unc0rr
parents: 1
diff changeset
    57
      begin
bcbd7adb4e4b - set svn:eol-style to native
unc0rr
parents: 1
diff changeset
    58
      s:= copy(ss, 2, byte(ss[1]));
bcbd7adb4e4b - set svn:eol-style to native
unc0rr
parents: 1
diff changeset
    59
      Delete(ss, 1, Succ(byte(ss[1])));
bcbd7adb4e4b - set svn:eol-style to native
unc0rr
parents: 1
diff changeset
    60
      Handler(s)
bcbd7adb4e4b - set svn:eol-style to native
unc0rr
parents: 1
diff changeset
    61
      end;
bcbd7adb4e4b - set svn:eol-style to native
unc0rr
parents: 1
diff changeset
    62
end;
bcbd7adb4e4b - set svn:eol-style to native
unc0rr
parents: 1
diff changeset
    63
bcbd7adb4e4b - set svn:eol-style to native
unc0rr
parents: 1
diff changeset
    64
procedure SendSock(Socket: TSocket; s: shortstring);
bcbd7adb4e4b - set svn:eol-style to native
unc0rr
parents: 1
diff changeset
    65
begin
bcbd7adb4e4b - set svn:eol-style to native
unc0rr
parents: 1
diff changeset
    66
//writeln(socket, '> ', s);
bcbd7adb4e4b - set svn:eol-style to native
unc0rr
parents: 1
diff changeset
    67
send(Socket, s[0], Succ(byte(s[0])), 0)
bcbd7adb4e4b - set svn:eol-style to native
unc0rr
parents: 1
diff changeset
    68
end;
bcbd7adb4e4b - set svn:eol-style to native
unc0rr
parents: 1
diff changeset
    69
bcbd7adb4e4b - set svn:eol-style to native
unc0rr
parents: 1
diff changeset
    70
procedure InitServer;
bcbd7adb4e4b - set svn:eol-style to native
unc0rr
parents: 1
diff changeset
    71
var myaddrTCP: TSockAddrIn;
bcbd7adb4e4b - set svn:eol-style to native
unc0rr
parents: 1
diff changeset
    72
    t: integer;
bcbd7adb4e4b - set svn:eol-style to native
unc0rr
parents: 1
diff changeset
    73
    stWSADataTCPIP : WSADATA;
bcbd7adb4e4b - set svn:eol-style to native
unc0rr
parents: 1
diff changeset
    74
begin
bcbd7adb4e4b - set svn:eol-style to native
unc0rr
parents: 1
diff changeset
    75
TryDo(WSAStartup($0101, stWSADataTCPIP) = 0, 'Error on WSAStartup');
bcbd7adb4e4b - set svn:eol-style to native
unc0rr
parents: 1
diff changeset
    76
hNetListenSockTCP:= socket(AF_INET, SOCK_STREAM, 0);
bcbd7adb4e4b - set svn:eol-style to native
unc0rr
parents: 1
diff changeset
    77
myaddrTCP.sin_family      := AF_INET;
bcbd7adb4e4b - set svn:eol-style to native
unc0rr
parents: 1
diff changeset
    78
myaddrTCP.sin_addr.s_addr := $0;
bcbd7adb4e4b - set svn:eol-style to native
unc0rr
parents: 1
diff changeset
    79
myaddrTCP.sin_port        := htons(IN_NET_PORT);
bcbd7adb4e4b - set svn:eol-style to native
unc0rr
parents: 1
diff changeset
    80
t:= sizeof(TSockAddrIn);
bcbd7adb4e4b - set svn:eol-style to native
unc0rr
parents: 1
diff changeset
    81
TryDo(   bind(hNetListenSockTCP, myaddrTCP, t) = 0, 'Error on bind'  );
bcbd7adb4e4b - set svn:eol-style to native
unc0rr
parents: 1
diff changeset
    82
TryDo( listen(hNetListenSockTCP, 1)            = 0, 'Error on listen');
bcbd7adb4e4b - set svn:eol-style to native
unc0rr
parents: 1
diff changeset
    83
WSAAsyncSelect(hNetListenSockTCP, hwndMain, WM_ASYNC_NETEVENT, FD_ACCEPT or FD_READ or FD_CLOSE)
bcbd7adb4e4b - set svn:eol-style to native
unc0rr
parents: 1
diff changeset
    84
end;
bcbd7adb4e4b - set svn:eol-style to native
unc0rr
parents: 1
diff changeset
    85
bcbd7adb4e4b - set svn:eol-style to native
unc0rr
parents: 1
diff changeset
    86
procedure ParseNetCommand(Player: PPlayer; s: shortstring);
bcbd7adb4e4b - set svn:eol-style to native
unc0rr
parents: 1
diff changeset
    87
begin
bcbd7adb4e4b - set svn:eol-style to native
unc0rr
parents: 1
diff changeset
    88
case s[1] of
bcbd7adb4e4b - set svn:eol-style to native
unc0rr
parents: 1
diff changeset
    89
     '?': SendSock(player.socket, '!');
bcbd7adb4e4b - set svn:eol-style to native
unc0rr
parents: 1
diff changeset
    90
     'n': begin
bcbd7adb4e4b - set svn:eol-style to native
unc0rr
parents: 1
diff changeset
    91
          player.Name:= copy(s, 2, length(s) - 1);
bcbd7adb4e4b - set svn:eol-style to native
unc0rr
parents: 1
diff changeset
    92
          Writeln(player.socket, ' now is ', player.Name)
bcbd7adb4e4b - set svn:eol-style to native
unc0rr
parents: 1
diff changeset
    93
          end;
bcbd7adb4e4b - set svn:eol-style to native
unc0rr
parents: 1
diff changeset
    94
     'C': SendConfig(player);
bcbd7adb4e4b - set svn:eol-style to native
unc0rr
parents: 1
diff changeset
    95
     'G': SendAll('G');
bcbd7adb4e4b - set svn:eol-style to native
unc0rr
parents: 1
diff changeset
    96
     'T': begin
bcbd7adb4e4b - set svn:eol-style to native
unc0rr
parents: 1
diff changeset
    97
          s[0]:= #5;
bcbd7adb4e4b - set svn:eol-style to native
unc0rr
parents: 1
diff changeset
    98
          s[1]:= 'T';
bcbd7adb4e4b - set svn:eol-style to native
unc0rr
parents: 1
diff changeset
    99
          PLongWord(@s[2])^:= GetTeamCount;
bcbd7adb4e4b - set svn:eol-style to native
unc0rr
parents: 1
diff changeset
   100
          SendSock(player.socket, s)
bcbd7adb4e4b - set svn:eol-style to native
unc0rr
parents: 1
diff changeset
   101
          end;
bcbd7adb4e4b - set svn:eol-style to native
unc0rr
parents: 1
diff changeset
   102
     'K': SelectFirstCFGTeam;
bcbd7adb4e4b - set svn:eol-style to native
unc0rr
parents: 1
diff changeset
   103
     'k': SelectNextCFGTeam;
bcbd7adb4e4b - set svn:eol-style to native
unc0rr
parents: 1
diff changeset
   104
     'h': ConfCurrTeam(s);
bcbd7adb4e4b - set svn:eol-style to native
unc0rr
parents: 1
diff changeset
   105
     else SendAllButOne(Player, s) end
bcbd7adb4e4b - set svn:eol-style to native
unc0rr
parents: 1
diff changeset
   106
end;
bcbd7adb4e4b - set svn:eol-style to native
unc0rr
parents: 1
diff changeset
   107
bcbd7adb4e4b - set svn:eol-style to native
unc0rr
parents: 1
diff changeset
   108
procedure NetSockEvent(sock, lParam: Longword);
bcbd7adb4e4b - set svn:eol-style to native
unc0rr
parents: 1
diff changeset
   109
var i: integer;
bcbd7adb4e4b - set svn:eol-style to native
unc0rr
parents: 1
diff changeset
   110
    buf: array[0..255] of byte;
bcbd7adb4e4b - set svn:eol-style to native
unc0rr
parents: 1
diff changeset
   111
    s: shortstring absolute buf;
bcbd7adb4e4b - set svn:eol-style to native
unc0rr
parents: 1
diff changeset
   112
    WSAEvent: word;
bcbd7adb4e4b - set svn:eol-style to native
unc0rr
parents: 1
diff changeset
   113
    player: PPlayer;
bcbd7adb4e4b - set svn:eol-style to native
unc0rr
parents: 1
diff changeset
   114
    sa: TSockAddr;
bcbd7adb4e4b - set svn:eol-style to native
unc0rr
parents: 1
diff changeset
   115
begin
bcbd7adb4e4b - set svn:eol-style to native
unc0rr
parents: 1
diff changeset
   116
WSAEvent:= WSAGETSELECTEVENT(lParam);
bcbd7adb4e4b - set svn:eol-style to native
unc0rr
parents: 1
diff changeset
   117
case WSAEvent of
bcbd7adb4e4b - set svn:eol-style to native
unc0rr
parents: 1
diff changeset
   118
      FD_ACCEPT: begin
bcbd7adb4e4b - set svn:eol-style to native
unc0rr
parents: 1
diff changeset
   119
                 i:= sizeof(sa);
bcbd7adb4e4b - set svn:eol-style to native
unc0rr
parents: 1
diff changeset
   120
                 sock:= accept(hNetListenSockTCP, @sa, @i);
bcbd7adb4e4b - set svn:eol-style to native
unc0rr
parents: 1
diff changeset
   121
                 Writeln('Connected player ', sock, ' from ', inet_ntoa(sa.sin_addr));
bcbd7adb4e4b - set svn:eol-style to native
unc0rr
parents: 1
diff changeset
   122
                 AddPlayer(sock);
bcbd7adb4e4b - set svn:eol-style to native
unc0rr
parents: 1
diff changeset
   123
                 SendSock(sock, 'i')
bcbd7adb4e4b - set svn:eol-style to native
unc0rr
parents: 1
diff changeset
   124
                 end;
bcbd7adb4e4b - set svn:eol-style to native
unc0rr
parents: 1
diff changeset
   125
       FD_CLOSE: begin
bcbd7adb4e4b - set svn:eol-style to native
unc0rr
parents: 1
diff changeset
   126
                 player:= FindPlayerbySock(sock);
bcbd7adb4e4b - set svn:eol-style to native
unc0rr
parents: 1
diff changeset
   127
                 TryDo(player <> nil, 'FD_CLOSE from unknown player??');
bcbd7adb4e4b - set svn:eol-style to native
unc0rr
parents: 1
diff changeset
   128
                 Write('Player quit: ');
bcbd7adb4e4b - set svn:eol-style to native
unc0rr
parents: 1
diff changeset
   129
                 if player.Name[0]=#0 then Writeln('socket ', player.socket)
bcbd7adb4e4b - set svn:eol-style to native
unc0rr
parents: 1
diff changeset
   130
                                      else Writeln(player.Name);
bcbd7adb4e4b - set svn:eol-style to native
unc0rr
parents: 1
diff changeset
   131
                 DeletePlayer(player);
bcbd7adb4e4b - set svn:eol-style to native
unc0rr
parents: 1
diff changeset
   132
                 closesocket(sock);
bcbd7adb4e4b - set svn:eol-style to native
unc0rr
parents: 1
diff changeset
   133
                 end;
bcbd7adb4e4b - set svn:eol-style to native
unc0rr
parents: 1
diff changeset
   134
        FD_READ: begin
bcbd7adb4e4b - set svn:eol-style to native
unc0rr
parents: 1
diff changeset
   135
                 player:= FindPlayerbySock(sock);
bcbd7adb4e4b - set svn:eol-style to native
unc0rr
parents: 1
diff changeset
   136
                 TryDo(player <> nil, 'FD_READ from unknown player??');
bcbd7adb4e4b - set svn:eol-style to native
unc0rr
parents: 1
diff changeset
   137
                 repeat
bcbd7adb4e4b - set svn:eol-style to native
unc0rr
parents: 1
diff changeset
   138
                 i:= recv(sock, buf[1], 255, 0);
bcbd7adb4e4b - set svn:eol-style to native
unc0rr
parents: 1
diff changeset
   139
                 if i > 0 then
bcbd7adb4e4b - set svn:eol-style to native
unc0rr
parents: 1
diff changeset
   140
                    begin
bcbd7adb4e4b - set svn:eol-style to native
unc0rr
parents: 1
diff changeset
   141
                    buf[0]:= i;
bcbd7adb4e4b - set svn:eol-style to native
unc0rr
parents: 1
diff changeset
   142
                    player.inbuf:= player.inbuf + s;
bcbd7adb4e4b - set svn:eol-style to native
unc0rr
parents: 1
diff changeset
   143
                    while (Length(player.inbuf) > 1)and(Length(player.inbuf) > byte(player.inbuf[1])) do
bcbd7adb4e4b - set svn:eol-style to native
unc0rr
parents: 1
diff changeset
   144
                          begin
bcbd7adb4e4b - set svn:eol-style to native
unc0rr
parents: 1
diff changeset
   145
                          ParseNetCommand(player, copy(player.inbuf, 2, byte(player.inbuf[1])));
bcbd7adb4e4b - set svn:eol-style to native
unc0rr
parents: 1
diff changeset
   146
                          Delete(player.inbuf, 1, Succ(byte(player.inbuf[1])))
bcbd7adb4e4b - set svn:eol-style to native
unc0rr
parents: 1
diff changeset
   147
                          end;
bcbd7adb4e4b - set svn:eol-style to native
unc0rr
parents: 1
diff changeset
   148
                    end;
bcbd7adb4e4b - set svn:eol-style to native
unc0rr
parents: 1
diff changeset
   149
                 until i < 1;
bcbd7adb4e4b - set svn:eol-style to native
unc0rr
parents: 1
diff changeset
   150
                 end
bcbd7adb4e4b - set svn:eol-style to native
unc0rr
parents: 1
diff changeset
   151
     end
bcbd7adb4e4b - set svn:eol-style to native
unc0rr
parents: 1
diff changeset
   152
end;
bcbd7adb4e4b - set svn:eol-style to native
unc0rr
parents: 1
diff changeset
   153
bcbd7adb4e4b - set svn:eol-style to native
unc0rr
parents: 1
diff changeset
   154
bcbd7adb4e4b - set svn:eol-style to native
unc0rr
parents: 1
diff changeset
   155
end.