1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165
|
{-
* Hedgewars, a free turn based strategy game
* Copyright (c) 2004-2015 Andrey Korotaev <unC0Rr@gmail.com>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; version 2 of the License
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
\-}
{-# LANGUAGE CPP, ScopedTypeVariables, OverloadedStrings #-}
module OfficialServer.DBInteraction
(
startDBConnection
) where
import Prelude hiding (catch);
import Control.Concurrent
import Control.Monad
import Data.List as L
import Data.ByteString.Char8 as B
#if defined(OFFICIAL_SERVER)
import System.Process
import System.IO as SIO
import qualified Control.Exception as Exception
import qualified Data.Map as Map
import Data.Maybe
import Data.Time
import System.Log.Logger
#endif
------------------------
import CoreTypes
#if defined(OFFICIAL_SERVER)
import Utils
#endif
localAddressList :: [B.ByteString]
localAddressList = ["127.0.0.1", "0:0:0:0:0:0:0:1", "0:0:0:0:0:ffff:7f00:1"]
fakeDbConnection :: forall b. ServerInfo -> IO b
fakeDbConnection si = forever $ do
q <- readChan $ dbQueries si
case q of
CheckAccount clId clUid _ clHost ->
writeChan (coreChan si) $ ClientAccountInfo clId clUid (if clHost `L.elem` localAddressList then Admin else Guest)
ClearCache -> return ()
SendStats {} -> return ()
dbConnectionLoop :: ServerInfo -> IO ()
#if defined(OFFICIAL_SERVER)
flushRequests :: ServerInfo -> IO ()
flushRequests si = do
e <- isEmptyChan $ dbQueries si
unless e $ do
q <- readChan $ dbQueries si
case q of
CheckAccount clId clUid _ clHost ->
writeChan (coreChan si) $ ClientAccountInfo clId clUid (if clHost `L.elem` localAddressList then Admin else Guest)
ClearCache -> return ()
SendStats {} -> return ()
GetReplayName {} -> return ()
StoreAchievements {} -> return ()
flushRequests si
pipeDbConnectionLoop :: Chan DBQuery -> Chan CoreMessage -> Handle -> Handle -> Map.Map ByteString (UTCTime, AccountInfo) -> Int -> IO (Map.Map ByteString (UTCTime, AccountInfo), Int)
pipeDbConnectionLoop queries cChan hIn hOut accountsCache req =
Exception.handle (\(e :: Exception.IOException) -> warningM "Database" (show e) >> return (accountsCache, req)) $
do
q <- readChan queries
(updatedCache, newReq) <- case q of
CheckAccount clId clUid clNick _ -> do
let cacheEntry = clNick `Map.lookup` accountsCache
currentTime <- getCurrentTime
if (isNothing cacheEntry) || (currentTime `diffUTCTime` (fst . fromJust) cacheEntry > 10 * 60) then
do
SIO.hPutStrLn hIn $ show q
hFlush hIn
(clId', clUid', accountInfo) <- SIO.hGetLine hOut >>= (maybeException . maybeRead)
writeChan cChan $ ClientAccountInfo clId' clUid' accountInfo
return $ (Map.insert clNick (currentTime, accountInfo) accountsCache, req + 1)
`Exception.onException`
(unGetChan queries q)
else
do
writeChan cChan $ ClientAccountInfo clId clUid (snd $ fromJust cacheEntry)
return (accountsCache, req)
GetReplayName {} -> do
SIO.hPutStrLn hIn $ show q
hFlush hIn
(clId', clUid', accountInfo) <- SIO.hGetLine hOut >>= (maybeException . maybeRead)
writeChan cChan $ ClientAccountInfo clId' clUid' accountInfo
return (accountsCache, req)
ClearCache -> return (Map.empty, req)
StoreAchievements {} -> (
(SIO.hPutStrLn hIn $ show q) >>
hFlush hIn >>
return (accountsCache, req))
`Exception.onException`
(unGetChan queries q)
SendStats {} -> (
(SIO.hPutStrLn hIn $ show q) >>
hFlush hIn >>
return (accountsCache, req))
`Exception.onException`
(unGetChan queries q)
pipeDbConnectionLoop queries cChan hIn hOut updatedCache newReq
where
maybeException (Just a) = return a
maybeException Nothing = ioError (userError "Can't read")
pipeDbConnection ::
Map.Map ByteString (UTCTime, AccountInfo)
-> ServerInfo
-> Int
-> IO ()
pipeDbConnection accountsCache si errNum = do
(updatedCache, newErrNum) <-
Exception.handle (\(e :: Exception.IOException) -> warningM "Database" (show e) >> return (accountsCache, errNum + 1)) $ do
(Just hIn, Just hOut, _, _) <- createProcess (proc "./OfficialServer/extdbinterface" [])
{std_in = CreatePipe,
std_out = CreatePipe}
hSetBuffering hIn LineBuffering
hSetBuffering hOut LineBuffering
B.hPutStrLn hIn $ dbHost si
B.hPutStrLn hIn $ dbName si
B.hPutStrLn hIn $ dbLogin si
B.hPutStrLn hIn $ dbPassword si
(c, r) <- pipeDbConnectionLoop (dbQueries si) (coreChan si) hIn hOut accountsCache 0
return (c, if r > 0 then 0 else errNum + 1)
when (newErrNum > 1) $ flushRequests si
threadDelay (3000000)
pipeDbConnection updatedCache si newErrNum
dbConnectionLoop si =
if (not . B.null $ dbHost si) then
pipeDbConnection Map.empty si 0
else
fakeDbConnection si
#else
dbConnectionLoop = fakeDbConnection
#endif
startDBConnection :: ServerInfo -> IO ()
startDBConnection serverInfo =
forkIO (dbConnectionLoop serverInfo) >> return ()
|