admin 管理员组

文章数量: 887021


2024年1月18日发(作者:head什么意思中文)

enum OVERLAPPED_TYPE{ RECV = 0, SEND, CONNECT};enum ADDRESS_TYPE{ LOCAL = 0, REMOTE};enum SOCKET_STATUS{ NONE = 0x0, ACCEPTING = 0x1, CONNECTING = 0x2, HANDSHAKING = 0x4, CONNECTED = 0x8, RECEIVING = 0x10, SENDING = 0x20, CLOSING = 0x40, CLOSED = 0x80, OPERATING = ACCEPTING | CONNECTING | HANDSHAKING | RECEIVING | SENDING};struct session;struct session_overlapped{ OVERLAPPED overlapped; DWORD result; session *psession;};struct session{ SOCKET s; // handle to socket char socket_buffer[2][BUFFER_SIZE]; // memory used for read/write from/to socket char ssl_buffer[2][BUFFER_SIZE]; // memory used for read/write from/to ssl memory bio DWORD ssl_buffer_size[2]; // indicates the bytes of valid data in ssl_buffer unsigned int status; // stores current socket status, bit-masked value of one or more of SOCKET_STATUS session_overlapped overlapped[3]; // structure for overlapped operations WSABUF wsabuf[2]; // structure used for pass buffer to overlapped operations DWORD bytes_transferred[2]; // store the bytes of buffer that received/sent from/to the socket DWORD wsa_flags[2]; // store the flags send/receive from overlapped operations, not used SSL *ssl; // SSL structure used by OpenSSL BIO *bio[2]; // memory BIO used by OpenSSL ssl_lock lock; // synchronization object for multiple-thread data access};- ocp处理关键流程bool session_process(session *psession){ bool fatal_error_occurred = false; if(nullptr != psession->ssl) { if(psession->bytes_transferred[RECV] > 0) { int bytes = BIO_write(psession->bio[RECV], psession->socket_buffer[RECV], psession->bytes_transferred[RECV]); if(bytes == psession->bytes_transferred[RECV]) { psession->bytes_transferred[RECV] = 0; }

} } if(psession->ssl_buffer_size[RECV] == 0) { int bytes = 0; do { bytes = SSL_read(psession->ssl, psession->ssl_buffer[RECV], BUFFER_SIZE); if ((HANDSHAKING == (psession->status & HANDSHAKING)) && SSL_is_init_finished(psession->ssl)) { psession->status &= ~HANDSHAKING; psession->status |= CONNECTED; app_on_session_connect(psession); } if (bytes > 0) { psession->ssl_buffer_size[RECV] = bytes; app_on_session_recv(psession); psession->ssl_buffer_size[RECV] = 0; } } while (bytes > 0); } if(psession->ssl_buffer_size[SEND] > 0) { int bytes = SSL_write(psession->ssl, psession->ssl_buffer[SEND], psession->ssl_buffer_size[SEND]); if(bytes == psession->ssl_buffer_size[SEND]) { psession->ssl_buffer_size[SEND] = 0; } } if(psession->wsabuf[SEND].len == 0 && (0 != psession->s_listening || BIO_pending(psession->bio[SEND]))) { int bytes = BIO_read(psession->bio[SEND], psession->socket_buffer[SEND], BUFFER_SIZE); if(bytes > 0) { psession->wsabuf[SEND].len = bytes; } } if(fatal_error_occurred) session_close(psession); } session_send(psession); session_recv(psession);

return !fatal_error_occurred;}


本文标签: 流程 意思 处理 作者 关键