Class: NATS::IO::Socket
- Defined in:
- lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/nats-pure-2.2.1/lib/nats/io/client.rb
Overview
Implementation adapted from github.com/redis/redis-rb
Instance Attribute Summary collapse
-
#socket ⇒ Object
Returns the value of attribute socket.
Instance Method Summary collapse
- #close ⇒ Object
- #closed? ⇒ Boolean
- #connect ⇒ Object
-
#initialize(options = {}) ⇒ Socket
constructor
A new instance of Socket.
- #read(max_bytes, deadline = nil) ⇒ Object
- #read_line(deadline = nil) ⇒ Object
- #write(data, deadline = nil) ⇒ Object
Constructor Details
#initialize(options = {}) ⇒ Socket
Returns a new instance of Socket.
1820 1821 1822 1823 1824 1825 1826 |
# File 'lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/nats-pure-2.2.1/lib/nats/io/client.rb', line 1820 def initialize(={}) @uri = [:uri] @connect_timeout = [:connect_timeout] @write_timeout = [:write_timeout] @read_timeout = [:read_timeout] @socket = nil end |
Instance Attribute Details
#socket ⇒ Object
Returns the value of attribute socket.
1818 1819 1820 |
# File 'lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/nats-pure-2.2.1/lib/nats/io/client.rb', line 1818 def socket @socket end |
Instance Method Details
#close ⇒ Object
1909 1910 1911 |
# File 'lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/nats-pure-2.2.1/lib/nats/io/client.rb', line 1909 def close @socket.close end |
#closed? ⇒ Boolean
1913 1914 1915 |
# File 'lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/nats-pure-2.2.1/lib/nats/io/client.rb', line 1913 def closed? @socket.closed? end |
#connect ⇒ Object
1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 |
# File 'lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/nats-pure-2.2.1/lib/nats/io/client.rb', line 1828 def connect addrinfo = ::Socket.getaddrinfo(@uri.hostname, nil, ::Socket::AF_UNSPEC, ::Socket::SOCK_STREAM) addrinfo.each_with_index do |ai, i| begin @socket = connect_addrinfo(ai, @uri.port, @connect_timeout) break rescue SystemCallError => e # Give up if no more available raise e if addrinfo.length == i+1 end end # Set TCP no delay by default @socket.setsockopt(::Socket::IPPROTO_TCP, ::Socket::TCP_NODELAY, 1) end |
#read(max_bytes, deadline = nil) ⇒ Object
1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 |
# File 'lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/nats-pure-2.2.1/lib/nats/io/client.rb', line 1852 def read(max_bytes, deadline=nil) begin return @socket.read_nonblock(max_bytes) rescue ::IO::WaitReadable if ::IO.select([@socket], nil, nil, deadline) retry else raise NATS::IO::SocketTimeoutError end rescue ::IO::WaitWritable if ::IO.select(nil, [@socket], nil, deadline) retry else raise NATS::IO::SocketTimeoutError end end rescue EOFError => e if RUBY_ENGINE == 'jruby' and e. == 'No message available' # FIXME: <EOFError: No message available> can happen in jruby # even though seems it is temporary and eventually possible # to read from socket. return nil end raise Errno::ECONNRESET end |
#read_line(deadline = nil) ⇒ Object
1844 1845 1846 1847 1848 1849 1850 |
# File 'lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/nats-pure-2.2.1/lib/nats/io/client.rb', line 1844 def read_line(deadline=nil) # FIXME: Should accumulate and read in a non blocking way instead unless ::IO.select([@socket], nil, nil, deadline) raise NATS::IO::SocketTimeoutError end @socket.gets end |
#write(data, deadline = nil) ⇒ Object
1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 |
# File 'lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/nats-pure-2.2.1/lib/nats/io/client.rb', line 1879 def write(data, deadline=nil) length = data.bytesize total_written = 0 loop do begin written = @socket.write_nonblock(data) total_written += written break total_written if total_written >= length data = data.byteslice(written..-1) rescue ::IO::WaitWritable if ::IO.select(nil, [@socket], nil, deadline) retry else raise NATS::IO::SocketTimeoutError end rescue ::IO::WaitReadable if ::IO.select([@socket], nil, nil, deadline) retry else raise NATS::IO::SocketTimeoutError end end end rescue EOFError raise Errno::ECONNRESET end |