Class: LibTLS::OpenedClient
- Inherits:
-
Object
- Object
- LibTLS::OpenedClient
- Defined in:
- lib/libtls/client.rb
Overview
A TLS client connected to a server
This class must be instantiated only by Client#connect and Server#accept.
When finished, #close must be called. This is implicitly handled for you by passing a block to the methods mentioned above.
Instance Attribute Summary collapse
-
#ctx ⇒ Object
readonly
The FFI wrapper around the struct tls object.
Instance Method Summary collapse
-
#close ⇒ Object
Close this connection.
-
#initialize(ctx) ⇒ OpenedClient
constructor
private
A new instance of OpenedClient.
-
#read ⇒ String
Read a string from the connection.
-
#write(str) ⇒ Object
Write the string to the connection.
Constructor Details
#initialize(ctx) ⇒ OpenedClient
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Returns a new instance of OpenedClient.
136 137 138 |
# File 'lib/libtls/client.rb', line 136 def initialize(ctx) @ctx = ctx end |
Instance Attribute Details
#ctx ⇒ Object (readonly)
The FFI wrapper around the struct tls object
This is only useful for calling any of the Raw methods.
132 133 134 |
# File 'lib/libtls/client.rb', line 132 def ctx @ctx end |
Instance Method Details
#close ⇒ Object
Close this connection
This method must be called either implicitly by passing a block to Client#connect or Server#accept, or explicitly by you.
145 146 147 148 149 |
# File 'lib/libtls/client.rb', line 145 def close if LibTLS::Raw.tls_close(ctx) < 0 raise LibTLS::CError, "tls_close: #{LibTLS::Raw.tls_error(ctx)}" end end |
#read ⇒ String
Read a string from the connection
173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 |
# File 'lib/libtls/client.rb', line 173 def read str = "" FFI::MemoryPointer.new(:size_t) do |outlen| FFI::MemoryPointer.new(:uchar, READ_LEN, true) do |buf| loop do if LibTLS::Raw.tls_read(ctx, buf, READ_LEN, outlen) < 0 raise LibTLS::CError, "tls_read: #{LibTLS::Raw.tls_error(ctx)}" end str += buf.get_string(0, outlen.get_int(0)) if READ_LEN > outlen.get_int(0) break end end end end str end |
#write(str) ⇒ Object
Write the string to the connection
156 157 158 159 160 161 162 163 164 165 166 |
# File 'lib/libtls/client.rb', line 156 def write(str) FFI::MemoryPointer.new(:size_t) do |outlen| FFI::MemoryPointer.new(:uchar, str.length + 1) do |str_ptr| str_ptr.put_string(0, str) if LibTLS::Raw.tls_write(ctx, str_ptr, str.length, outlen) < 0 raise LibTLS::CError, "tls_write: #{LibTLS::Raw.tls_error(ctx)}" end end end end |