# HG changeset patch # User unc0rr # Date 1431810870 -10800 # Node ID f1da4126a61c285bde94c71d6383eb1da3ad1e31 # Parent 384765cd0caf973387f2bc9123c2a858707defa8 Some more work on flib network diff -r 384765cd0caf -r f1da4126a61c hedgewars/uFLIPC.pas --- a/hedgewars/uFLIPC.pas Fri May 15 23:28:31 2015 +0300 +++ b/hedgewars/uFLIPC.pas Sun May 17 00:14:30 2015 +0300 @@ -126,10 +126,10 @@ ipcCheckFromFrontend:= ipcCheck(msgEngine, mutEngine) end; -function listener(p: pointer): Longint; cdecl; export; +function engineListener(p: pointer): Longint; cdecl; export; var msg: TIPCMessage; begin - listener:= 0; + engineListener:= 0; repeat msg:= ipcReadFromEngine(); if msg.buf = nil then @@ -146,7 +146,7 @@ begin callbackPointer:= p; callbackFunction:= f; - callbackListenerThread:= SDL_CreateThread(@listener{$IFDEF SDL2}, 'ipcListener'{$ENDIF}, nil); + callbackListenerThread:= SDL_CreateThread(@engineListener{$IFDEF SDL2}, 'engineListener'{$ENDIF}, nil); end; procedure initIPC; diff -r 384765cd0caf -r f1da4126a61c hedgewars/uFLNet.pas --- a/hedgewars/uFLNet.pas Fri May 15 23:28:31 2015 +0300 +++ b/hedgewars/uFLNet.pas Sun May 17 00:14:30 2015 +0300 @@ -15,7 +15,7 @@ function getCurrChar: char; forward; procedure sendNet(s: shortstring); forward; -type TCmdType = (cmd_ASKPASSWORD, cmd_BANLIST, cmd_BYE, cmd_CHAT, cmd_CLIENT_FLAGS, cmd_CONNECTED, cmd_EM, cmd_HH_NUM, cmd_INFO, cmd_JOINED, cmd_JOINING, cmd_KICKED, cmd_LEFT, cmd_LOBBY_JOINED, cmd_LOBBY_LEFT, cmd_NICK, cmd_NOTICE, cmd_PING, cmd_PROTO, cmd_ROOMS, cmd_ROUND_FINISHED, cmd_RUN_GAME, cmd_SERVER_AUTH, cmd_SERVER_MESSAGE, cmd_SERVER_VARS, cmd_TEAM_ACCEPTED, cmd_TEAM_COLOR, cmd_WARNING, cmd___UNKNOWN__); +type TCmdType = (cmd___UNKNOWN__, cmd_WARNING, cmd_TEAM_COLOR, cmd_TEAM_ACCEPTED, cmd_SERVER_VARS, cmd_SERVER_MESSAGE, cmd_SERVER_AUTH, cmd_RUN_GAME, cmd_ROUND_FINISHED, cmd_ROOMS, cmd_PROTO, cmd_PING, cmd_NOTICE, cmd_NICK, cmd_LOBBY_LEFT, cmd_LOBBY_JOINED, cmd_LEFT, cmd_KICKED, cmd_JOINING, cmd_JOINED, cmd_INFO, cmd_HH_NUM, cmd_EM, cmd_CONNECTED, cmd_CLIENT_FLAGS, cmd_CHAT, cmd_BYE, cmd_BANLIST, cmd_ASKPASSWORD); type TNetState = (netDisconnected, netConnecting, netLoggedIn); @@ -37,150 +37,119 @@ procedure handler_ASKPASSWORD; begin - state.cmd:= cmd_ASKPASSWORD; end; procedure handler_BANLIST; begin - state.cmd:= cmd_BANLIST; end; procedure handler_BYE; begin - state.cmd:= cmd_BYE; end; procedure handler_CHAT; begin - state.cmd:= cmd_CHAT; end; procedure handler_CLIENT_FLAGS; begin - state.cmd:= cmd_CLIENT_FLAGS; end; procedure handler_CONNECTED; begin - state.cmd:= cmd_CONNECTED; end; procedure handler_EM; begin - state.cmd:= cmd_EM; end; procedure handler_HH_NUM; begin - state.cmd:= cmd_HH_NUM; end; procedure handler_INFO; begin - state.cmd:= cmd_INFO; end; procedure handler_JOINED; begin - state.cmd:= cmd_JOINED; end; procedure handler_JOINING; begin - state.cmd:= cmd_JOINING; end; procedure handler_KICKED; begin - state.cmd:= cmd_KICKED; end; procedure handler_LEFT; begin - state.cmd:= cmd_LEFT; end; procedure handler_LOBBY_JOINED; begin - state.cmd:= cmd_LOBBY_JOINED; end; procedure handler_LOBBY_LEFT; begin - state.cmd:= cmd_LOBBY_LEFT; end; procedure handler_NICK; begin - state.cmd:= cmd_NICK; end; procedure handler_NOTICE; begin - state.cmd:= cmd_NOTICE; end; procedure handler_PING; begin - state.cmd:= cmd_PING; - - sendNet('PONG'); + sendNet('PONG') end; procedure handler_PROTO; begin - state.cmd:= cmd_PROTO; end; procedure handler_ROOMS; begin - state.cmd:= cmd_ROOMS; end; procedure handler_ROUND_FINISHED; begin - state.cmd:= cmd_ROUND_FINISHED; end; procedure handler_RUN_GAME; begin - state.cmd:= cmd_RUN_GAME; end; procedure handler_SERVER_AUTH; begin - state.cmd:= cmd_SERVER_AUTH; end; procedure handler_SERVER_MESSAGE; begin - state.cmd:= cmd_SERVER_MESSAGE; end; procedure handler_SERVER_VARS; begin - state.cmd:= cmd_SERVER_VARS; end; procedure handler_TEAM_ACCEPTED; begin - state.cmd:= cmd_TEAM_ACCEPTED; end; procedure handler_TEAM_COLOR; begin - state.cmd:= cmd_TEAM_COLOR; end; procedure handler_WARNING; begin - state.cmd:= cmd_WARNING; end; procedure handler___UNKNOWN__; begin - state.cmd:= cmd___UNKNOWN__; - writeln('[NET] Unknown cmd'); end; @@ -205,7 +174,6 @@ end; var sock: PTCPSocket; - fds: PSDLNet_SocketSet; netReaderThread: PSDL_Thread; function getCurrChar: char; @@ -236,36 +204,53 @@ getNextChar:= state.buf[state.bufpos]; end; +function netWriter(sock: PTCPSocket): LongInt; cdecl; export; +begin + netWriter:= 0; +end; + function netReader(data: pointer): LongInt; cdecl; export; var c: char; + ipaddr: TIPAddress; begin -repeat - c:= getNextChar; - writeln('>>>>> ', c, ' [', letters[state.l], '] ', commands[state.l]); - if c = #0 then - state.netState:= netDisconnected - else - begin - while (letters[state.l] <> c) and (commands[state.l] > 0) do - inc(state.l, commands[state.l]); + netReader:= 0; + + if SDLNet_ResolveHost(ipaddr, PChar('netserver.hedgewars.org'), 46631) = 0 then + sock:= SDLNet_TCP_Open(ipaddr); + + SDL_CreateThread(@netWriter{$IFDEF SDL2}, 'netWriter'{$ENDIF}, sock); - if c = letters[state.l] then - if commands[state.l] < 0 then + repeat + c:= getNextChar; + //writeln('>>>>> ', c, ' [', letters[state.l], '] ', commands[state.l]); + if c = #0 then + state.netState:= netDisconnected + else + begin + while (letters[state.l] <> c) and (commands[state.l] > 0) do + inc(state.l, commands[state.l]); + + if c = letters[state.l] then + if commands[state.l] < 0 then begin + state.cmd:= TCmdType(-10 - commands[state.l]); + writeln('[NET] ', state.cmd); handlers[-10 - commands[state.l]](); handleTail() end + else + inc(state.l) else - inc(state.l) - else - begin - handler___UNKNOWN__(); - handleTail() + begin + handler___UNKNOWN__(); + handleTail() + end end - end -until state.netState = netDisconnected; + until state.netState = netDisconnected; -writeln('[NET] netReader: disconnected'); + sock:= nil; + + writeln('[NET] netReader: disconnected'); end; procedure sendNet(s: shortstring); @@ -276,14 +261,10 @@ end; procedure connectOfficialServer; -var ipaddr: TIPAddress; begin if sock <> nil then exit; - if SDLNet_ResolveHost(ipaddr, PChar('netserver.hedgewars.org'), 46631) = 0 then - sock:= SDLNet_TCP_Open(ipaddr); - state.bufpos:= 0; state.buf:= ''; @@ -298,7 +279,6 @@ sock:= nil; SDLNet_Init; - fds:= SDLNet_AllocSocketSet(1); end; procedure freeModule; diff -r 384765cd0caf -r f1da4126a61c tools/protocolParser.hs --- a/tools/protocolParser.hs Fri May 15 23:28:31 2015 +0300 +++ b/tools/protocolParser.hs Sun May 17 00:14:30 2015 +0300 @@ -109,11 +109,8 @@ bodies = vcat $ punctuate (char '\n') $ map handlerBody fixedNames handlerBody n = text "procedure handler_" <> text n <> semi $+$ text "begin" - $+$ nest 4 ( - text "state.cmd:= cmd_" <> text n <> semi - ) $+$ text "end" <> semi - cmds = text "type TCmdType = " <> parens (hsep $ punctuate comma $ map ((<>) (text "cmd_") . text) fixedNames) <> semi + cmds = text "type TCmdType = " <> parens (hsep $ punctuate comma $ map ((<>) (text "cmd_") . text) $ reverse fixedNames) <> semi pas = renderArrays $ buildTables $ buildParseTree commands where