继上一篇阻塞情况下OpenSSL库的调用,如果是非阻塞情况

**前言:**在现的项目中是非阻塞的情况,不可能在等待客户端来连接,请他事情不做,accept默认阻塞,使其不阻塞。如果在非阻塞情况下,SSL_accept返回值为-1.

阻塞:

unsigned long iMode = 1;	
//set to non-blocking
if(ioctlsocket(listen_sock,FIONBIO, (unsigned long *)&iMode) < 0)
{
	closesocket(listen_sock);
	LOGERR("set socket non blocking failed!\n");
	return (g_err_code = UAI_ERROR);
}

然后在调用SSL_accept的时候

while(true)
{
    int SSLacceptReturnCode = SSL_accept(st_handle.handle);
    if(SSLacceptReturnCode == 0)
    {
        LOGERR("SSL_get_error:%d\n", SSL_get_error((const SSL*)st_handle.handle, -1));
        LOGERR("SSL_accpect %s\n",SSL_state_string_long((const SSL*)st_handle.handle));

        SSL_shutdown((SSL*)st_handle.handle);
        SSL_free((SSL*)st_handle.handle);
        closesocket(iClientSocket);

        SSL_CTX_free((SSL_CTX *)st_handle.context);
        return (g_err_code = UAI_ERROR);
    }
    else if(SSLacceptReturnCode == 1)
    {
        break;
    }
    else if(SSLacceptReturnCode < 0)
    {
        int SSLgetErrorCode = SSL_get_error(st_handle.handle, SSLacceptReturnCode);
        if(SSLgetErrorCode == SSL_ERROR_WANT_READ || SSLgetErrorCode == SSL_ERROR_WANT_WRITE)
        {
            /* Wait for data to be read */
            continue;
        }
        else
        {
            LOGERR("SSL_get_error:%d\n", SSL_get_error((const SSL*)st_handle.handle, -1));
            LOGERR("SSL_accpect %s\n",SSL_state_string_long((const SSL*)st_handle.handle));

            SSL_shutdown((SSL*)st_handle.handle);
            SSL_free((SSL*)st_handle.handle);
            closesocket(iClientSocket);

            SSL_CTX_free((SSL_CTX *)st_handle.context);
            return (g_err_code = UAI_ERROR);;
        }
    }
    else
    {
        LOGERR("SSL_get_error:%d\n", SSL_get_error((const SSL*)st_handle.handle, -1));
        LOGERR("SSL_accpect %s\n",SSL_state_string_long((const SSL*)st_handle.handle));

        SSL_shutdown((SSL*)st_handle.handle);
        SSL_free((SSL*)st_handle.handle);
        closesocket(iClientSocket);

        SSL_CTX_free((SSL_CTX *)st_handle.context);
        return (g_err_code = UAI_ERROR);;
    }
}

读取的时候:

int ires = 0, count = 0;;
bool isCoutinue = true;
while (true)
{
	memset(buf,'\0',sizeof(buf));
	count = 0;
	while (isCoutinue)
	{
		ires = SSL_read(ssl, buf + count, 12 - count);
		int nRes = SSL_get_error(ssl, ires);
		if(nRes == SSL_ERROR_NONE)
		{
			if(ires > 0)
			{
				count += ires;
				if (count >= 12)
				{
					cout << buf << endl;
					break;
				}
				continue;
			}
		}
		else if (nRes == SSL_ERROR_WANT_READ)
		{
			continue;
		}
		else
		{
			break;
		}
	}
	Sleep(500);
}

Handling Non-Blocking I/O Errors in OpenSSL
SSL_accept()
After result of: Select on: Try this next:
<0, SSL_ERROR_WANT_READ R SSL_accept()
<0, SSL_ERROR_WANT_WRITE W SSL_accept()
0, SSL_ERROR_SYSCALL nothing (illegal EOF)
<0, SSL_ERROR_SYSCALL see errno

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值