예제 #1
0
        private LinuxError ConvertError(WsaError errorCode)
        {
            if (!_errorMap.TryGetValue(errorCode, out LinuxError errno))
            {
                errno = (LinuxError)errorCode;
            }

            return(errno);
        }
예제 #2
0
        public ResultCode Write(out int writtenCount, ReadOnlyMemory <byte> buffer)
        {
            if (!Socket.Poll(0, SelectMode.SelectWrite))
            {
                writtenCount = 0;

                return(ResultCode.WouldBlock);
            }

            StartSslOperation();

            try
            {
                _stream.Write(buffer.Span);
            }
            catch (IOException exception)
            {
                writtenCount = -1;

                if (exception.InnerException is SocketException socketException)
                {
                    WsaError socketErrorCode = (WsaError)socketException.SocketErrorCode;

                    if (TryTranslateWinSockError(_isBlockingSocket, socketErrorCode, out ResultCode result))
                    {
                        return(result);
                    }
                    else
                    {
                        throw socketException;
                    }
                }
                else
                {
                    throw exception;
                }
            }
            finally
            {
                EndSslOperation();
            }

            // .NET API doesn't provide the size written, assume all written.
            writtenCount = buffer.Length;

            return(ResultCode.Success);
        }
예제 #3
0
        public ResultCode Read(out int readCount, Memory <byte> buffer)
        {
            if (!Socket.Poll(0, SelectMode.SelectRead))
            {
                readCount = -1;

                return(ResultCode.WouldBlock);
            }

            StartSslReadOperation();

            try
            {
                readCount = _stream.Read(buffer.Span);
            }
            catch (IOException exception)
            {
                readCount = -1;

                if (exception.InnerException is SocketException socketException)
                {
                    WsaError socketErrorCode = (WsaError)socketException.SocketErrorCode;

                    if (TryTranslateWinSockError(_isBlockingSocket, socketErrorCode, out ResultCode result))
                    {
                        return(result);
                    }
                    else
                    {
                        throw socketException;
                    }
                }
                else
                {
                    throw exception;
                }
            }
            finally
            {
                EndSslReadOperation();
            }

            return(ResultCode.Success);
        }
예제 #4
0
        private static bool TryTranslateWinSockError(bool isBlocking, WsaError error, out ResultCode resultCode)
        {
            switch (error)
            {
            case WsaError.WSAETIMEDOUT:
                resultCode = isBlocking ? ResultCode.Timeout : ResultCode.WouldBlock;
                return(true);

            case WsaError.WSAECONNABORTED:
                resultCode = ResultCode.ConnectionAbort;
                return(true);

            case WsaError.WSAECONNRESET:
                resultCode = ResultCode.ConnectionReset;
                return(true);

            default:
                resultCode = ResultCode.Success;
                return(false);
            }
        }
예제 #5
0
 private ResultCode WriteWinSock2Error(ServiceCtx context, WsaError errorCode)
 {
     return(WriteBsdResult(context, -1, ConvertError(errorCode)));
 }