# ACTIVESTATE TEAPOT-PKG BEGIN TM -*- tcl -*- # -- Tcl Module # @@ Meta Begin # Package transfer::connect 0.2 # Meta as::build::date 2015-05-25 # Meta as::origin http://sourceforge.net/projects/tcllib # Meta category Data transfer facilities # Meta description Connection setup # Meta license BSD # Meta platform tcl # Meta require {Tcl 8.4} # Meta require snit # Meta subject connection active transfer channel ssl tls secure # Meta subject passive # Meta summary transfer::connect # @@ Meta End # ACTIVESTATE TEAPOT-PKG BEGIN REQUIREMENTS package require Tcl 8.4 package require snit # ACTIVESTATE TEAPOT-PKG END REQUIREMENTS # ACTIVESTATE TEAPOT-PKG BEGIN DECLARE package provide transfer::connect 0.2 # ACTIVESTATE TEAPOT-PKG END DECLARE # ACTIVESTATE TEAPOT-PKG END TM # -*- tcl -*- # ### ### ### ######### ######### ######### ## # Class for handling of active/passive connectivity. # ### ### ### ######### ######### ######### ## Requirements package require snit # ### ### ### ######### ######### ######### ## Implementation snit::type ::transfer::connect { # ### ### ### ######### ######### ######### ## API option -host -default localhost option -port -default 0 option -mode -default active -type {snit::enum -values {active passive}} option -socketcmd -default ::socket option -translation -default {} option -encoding -default {} option -eofchar -default {} method connect {command} {} # active: # - connect to host/port # # passive: # - listen on port for connection # ### ### ### ######### ######### ######### ## Implementation method connect {command} { if {$options(-mode) eq "active"} { set sock [Socket $options(-host) $options(-port)] $self ConfigureTheOpenedSocket $sock $command return } else { set mysock [Socket -server [mymethod IsConnected $command] \ $options(-port)] # Return port the server socket is listening on for the # connection. return [lindex [fconfigure $mysock -sockname] 2] } return } # ### ### ### ######### ######### ######### ## Internal helper commands. method IsConnected {command sock peerhost peerport} { # Accept only a one connection. close $mysock $self ConfigureTheOpenedSocket $sock $command return } method ConfigureTheOpenedSocket {sock command} { foreach o {-translation -encoding -eofchar} { if {$options($o) eq ""} continue fconfigure $sock $o $options($o) } after 0 [linsert $command end $self $sock] return } # ### ### ### ######### ######### ######### ## Internal helper commands. proc Socket {args} { upvar 1 options(-socketcmd) socketcmd return [eval [linsert $args 0 $socketcmd]] } # ### ### ### ######### ######### ######### ## Data structures variable mysock {} ## # ### ### ### ######### ######### ######### } # ### ### ### ######### ######### ######### ## Ready package provide transfer::connect 0.2