rakie 0.0.5 → 0.0.10

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 754086391c9ec9e2eb623d8d6a1ead9ca9188d49fb0c391b26c4a9e36db88236
4
- data.tar.gz: 6ac34e0868c8bc1748e7f572266b4223a91f676d9d7c11b22fa5a7e10586713c
3
+ metadata.gz: 196e26c710b29918f06ee04e5303caf7f08a1ab989cd6614e36cb469dd3e24b3
4
+ data.tar.gz: 15d0459b668f44c3fe8e9b323a24f8f1c19c647e29ec7a7cd9477615063a6961
5
5
  SHA512:
6
- metadata.gz: 839acbd5155b745948a6144a37480b770e50f01ff4daf29146e39c8fb2e36cf0f434f10313577d3f7b1efcfd039317b036d9c87f96d6c57d09a25c9da6ba5e10
7
- data.tar.gz: a844de019fa10103c96e7cc7ab4f7e78ffb2cd5a7c23b28be6483c35fbb7a5cfd1b51f4a1996fa053fa33e03d701af52682c0af306d92b3f2c2c98878aa7c4c6
6
+ metadata.gz: 8f1ac0ed83237b1c57c3c1a58d4b9415602804d92d035ac89238435927efa7f8eb7c29166f0e174bb91bf92b47d63ec47a159c6b8a427b2a280509908acb06cb
7
+ data.tar.gz: 6ebde9c958b93c395b88c30c6ac53eb8418eeb8c9571043d66364b508df148d97456a5b10948993b4b42422324618f1568632287e2e0cf3468d37e53a1b2799a
data/lib/rakie/channel.rb CHANGED
@@ -20,7 +20,10 @@ module Rakie
20
20
  end
21
21
 
22
22
  rescue IO::EAGAINWaitReadable
23
- Log.debug("Channel read finished")
23
+ Log.debug("Channel read pending")
24
+
25
+ rescue IO::EWOULDBLOCKWaitReadable
26
+ Log.debug("Channel read pending")
24
27
 
25
28
  rescue Exception => e
26
29
  # Process the last message on exception
@@ -67,19 +70,27 @@ module Rakie
67
70
 
68
71
  def on_write(io)
69
72
  len = 0
73
+ offset = 0
70
74
 
71
75
  begin
72
76
  while @write_buffer.length > 0
73
77
  len = io.write_nonblock(@write_buffer)
78
+ offset += len
74
79
  @write_buffer = @write_buffer[len .. -1]
75
80
  end
76
81
 
77
82
  Log.debug("Channel write finished")
78
83
 
79
84
  rescue IO::EAGAINWaitWritable
80
- self.handle_write(len)
85
+ self.handle_write(offset)
81
86
 
82
- Log.debug("Channel write continue")
87
+ Log.debug("Channel write pending")
88
+ return Event::HANDLE_CONTINUED
89
+
90
+ rescue IO::EWOULDBLOCKWaitWritable
91
+ self.handle_write(offset)
92
+
93
+ Log.debug("Channel write pending")
83
94
  return Event::HANDLE_CONTINUED
84
95
 
85
96
  rescue
@@ -87,21 +98,26 @@ module Rakie
87
98
  return Event::HANDLE_FAILED
88
99
  end
89
100
 
90
- self.handle_write(len)
101
+ self.handle_write(offset)
91
102
 
92
103
  return Event::HANDLE_FINISHED
93
104
  end
94
105
 
95
106
  def on_detach(io)
107
+ if io.closed?
108
+ return
109
+ end
110
+
96
111
  begin
97
112
  io.close
98
113
 
99
- if @delegate
100
- @delegate.on_close(self)
101
- end
102
-
103
114
  rescue
104
115
  Log.debug("Channel is already closed")
116
+ return
117
+ end
118
+
119
+ if @delegate
120
+ @delegate.on_close(self)
105
121
  end
106
122
 
107
123
  Log.debug("Channel close ok")
@@ -13,10 +13,13 @@ module Rakie
13
13
  end
14
14
  end
15
15
 
16
- attr_reader :channel, :opt
16
+ attr_reader :channel, :opt, :host, :port
17
17
 
18
- def initialize(delegate=nil)
19
- @channel = TCPServerChannel.new('127.0.0.1', 10086, self)
18
+ def initialize(host: '127.0.0.1', port: 10086, delegate: nil)
19
+ @host = host
20
+ @port = port
21
+
22
+ @channel = TCPServerChannel.new(host, port, self)
20
23
 
21
24
  # @type [Hash{Channel=>Session}]
22
25
  @sessions = {}
@@ -123,5 +126,9 @@ module Rakie
123
126
  end
124
127
  end
125
128
  end
129
+
130
+ def on_close(channel)
131
+ @sessions.delete(channel)
132
+ end
126
133
  end
127
134
  end
data/lib/rakie/version.rb CHANGED
@@ -1,5 +1,5 @@
1
1
  module Rakie
2
- VERSION = [0, 0, 5]
2
+ VERSION = [0, 0, 10]
3
3
 
4
4
  def self.version_s
5
5
  VERSION.join('.')
@@ -21,58 +21,75 @@ module Rakie
21
21
  @client_side = true
22
22
  end
23
23
 
24
+ # @param [String] data
24
25
  def on_recv(channel, data)
25
- # Log.debug("Rakie::HTTPServer recv: #{data}")
26
+ Log.debug("Rakie::Websocket recv: #{data}")
26
27
 
27
- # @type [WebsocketMessage] request
28
- message = @recv_message
28
+ total_parsed = 0
29
29
 
30
- if message.parse_status == ParseStatus::COMPLETE
31
- message = WebsocketMessage.new
32
- @recv_message = message
33
- end
30
+ while data.length > 0
31
+ # @type [WebsocketMessage] request
32
+ message = @recv_message
34
33
 
35
- len = message.parse(data)
34
+ if message.parse_status == ParseStatus::COMPLETE
35
+ message = WebsocketMessage.new
36
+ @recv_message = message
37
+ end
36
38
 
37
- Log.debug("Rakie::Websocket receive message: #{message.to_s}")
39
+ len = message.parse(data)
40
+ total_parsed += len
38
41
 
39
- if message.parse_status == ParseStatus::COMPLETE
40
- response = WebsocketMessage.new
42
+ Log.debug("Rakie::Websocket receive message: #{message.to_s} parse with #{len}")
41
43
 
42
- if message.op_code == WebsocketMessage::OP_PING
43
- response.fin = true
44
- response.op_code = WebsocketMessage::OP_PONG
45
- response.payload = "pong"
44
+ if message.parse_status == ParseStatus::COMPLETE
45
+ response = WebsocketMessage.new
46
46
 
47
- elsif message.op_code == WebsocketMessage::OP_PONG
48
- response.fin = true
49
- response.op_code = WebsocketMessage::OP_PING
50
- response.payload = "ping"
47
+ if message.op_code == WebsocketMessage::OP_PING
48
+ response.fin = true
49
+ response.op_code = WebsocketMessage::OP_PONG
50
+ response.payload = "Rakie::Websocket: op pong"
51
51
 
52
- elsif @delegate
53
- @delegate.on_message(self, message.payload)
54
- return len
52
+ elsif message.op_code == WebsocketMessage::OP_PONG
53
+ response.fin = true
54
+ response.op_code = WebsocketMessage::OP_PING
55
+ response.payload = "Rakie::Websocket: op ping"
55
56
 
56
- else
57
- response.fin = true
58
- response.op_code = WebsocketMessage::OP_TEXT
59
- response.payload = "Rakie!"
60
- end
61
-
62
- response_data = response.to_s
57
+ elsif message.op_code == WebsocketMessage::OP_CLOSE
58
+ channel.close
59
+
60
+ Log.debug("Rakie::Websocket: op close")
61
+ return 0
62
+
63
+ elsif @delegate
64
+ @delegate.on_message(self, message.payload)
65
+
66
+ else
67
+ response.fin = true
68
+ response.op_code = WebsocketMessage::OP_TEXT
69
+ response.payload = "Rakie!"
70
+ end
71
+
72
+ response_data = response.to_s
63
73
 
64
- Log.debug("Rakie::Websocket response: #{response_data}")
74
+ Log.debug("Rakie::Websocket response: #{response_data}")
65
75
 
66
- channel.write(response_data) # Response data
76
+ channel.write(response_data) # Response data
67
77
 
68
- elsif message.parse_status == ParseStatus::ERROR
69
- channel.close
78
+ elsif message.parse_status == ParseStatus::ERROR
79
+ channel.close
80
+
81
+ Log.debug("Rakie::Websocket: Illegal message")
82
+ return 0
83
+ end
84
+
85
+ if len >= data.length
86
+ break
87
+ end
70
88
 
71
- Log.debug("Rakie::Websocket: Illegal message")
72
- return len
89
+ data = data[len .. -1]
73
90
  end
74
91
 
75
- return len
92
+ return total_parsed
76
93
  end
77
94
 
78
95
  def on_send(channel)
@@ -109,6 +126,7 @@ module Rakie
109
126
  end
110
127
 
111
128
  send_message = ws_message.to_s
129
+ @send_messages << ws_message
112
130
 
113
131
  Log.debug("Rakie::Websocket send: #{send_message}")
114
132
 
@@ -134,4 +152,4 @@ module Rakie
134
152
  @channel.write(send_message) # Response data
135
153
  end
136
154
  end
137
- end
155
+ end
@@ -1,13 +1,16 @@
1
1
  module Rakie
2
2
  class WebsocketServer < Websocket
3
3
  # @param [Rakie::HttpServer] http_server
4
- def initialize(delegate=nil, http_server=nil)
4
+ def initialize(host: '127.0.0.1', port: 10086, delegate: nil, http_server: nil)
5
5
  @delegate = delegate
6
6
 
7
7
  if http_server == nil
8
- http_server = HttpServer.new('127.0.0.1', 10086, self)
8
+ http_server = HttpServer.new(host: host, port: port)
9
9
  end
10
10
 
11
+ @host = http_server.host
12
+ @port = http_server.port
13
+
11
14
  http_server.opt[:websocket_delegate] = self
12
15
  @channel = http_server.channel
13
16
 
@@ -23,7 +26,6 @@ module Rakie
23
26
 
24
27
  if @delegate
25
28
  @delegate.on_connect(ws_client)
26
- return
27
29
  end
28
30
 
29
31
  @clients[channel] = ws_client
@@ -49,8 +51,10 @@ module Rakie
49
51
  client = @clients[channel]
50
52
 
51
53
  if client
52
- client.on_recv(channel, data)
54
+ return client.on_recv(channel, data)
53
55
  end
56
+
57
+ return data.length
54
58
  end
55
59
 
56
60
  def on_send(channel)
@@ -67,6 +71,8 @@ module Rakie
67
71
  if client
68
72
  client.on_close(channel)
69
73
  end
74
+
75
+ @clients.delete(channel)
70
76
  end
71
77
 
72
78
  def send(message, is_binary=false); end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rakie
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.5
4
+ version: 0.0.10
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jakit Liang
@@ -43,9 +43,9 @@ require_paths:
43
43
  - lib
44
44
  required_ruby_version: !ruby/object:Gem::Requirement
45
45
  requirements:
46
- - - ">="
46
+ - - ">"
47
47
  - !ruby/object:Gem::Version
48
- version: '0'
48
+ version: '2.5'
49
49
  required_rubygems_version: !ruby/object:Gem::Requirement
50
50
  requirements:
51
51
  - - ">="