Sha256: 624d97deb844494d48edd200c1ccd38b81628bf45fb2b98bb50fd209b5231897

Contents?: true

Size: 1.5 KB

Versions: 10

Compression:

Stored size: 1.5 KB

Contents

# frozen_string_literal: true

# Released under the MIT License.
# Copyright, 2023, by Samuel Williams.

require_relative 'generic'
require_relative 'composite_endpoint'
require_relative 'socket_endpoint'

require 'openssl'

module IO::Endpoint
	class ConnectedEndpoint < Generic
		def self.connected(endpoint, close_on_exec: false)
			socket = endpoint.connect
			
			socket.close_on_exec = close_on_exec
			
			return self.new(endpoint, socket, **endpoint.options)
		end
		
		def initialize(endpoint, socket, **options)
			super(**options)
			
			@endpoint = endpoint
			@socket = socket
		end
		
		attr :endpoint
		attr :socket
		
		# A endpoint for the local end of the bound socket.
		# @returns [AddressEndpoint] A endpoint for the local end of the connected socket.
		def local_address_endpoint(**options)
			AddressEndpoint.new(socket.to_io.local_address, **options)
		end
		
		# A endpoint for the remote end of the bound socket.
		# @returns [AddressEndpoint] A endpoint for the remote end of the connected socket.
		def remote_address_endpoint(**options)
			AddressEndpoint.new(socket.to_io.remote_address, **options)
		end
		
		def connect(wrapper = Wrapper.default, &block)
			if block_given?
				yield @socket
			else
				return @socket.dup
			end
		end
		
		def close
			if @socket
				@socket.close
				@socket = nil
			end
		end
		
		def to_s
			"\#<#{self.class} #{@socket} connected for #{@endpoint}>"
		end
	end
		
	class Generic
		def connected(**options)
			ConnectedEndpoint.connected(self, **options)
		end
	end
end

Version data entries

10 entries across 10 versions & 1 rubygems

Version Path
io-endpoint-0.10.1 lib/io/endpoint/connected_endpoint.rb
io-endpoint-0.10.0 lib/io/endpoint/connected_endpoint.rb
io-endpoint-0.9.0 lib/io/endpoint/connected_endpoint.rb
io-endpoint-0.8.1 lib/io/endpoint/connected_endpoint.rb
io-endpoint-0.8.0 lib/io/endpoint/connected_endpoint.rb
io-endpoint-0.7.2 lib/io/endpoint/connected_endpoint.rb
io-endpoint-0.7.1 lib/io/endpoint/connected_endpoint.rb
io-endpoint-0.7.0 lib/io/endpoint/connected_endpoint.rb
io-endpoint-0.6.0 lib/io/endpoint/connected_endpoint.rb
io-endpoint-0.5.0 lib/io/endpoint/connected_endpoint.rb