diff options
author | Joe Crayne <joe@jerkface.net> | 2019-12-01 02:22:59 -0500 |
---|---|---|
committer | Joe Crayne <joe@jerkface.net> | 2020-01-01 23:22:52 -0500 |
commit | 7c88315f6e01e203c537fa3193a83acd3a7afa2c (patch) | |
tree | 73d0164ee26f3d015c542d533e104695444633ac /dht/src | |
parent | 1a0819cd502f578d25faa7b2add75a20f5d5d0d7 (diff) |
Avoid calling getSocketName after socketToHandle.
Diffstat (limited to 'dht/src')
-rw-r--r-- | dht/src/Network/StreamServer.hs | 23 |
1 files changed, 11 insertions, 12 deletions
diff --git a/dht/src/Network/StreamServer.hs b/dht/src/Network/StreamServer.hs index 37f8812a..1da612ce 100644 --- a/dht/src/Network/StreamServer.hs +++ b/dht/src/Network/StreamServer.hs | |||
@@ -13,6 +13,8 @@ module Network.StreamServer | |||
13 | , quitListening | 13 | , quitListening |
14 | --, dummyServerHandle | 14 | --, dummyServerHandle |
15 | , listenSocket | 15 | , listenSocket |
16 | , Local(..) | ||
17 | , Remote(..) | ||
16 | ) where | 18 | ) where |
17 | 19 | ||
18 | import Data.Monoid | 20 | import Data.Monoid |
@@ -89,15 +91,18 @@ bshow e = show e | |||
89 | warnStderr :: String -> IO () | 91 | warnStderr :: String -> IO () |
90 | warnStderr str = dput XMisc str >> hFlush stderr | 92 | warnStderr str = dput XMisc str >> hFlush stderr |
91 | 93 | ||
94 | newtype Local a = Local a deriving (Eq,Ord,Show) | ||
95 | newtype Remote a = Remote a deriving (Eq,Ord,Show) | ||
96 | |||
92 | data ServerConfig = ServerConfig | 97 | data ServerConfig = ServerConfig |
93 | { serverWarn :: String -> IO () | 98 | { serverWarn :: String -> IO () |
94 | -- ^ Action to report warnings and errors. | 99 | -- ^ Action to report warnings and errors. |
95 | , serverSession :: RestrictedSocket -> Int -> Handle -> IO () | 100 | , serverSession :: ( RestrictedSocket, (Local SockAddr, Remote SockAddr)) -> Int -> Handle -> IO () |
96 | -- ^ Action to handle interaction with a client | 101 | -- ^ Action to handle interaction with a client |
97 | } | 102 | } |
98 | 103 | ||
99 | -- | Initialize a 'ServerConfig' using the provided session handler. | 104 | -- | Initialize a 'ServerConfig' using the provided session handler. |
100 | withSession :: (RestrictedSocket -> Int -> Handle -> IO ()) -> ServerConfig | 105 | withSession :: ((RestrictedSocket,(Local SockAddr,Remote SockAddr)) -> Int -> Handle -> IO ()) -> ServerConfig |
101 | withSession session = ServerConfig warnStderr session | 106 | withSession session = ServerConfig warnStderr session |
102 | 107 | ||
103 | -- | Launch a thread to listen at the given bind address and dispatch | 108 | -- | Launch a thread to listen at the given bind address and dispatch |
@@ -138,19 +143,13 @@ streamServer cfg addrs = do | |||
138 | -- socket must be closed by 'quitListening'. | 143 | -- socket must be closed by 'quitListening'. |
139 | acceptLoop :: ServerConfig -> Socket -> Int -> IO () | 144 | acceptLoop :: ServerConfig -> Socket -> Int -> IO () |
140 | acceptLoop cfg sock n = handle (acceptException cfg n sock) $ do | 145 | acceptLoop cfg sock n = handle (acceptException cfg n sock) $ do |
141 | con <- accept sock -- TODO: We need to remember the (snd con) peer address here!!! | 146 | (con,raddr) <- accept sock |
142 | -- See also: "-- Weird hack: addr is would-be peer name" in XMPPServer.hs | ||
143 | -- If we remember the peer address here, we won't need that weird | ||
144 | -- hack or to call sIsConnected. | ||
145 | -- Probably we should move the | ||
146 | -- newtype Local a = Local a deriving (Eq,Ord,Show) | ||
147 | -- newtype Remote a = Remote a deriving (Eq,Ord,Show) | ||
148 | -- defines so that they are accessible to this module. | ||
149 | let conkey = n + 1 | 147 | let conkey = n + 1 |
150 | h <- socketToHandle (fst con) ReadWriteMode | 148 | laddr <- Socket.getSocketName con |
149 | h <- socketToHandle con ReadWriteMode | ||
151 | forkIO $ do | 150 | forkIO $ do |
152 | myThreadId >>= flip labelThread "StreamServer.session" | 151 | myThreadId >>= flip labelThread "StreamServer.session" |
153 | serverSession cfg (restrictHandleSocket h (fst con)) conkey h | 152 | serverSession cfg (restrictHandleSocket h con, (Local laddr, Remote raddr)) conkey h |
154 | acceptLoop cfg sock (n + 1) | 153 | acceptLoop cfg sock (n + 1) |
155 | 154 | ||
156 | acceptException :: ServerConfig -> Int -> Socket -> IOError -> IO () | 155 | acceptException :: ServerConfig -> Int -> Socket -> IOError -> IO () |