Sha256: ad7a8b91622b60609856ed2137940baf37ff8843436111ade1b87cd3d7117204

Contents?: true

Size: 1.55 KB

Versions: 1

Compression:

Stored size: 1.55 KB

Contents

# frozen_string_literal: true

# Released under the MIT License.
# Copyright, 2024, 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
			"connected:#{@endpoint}"
		end
		
		def inspect
			"\#<#{self.class} #{@socket} connected for #{@endpoint}>"
		end
	end
		
	class Generic
		def connected(**options)
			ConnectedEndpoint.connected(self, **options)
		end
	end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
io-endpoint-0.14.0 lib/io/endpoint/connected_endpoint.rb