本文簡要介紹ruby語言中 OpenSSL::Buffering.write_nonblock 的用法。
用法
write_nonblock(s, exception: true)
以非阻塞方式寫入s。
如果有緩衝數據,則首先刷新。這可能會阻塞。
write_nonblock 返回寫入 SSL 連接的字節數。
當沒有數據可以寫入而不阻塞時,它會引發由 IO::WaitReadable 或 IO::WaitWritable 擴展的 OpenSSL::SSL::SSLError 。
IO::WaitReadable 表示 SSL 需要在內部讀取,因此在底層 IO 可讀後應再次調用 write_nonblock 。
IO::WaitWritable 表示 SSL 需要在內部寫入,因此在底層 IO 可寫後應再次調用 write_nonblock 。
所以 OpenSSL::Buffering#write_nonblock 需要兩個rescue子句如下。
# emulates blocking write.
begin
result = ssl.write_nonblock(str)
rescue IO::WaitReadable
IO.select([io])
retry
rescue IO::WaitWritable
IO.select(nil, [io])
retry
end
請注意, write_nonblock 從底層 IO 讀取的一個原因是當對等方請求新的 TLS/SSL 握手時。有關更多詳細信息,請參閱 openssl 常見問題解答。 www.openssl.org/support/faq.html
通過將關鍵字參數 exception 指定為 false ,您可以指示 write_nonblock 不應引發 IO::Wait*able 異常,而是返回符號 :wait_writable 或 :wait_readable。
相關用法
- Ruby Buffering.read_nonblock用法及代碼示例
- Ruby Buffer.slice用法及代碼示例
- Ruby Buffer.set_value用法及代碼示例
- Ruby Buffer.external?用法及代碼示例
- Ruby Buffer.free用法及代碼示例
- Ruby Buffer.new用法及代碼示例
- Ruby Buffer.for用法及代碼示例
- Ruby Buffer.locked?用法及代碼示例
- Ruby Buffer類用法及代碼示例
- Ruby Buffer.transfer用法及代碼示例
- Ruby Buffer.map用法及代碼示例
- Ruby Buffer.clear用法及代碼示例
- Ruby Buffer.copy用法及代碼示例
- Ruby Buffer.locked用法及代碼示例
- Ruby Buffer.get_value用法及代碼示例
- Ruby Buffer.get_string用法及代碼示例
- Ruby Buffer.to_s用法及代碼示例
- Ruby Buffer.resize用法及代碼示例
- Ruby Bundler.setup用法及代碼示例
- Ruby Bundler模塊用法及代碼示例
- Ruby Bundler.require用法及代碼示例
- Ruby Bundler.definition用法及代碼示例
- Ruby BigMath.cos用法及代碼示例
- Ruby Binding.local_variable_defined?用法及代碼示例
- Ruby BigDecimal.self >用法及代碼示例
注:本文由純淨天空篩選整理自ruby-lang.org大神的英文原創作品 Buffering.write_nonblock。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。
