tiny_tcp_service 1.2.1 → 1.3.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (3) hide show
  1. checksums.yaml +4 -4
  2. data/lib/tiny_tcp_service.rb +37 -43
  3. metadata +1 -1
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 705433100d134e1b65ea922d21ced51f6429570bdc86aeaba322765c6e4d0cf3
4
- data.tar.gz: 3ab6c3c0510341c0e3bb26180a6baf0576570c0e96b1f6b12cf0ab4c38357521
3
+ metadata.gz: df345d1a0441ab1451364e2d6c60dcabae1539387f8d32c0a8f6c7a506bc43a4
4
+ data.tar.gz: be6dacf5fe1d224280f73b0b4529ae5cdea4d15b53e09a29d2effbf430e69e71
5
5
  SHA512:
6
- metadata.gz: 35ca44ed886b5d9edca59455d6a88cc8f43904c0a3c139596d5656f47f0d431dcc621f1329d178cea0be775f2caf19f9fe7db5b850edc99ae4d9860a3c5d01b9
7
- data.tar.gz: 8b862c075f947a468ce8c42bc6e2fe0de03eb7e37011bed1da0de6bb2af4ece354a6a37ee6aaa09b5af72380b96e56c9093efaffa8d588e471c97cfac8efb209
6
+ metadata.gz: bd0fb1ee8872b784205f29e3a16a5a10764b11ea0a33646db31ac60da8bffd32c2070c24087abfedf746905a23e2c8626a36cdb7a755f9fd65d686325f07af96
7
+ data.tar.gz: 3c5cb2735db8c26e076aa2cf07893d199047e5f39a18bb0932ec6e884e351d4fc0739afd3b847f56f8003acb1b328df53990967ef82bdb071099630020dc82a9
@@ -6,7 +6,6 @@ require 'socket'
6
6
  # ->(m) { puts m }
7
7
  # )
8
8
  #
9
- # s.start! # everything runs in background threads
10
9
  # s.stop! # gracefully shutdown the server
11
10
  #
12
11
  # TinyTCPService implements a line-based, call and response protocol, where
@@ -27,49 +26,10 @@ class TinyTCPService
27
26
 
28
27
  @server = TCPServer.new(port)
29
28
  @clients = []
30
- @running = false
29
+ @running = true
31
30
 
32
31
  @msg_handler = nil
33
32
  @error_handlers = {}
34
- end
35
-
36
- # h - some object that responds to #call
37
- def msg_handler=(h)
38
- @msg_handler = h
39
- end
40
-
41
- # returns true if the server is running
42
- # false otherwise
43
- def running?
44
- @running
45
- end
46
-
47
- # add the error handler and block for the specified class
48
- #
49
- # you can assume that the local variable name of the error will be `e'
50
- def add_error_handler(klass, block)
51
- @error_handlers[klass] = block
52
- end
53
-
54
- # remove the error handler associated with klass
55
- def remove_error_handler(klass)
56
- @error_handlers.delete(klass)
57
- end
58
-
59
- # returns the number of connected clients
60
- def num_clients
61
- @clients.length
62
- end
63
-
64
- def _remove_client!(c)
65
- @clients.delete(c)
66
- c.close if c && !c.closed?
67
- end
68
-
69
- # starts the server
70
- def start!
71
- return if running?
72
- @running = true
73
33
 
74
34
  # client accept thread
75
35
  Thread.new do |t|
@@ -90,8 +50,9 @@ class TinyTCPService
90
50
  readable, _, errored = IO.select(@clients, nil, @clients, 1)
91
51
  readable&.each do |c|
92
52
  begin
93
- c.puts(@msg_handler&.call(c.gets.chomp))
94
- rescue TinyTCPService::BadClient => e
53
+ m = c.gets&.chomp
54
+ c.puts(@msg_handler&.call(m) || 'ok')
55
+ rescue TinyTCPService::BadClient, Errno::ECONNRESET => e
95
56
  _remove_client!(c)
96
57
  rescue => e
97
58
  handler = @error_handlers[e.class]
@@ -112,6 +73,39 @@ class TinyTCPService
112
73
  end
113
74
  end
114
75
 
76
+ # h - some object that responds to #call
77
+ def msg_handler=(h)
78
+ @msg_handler = h
79
+ end
80
+
81
+ # returns true if the server is running
82
+ # false otherwise
83
+ def running?
84
+ @running
85
+ end
86
+
87
+ # add the error handler and block for the specified class
88
+ #
89
+ # you can assume that the local variable name of the error will be `e'
90
+ def add_error_handler(klass, block)
91
+ @error_handlers[klass] = block
92
+ end
93
+
94
+ # remove the error handler associated with klass
95
+ def remove_error_handler(klass)
96
+ @error_handlers.delete(klass)
97
+ end
98
+
99
+ # returns the number of connected clients
100
+ def num_clients
101
+ @clients.length
102
+ end
103
+
104
+ def _remove_client!(c)
105
+ @clients.delete(c)
106
+ c.close if c && !c.closed?
107
+ end
108
+
115
109
  # stops the server gracefully
116
110
  def stop!
117
111
  @running = false
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: tiny_tcp_service
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.2.1
4
+ version: 1.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jeff Lunt