ionian 0.6.8 → 0.6.9

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/lib/ionian/socket.rb CHANGED
@@ -22,24 +22,55 @@ module Ionian
22
22
 
23
23
  # Creates a new socket or wraps an existing socket.
24
24
  #
25
- # Args:
26
- # host: IP or hostname to connect to. Can contain the port in the format "host:port".
27
- # port: Connection's port number. Default is 23. Unused by :unix protocol.
28
- # protocol: Type of socket to create. :tcp, :udp, :unix. Default is :tcp.
29
- # :udp will be automatically selected for addresses in the multicast range.
30
- # persistent: The socket remains open after data is sent if this is true.
31
- # The socket closes after data is sent and a packet is received
32
- # if this is false. Default is true.
33
- # bind_port: Local UDP port to bind to for receiving data, if different than
34
- # the remote port being connected to.
35
- # reuse_addr: Set true to enable the SO_REUSEADDR flag. Allows local address reuse.
36
- # no_delay: Set true to enable the TCP_NODELAY flag. Disables Nagle algorithm.
37
- # cork: Set true to enable the TCP_CORK flag. Buffers multiple writes
38
- # into one segment.
39
- # linger: Set true to enable the SO_LINGER flag. When #close is called,
40
- # waits for the send buffer to empty before closing the socket.
41
- # expression: Overrides the #read_match regular expression for received data.
42
- def initialize existing_socket = nil, **kwargs
25
+ #
26
+ # @param existing_socket [Socket] An instantiated socket to be wrapped in
27
+ # and returned as an {Ionian::Socket} (for example, TCPSocket). A new
28
+ # socket will be created if this parameter is nil.
29
+ #
30
+ #
31
+ # @param kwargs [Hash] :host is mandatory.
32
+ #
33
+ # @option kwargs [String] :host IP or hostname to connect to.
34
+ # Can contain the port in the format "host:port".
35
+ #
36
+ # @option kwargs [Fixnum] :port (23) Connection's port number. Unused by the
37
+ # :unix protocol.
38
+ #
39
+ # @option kwargs [:tcp, :udp, :unix] :protocol (:tcp) Type of socket to create.
40
+ # :udp will be automatically selected for addresses in the multicast
41
+ # range, or if the broadcast flag is set.
42
+ #
43
+ # @option kwargs [Boolean] :persistent (true) The socket remains open after
44
+ # data is sent if this is true. The socket closes after data is sent and
45
+ # a packet is received if this is false.
46
+ #
47
+ # @option kwargs [Boolean] :bind_port (:port) Local UDP port to bind to for
48
+ # receiving data, if different than the remote port being connected to.
49
+ #
50
+ # @option kwargs [Boolean] :broadcast (false) Enable the SO_BROADCAST flag.
51
+ # Sets protocol to :udp implicitly.
52
+ #
53
+ # @option kwargs [Boolean] :reuse_addr (false) Enable the SO_REUSEADDR flag.
54
+ # Allows local address reuse.
55
+ #
56
+ # @option kwargs [Boolean] :no_delay (false) Enable the TCP_NODELAY flag.
57
+ # Disables Nagle algorithm.
58
+ #
59
+ # @option kwargs [Boolean] :cork (false) Enable the TCP_CORK flag.
60
+ # Buffers multiple writes into one segment.
61
+ #
62
+ # @option kwargs [Boolean] :linger (false) Enable the SO_LINGER flag.
63
+ # When #close is called, waits for the send buffer to empty before closing
64
+ # the socket.
65
+ #
66
+ # @option kwargs [Regexp, String] :expression Overrides the
67
+ # {Ionian::Extension::IO#read_match} regular expression for received data.
68
+ #
69
+ #
70
+ # @yieldparam socket [Ionian::Socket] This socket is yielded to the block.
71
+ # Socket flushes and closes when exiting the block.
72
+ #
73
+ def initialize existing_socket = nil, **kwargs, &block
43
74
  @socket = existing_socket
44
75
 
45
76
  @ionian_listeners = []
@@ -67,7 +98,7 @@ module Ionian
67
98
  @protocol = :unix
68
99
  end
69
100
 
70
- @persistent = true # Existing sockets are always persistent.
101
+ @persistent = true # Existing sockets are always persistent.
71
102
 
72
103
  @socket.expression = @expression if @expression
73
104
 
@@ -81,12 +112,15 @@ module Ionian
81
112
 
82
113
  @host = host_port_ary[0]
83
114
  @port = kwargs.fetch :port, host_port_ary[1].to_i || 23
84
- @bind_port = kwargs.fetch :bind_port, @port
115
+ @bind_port = kwargs.fetch :bind_port, @port
116
+
117
+ @broadcast = kwargs.fetch :broadcast, false
85
118
 
86
119
  # Automatically select UDP for the multicast range. Otherwise default to TCP.
87
120
  default_protocol = :tcp
88
121
  default_protocol = :udp if Ionian::Extension::Socket.multicast? @host
89
122
  default_protocol = :unix if @host.start_with? '/'
123
+ default_protocol = :udp if @broadcast
90
124
 
91
125
  @protocol = kwargs.fetch :protocol, default_protocol
92
126
  @persistent = kwargs.fetch :persistent, true
@@ -107,6 +141,14 @@ module Ionian
107
141
 
108
142
  create_socket if @persistent
109
143
  end
144
+
145
+ if block
146
+ block.call self
147
+ unless self.closed?
148
+ self.flush
149
+ self.close
150
+ end
151
+ end
110
152
  end
111
153
 
112
154
  # Returns the regular expression used to match incoming data.
@@ -126,13 +168,18 @@ module Ionian
126
168
  end
127
169
 
128
170
  # Send a command (data) to the socket.
129
- # Returns an array of received matches.
130
- # Block yields received match.
131
- # See Ionian::Extension::IO#read_match.
132
- def cmd string, **kwargs, &block
171
+ #
172
+ # @param [Hash] kwargs Pass through to {Ionian::Extension::IO#read_match}.
173
+ #
174
+ # @return [Array<MatchData>] An array of received matches.
175
+ #
176
+ # @yieldparam match [MatchData] Received match.
177
+ #
178
+ # @see Ionian::Extension::IO#read_match
179
+ def cmd data, **kwargs, &block
133
180
  create_socket unless @persistent
134
181
 
135
- write string
182
+ write data
136
183
  @socket.flush
137
184
 
138
185
  matches = @socket.read_match(kwargs) { |match| yield match if block_given? }
@@ -241,7 +288,11 @@ module Ionian
241
288
  @socket.extend Ionian::Extension::Socket
242
289
 
243
290
  @socket.reuse_addr = true if
244
- @reuse_addr or Ionian::Extension::Socket.multicast? @host
291
+ @reuse_addr or
292
+ @broadcast or
293
+ Ionian::Extension::Socket.multicast? @host
294
+
295
+ @socket.broadcast = true if @broadcast
245
296
 
246
297
  @socket.bind ::Socket::INADDR_ANY, @bind_port
247
298
  @socket.connect @host, @port
@@ -254,7 +305,8 @@ module Ionian
254
305
 
255
306
  end
256
307
 
257
- @socket.linger = @linger
308
+ # Windows complains at SO_LINGER, so only set it if it was specified.
309
+ @socket.linger = @linger if @linger
258
310
 
259
311
  @socket.expression = @expression if @expression
260
312
 
data/license.txt CHANGED
@@ -1,6 +1,6 @@
1
1
  The MIT License (MIT)
2
2
 
3
- Copyright (c) 2013 Alex McLain
3
+ Copyright (c) 2013-2014 Alex McLain
4
4
 
5
5
  Permission is hereby granted, free of charge, to any person obtaining a copy
6
6
  of this software and associated documentation files (the "Software"), to deal
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ionian
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.6.8
4
+ version: 0.6.9
5
5
  platform: ruby
6
6
  authors:
7
7
  - Alex McLain
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-04-14 00:00:00.000000000 Z
11
+ date: 2014-10-31 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rake
@@ -53,7 +53,7 @@ dependencies:
53
53
  - !ruby/object:Gem::Version
54
54
  version: '0'
55
55
  - !ruby/object:Gem::Dependency
56
- name: rspec
56
+ name: rb-readline
57
57
  requirement: !ruby/object:Gem::Requirement
58
58
  requirements:
59
59
  - - ">="
@@ -66,6 +66,34 @@ dependencies:
66
66
  - - ">="
67
67
  - !ruby/object:Gem::Version
68
68
  version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: rspec
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: 3.1.0
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: 3.1.0
83
+ - !ruby/object:Gem::Dependency
84
+ name: rspec-its
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - "~>"
88
+ - !ruby/object:Gem::Version
89
+ version: 1.0.1
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - "~>"
95
+ - !ruby/object:Gem::Version
96
+ version: 1.0.1
69
97
  - !ruby/object:Gem::Dependency
70
98
  name: fivemat
71
99
  requirement: !ruby/object:Gem::Requirement
@@ -128,7 +156,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
128
156
  requirements:
129
157
  - - ">="
130
158
  - !ruby/object:Gem::Version
131
- version: '0'
159
+ version: 2.0.0
132
160
  required_rubygems_version: !ruby/object:Gem::Requirement
133
161
  requirements:
134
162
  - - ">="