Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactoring after #216 changes #218

Merged
merged 2 commits into from
Aug 4, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 9 additions & 27 deletions Network/Socket/ByteString.hsc
Original file line number Diff line number Diff line change
Expand Up @@ -43,14 +43,15 @@ module Network.Socket.ByteString
-- $example
) where

import Control.Exception (catch, throwIO)
import Control.Monad (when)
import Data.ByteString (ByteString)
import Data.ByteString.Internal (createAndTrim)
import Data.ByteString.Unsafe (unsafeUseAsCStringLen)
import Data.Word (Word8)
import Foreign.Marshal.Alloc (allocaBytes)
import Foreign.Ptr (Ptr, castPtr)
import Network.Socket (sendBuf, sendBufTo, recvBufFrom)
import Foreign.Ptr (castPtr)
import Network.Socket (sendBuf, sendBufTo, recvBuf, recvBufFrom)
import System.IO.Error (isEOFError)

import qualified Data.ByteString as B

Expand All @@ -62,20 +63,11 @@ import Network.Socket.Types
import Control.Monad (liftM, zipWithM_)
import Foreign.Marshal.Array (allocaArray)
import Foreign.Marshal.Utils (with)
import Foreign.Ptr (plusPtr)
import Foreign.Ptr (Ptr, plusPtr)
import Foreign.Storable (Storable(..))
import Foreign.C.Types (CChar, CSize(..), CInt(..))

import Network.Socket.ByteString.IOVec (IOVec(..))
import Network.Socket.ByteString.MsgHdr (MsgHdr(..))

#else
import GHC.IO.FD (FD(..), readRawBufferPtr)
#endif

#if !defined(mingw32_HOST_OS)
foreign import CALLCONV unsafe "recv"
c_recv :: CInt -> Ptr CChar -> CSize -> CInt -> IO CInt
#endif

-- ----------------------------------------------------------------------------
Expand Down Expand Up @@ -233,20 +225,10 @@ recv :: Socket -- ^ Connected socket
-> IO ByteString -- ^ Data received
recv sock nbytes
| nbytes < 0 = ioError (mkInvalidRecvArgError "Network.Socket.ByteString.recv")
| otherwise = createAndTrim nbytes $ recvInner sock nbytes

recvInner :: Socket -> Int -> Ptr Word8 -> IO Int
recvInner sock nbytes ptr =
fmap fromIntegral $
#if defined(mingw32_HOST_OS)
throwSocketErrorIfMinus1Retry "Network.Socket.recvBuf" $
readRawBufferPtr "Network.Socket.ByteString.recv" (FD s 1) ptr 0 (fromIntegral nbytes)
#else
throwSocketErrorWaitRead sock "recv" $
c_recv s (castPtr ptr) (fromIntegral nbytes) 0
#endif
where
s = sockFd sock
| otherwise = createAndTrim nbytes $ \ptr ->
catch
(recvBuf sock ptr nbytes)
(\e -> if isEOFError e then return 0 else throwIO e)

-- | Receive data from the socket. The socket need not be in a
-- connected state. Returns @(bytes, address)@ where @bytes@ is a
Expand Down
1 change: 0 additions & 1 deletion network.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,6 @@ test-suite simple
test-suite regression
hs-source-dirs: tests
main-is: Regression.hs
other-modules: Regression.Issue215
type: exitcode-stdio-1.0

build-depends:
Expand Down
3 changes: 0 additions & 3 deletions tests/Regression.hs
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@ import Test.Framework (Test, defaultMain)
import Test.Framework.Providers.HUnit (testCase)
import Test.HUnit (assertFailure)

import qualified Regression.Issue215 as Issue215

------------------------------------------------------------------------
-- Tests

Expand Down Expand Up @@ -51,7 +49,6 @@ tests =
[ testCase "testGetAddrInfo" testGetAddrInfo
, testCase "badRecvShouldThrow" badRecvShouldThrow
, testCase "badSendShouldThrow" badSendShouldThrow
, testCase "recvShouldntThrowOnClosedSocket" Issue215.main
]

------------------------------------------------------------------------
Expand Down
29 changes: 0 additions & 29 deletions tests/Regression/Issue215.hs

This file was deleted.

23 changes: 22 additions & 1 deletion tests/Simple.hs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import qualified Data.ByteString.Char8 as C
import Data.Maybe (fromJust)
#endif
import Network.Socket hiding (recv, recvFrom, send, sendTo)
import qualified Network.Socket (recv)
import Network.Socket.ByteString

--- To tests for AF_CAN on Linux, you need to bring up a virtual (or real can
Expand All @@ -30,7 +31,7 @@ import Network.BSD (ifNameToIndex)
#endif
import Test.Framework (Test, defaultMain, testGroup)
import Test.Framework.Providers.HUnit (testCase)
import Test.HUnit (Assertion, (@=?))
import Test.HUnit (Assertion, (@=?), assertFailure)

------------------------------------------------------------------------

Expand Down Expand Up @@ -229,6 +230,24 @@ canTest ifname clientAct serverAct = do
serverSetup = clientSetup
#endif

-- The String version of 'recv' should throw an exception when the remote end
-- has closed the connection, the ByteString version should return an empty
-- ByteString.
testStringEol :: Assertion
testStringEol = tcpTest client close
where client s = do
res <- E.try $ Network.Socket.recv s 4096
case res of
Left (_ :: IOError) -> return ()
Right _ -> assertFailure
"String recv didn't throw an exception on a closed socket"

testByteStringEol :: Assertion
testByteStringEol = tcpTest client close
where client s = do
res :: Either IOError C.ByteString <- E.try $ recv s 4096
res @=? Right S.empty

------------------------------------------------------------------------
-- Conversions of IP addresses

Expand Down Expand Up @@ -287,6 +306,8 @@ basicTests = testGroup "Basic socket operations"
, testCase "testUserTimeout" testUserTimeout
-- , testCase "testGetPeerCred" testGetPeerCred
-- , testCase "testGetPeerEid" testGetPeerEid
, testCase "testStringEol" testStringEol
, testCase "testByteStringEol" testByteStringEol
#if defined(HAVE_LINUX_CAN_H)
, testCase "testCanSend" testCanSend
#endif
Expand Down