Class: TinyDTLS::Session
- Inherits:
-
Object
- Object
- TinyDTLS::Session
- Defined in:
- lib/tinydtls/session.rb
Overview
This class offers a higher-level abstraction for the ‘session_t` type.
Instance Attribute Summary collapse
-
#addrinfo ⇒ Object
readonly
Returns the value of attribute addrinfo.
Class Method Summary collapse
-
.addr_from_ptr(ptr) ⇒ Object
Extracts an Addrinfo instance of a FFI::Pointer to a ‘session_t` as returned by #to_ptr.
Instance Method Summary collapse
-
#close(ctx = nil) ⇒ Object
Frees all resources associated with the underlying ‘session_t`.
-
#initialize(addrinfo) ⇒ Session
constructor
Creates a new instance of this class from a given Addrinfo instance.
-
#to_ptr ⇒ Object
Converts the object into a C pointer to a ‘session_t` tinydtls type.
Constructor Details
#initialize(addrinfo) ⇒ Session
Creates a new instance of this class from a given Addrinfo instance. This functions allocates memory for the underlying ‘session_t` type which needs to be freed explicitly freed using #close.
10 11 12 13 14 15 16 17 18 19 20 21 |
# File 'lib/tinydtls/session.rb', line 10 def initialize(addrinfo) @addrinfo = addrinfo unless @addrinfo.is_a? Addrinfo raise TypeError.new("Expected Addrinfo or FFI::Pointer") end sockaddr = @addrinfo.to_sockaddr @session = Wrapper::dtls_new_session(sockaddr, sockaddr.bytesize) if @session.null? raise Errno::ENOMEM end end |
Instance Attribute Details
#addrinfo ⇒ Object (readonly)
Returns the value of attribute addrinfo.
4 5 6 |
# File 'lib/tinydtls/session.rb', line 4 def addrinfo @addrinfo end |
Class Method Details
.addr_from_ptr(ptr) ⇒ Object
Extracts an Addrinfo instance of a FFI::Pointer to a ‘session_t` as returned by #to_ptr.
25 26 27 28 29 30 |
# File 'lib/tinydtls/session.rb', line 25 def self.addr_from_ptr(ptr) lenptr = Wrapper::SocklenPtr.new sockaddr = Wrapper::dtls_session_addr(ptr, lenptr) Addrinfo.new(sockaddr.read_string(lenptr[:value])) end |
Instance Method Details
#close(ctx = nil) ⇒ Object
Frees all resources associated with the underlying ‘session_t`. Optionally it also resets all peer connections associated with the session (if any). In order to do so a TinyDTLS::Context needs to be passed.
43 44 45 46 47 48 49 50 51 |
# File 'lib/tinydtls/session.rb', line 43 def close(ctx = nil) unless ctx.nil? peer = Wrapper::dtls_get_peer(ctx.to_ffi, @session) Wrapper::dtls_reset_peer(ctx.to_ffi, peer) unless peer.null? end Wrapper::dtls_free_session(@session) @session = nil end |
#to_ptr ⇒ Object
Converts the object into a C pointer to a ‘session_t` tinydtls type. This pointer can be passed to various functions provided by TinyDTLS::Wrapper.
35 36 37 |
# File 'lib/tinydtls/session.rb', line 35 def to_ptr @session end |