tiny_tcp_service 1.2.1 → 1.3.0
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.
- checksums.yaml +4 -4
- data/lib/tiny_tcp_service.rb +37 -43
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: df345d1a0441ab1451364e2d6c60dcabae1539387f8d32c0a8f6c7a506bc43a4
|
4
|
+
data.tar.gz: be6dacf5fe1d224280f73b0b4529ae5cdea4d15b53e09a29d2effbf430e69e71
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: bd0fb1ee8872b784205f29e3a16a5a10764b11ea0a33646db31ac60da8bffd32c2070c24087abfedf746905a23e2c8626a36cdb7a755f9fd65d686325f07af96
|
7
|
+
data.tar.gz: 3c5cb2735db8c26e076aa2cf07893d199047e5f39a18bb0932ec6e884e351d4fc0739afd3b847f56f8003acb1b328df53990967ef82bdb071099630020dc82a9
|
data/lib/tiny_tcp_service.rb
CHANGED
@@ -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 =
|
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.
|
94
|
-
|
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
|