json-socket 1.0.4 → 2.0.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/json-socket.rb +21 -25
  3. metadata +18 -4
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: c5b25be9f8edfe3dadee684bfc30dcccc5195349a423f39c21d7dc89e88d876a
4
- data.tar.gz: 12d01e5b7587929b7d1756ee54ab599eb03a2cefca37091d44f9b6b6a1eed1ec
3
+ metadata.gz: 875091899544dfd73d4014d91052776f04ef92c62acd78d233701baf82a2f01c
4
+ data.tar.gz: db3aedc20dabe6e640d83fe682cde75dbde60ddc601eda29269099e6f2fca52f
5
5
  SHA512:
6
- metadata.gz: 8c6a5697b322807ed6676ca82e355705d455b83fd488fb0b3d57754c04c3d9eab4f90641fcc79d30b6e99e5f15738a99ad0f42c3e8c63cc9cc31f3a4d2c4fe74
7
- data.tar.gz: 895a212c1c6a5ad27b7ba44676a2da45dd36e5c3d7d306413df10e823d27d841614dd9a45f830676ef0ae348f5984397842f0d1ff87d6722fa172790746a4534
6
+ metadata.gz: bc524ff94905b6f97ab70d6bbc6c0ee067737cededacb4eec6c2bfc5c2dd74ed76d2455fcc494c2abaa3c8ef2f4b74016941893e2f747715d3d3646f25b1d67e
7
+ data.tar.gz: 7a56e89c0320e7b494dafee28037567214b4663168d82eb08536e8c90c74a15074a35958d939b9cfe100b11c7f83e544eaf35287594ce891b2e2a7d2120679f4
data/lib/json-socket.rb CHANGED
@@ -1,6 +1,8 @@
1
- require "socket"
1
+ # require "socket"
2
2
  require "fileutils"
3
3
  require "oj"
4
+ require "async/io"
5
+ require 'async/io/unix_endpoint'
4
6
 
5
7
  Oj.default_options = { :mode => :strict }
6
8
 
@@ -25,32 +27,30 @@ module JSONSocket
25
27
  def initialize(host: "127.0.0.1", port: 1234, delimeter: "#", unix_socket: nil, oj_options: nil)
26
28
  Oj.default_options = oj_options if oj_options
27
29
  @delimeter = delimeter
28
- @stop = false
29
30
  @server = if unix_socket
30
31
  FileUtils.rm_f(unix_socket)
31
- UNIXServer.new unix_socket
32
+ Async::IO::Endpoint.unix(unix_socket)
32
33
  else
33
- TCPServer.new host, port
34
+ Async::IO::Endpoint.tcp(host, port)
34
35
  end
35
36
  end
36
37
 
37
- def stop
38
- @stop = true
39
- end
40
-
41
38
  def listen
42
- loop do
43
- Thread.start(@server.accept) do |client|
44
- begin
45
- # https://stackoverflow.com/questions/25303943/how-to-send-a-utf-8-encoded-strings-via-tcpsocket-in-ruby/25305256
46
- client.set_encoding 'UTF-8'
47
- message_length = client.gets(@delimeter).to_i
39
+ Async do |task|
40
+ begin
41
+ @server.accept do |client|
42
+ client = client.set_encoding 'UTF-8'
43
+ # https://stackoverflow.com/questions/25303943/how-to-send-a-utf-8-encoded-strings-via-tcpsocket-in-ruby/25305256
44
+ message_length = client.gets(@delimeter).to_i
45
+ unless message_length == 0
48
46
  on_message(parse_json(client.read(message_length)), client)
49
- rescue Exception => e
50
- on_error e
47
+ else
48
+ client.close
51
49
  end
52
50
  end
53
- break if @stop
51
+ rescue Exception => e
52
+ on_error e
53
+ end
54
54
  end
55
55
  end
56
56
 
@@ -104,14 +104,10 @@ module JSONSocket
104
104
  end
105
105
 
106
106
  def send(message)
107
- begin
108
- if @unix_socket
109
- UNIXSocket.open(@unix_socket) {|socket| handle_send_receive(socket, message) }
110
- else
111
- TCPSocket.open(@host, @port) {|socket| handle_send_receive(socket, message) }
112
- end
113
- rescue Exception => e
114
- STDERR.puts e
107
+ if @unix_socket
108
+ UNIXSocket.open(@unix_socket) {|socket| handle_send_receive(socket, message) }
109
+ else
110
+ TCPSocket.open(@host, @port) {|socket| handle_send_receive(socket, message) }
115
111
  end
116
112
  end
117
113
 
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: json-socket
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.4
4
+ version: 2.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - foi
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2023-02-04 00:00:00.000000000 Z
11
+ date: 2023-02-07 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: oj
@@ -24,6 +24,20 @@ dependencies:
24
24
  - - ">="
25
25
  - !ruby/object:Gem::Version
26
26
  version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: async-io
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
27
41
  - !ruby/object:Gem::Dependency
28
42
  name: rspec
29
43
  requirement: !ruby/object:Gem::Requirement
@@ -38,8 +52,8 @@ dependencies:
38
52
  - - ">="
39
53
  - !ruby/object:Gem::Version
40
54
  version: '0'
41
- description: JSON-socket client & server implementation. Inspired by and compatible
42
- with sebastianseilund/node-json-socket
55
+ description: JSON-socket client & server implementation based on async-io. Inspired
56
+ by and compatible with sebastianseilund/node-json-socket
43
57
  email: foi@live.ru
44
58
  executables: []
45
59
  extensions: []