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 +4 -4
- data/lib/rakie/channel.rb +24 -8
- data/lib/rakie/http_server.rb +10 -3
- data/lib/rakie/version.rb +1 -1
- data/lib/rakie/websocket.rb +55 -37
- data/lib/rakie/websocket_server.rb +10 -4
- metadata +3 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 196e26c710b29918f06ee04e5303caf7f08a1ab989cd6614e36cb469dd3e24b3
|
4
|
+
data.tar.gz: 15d0459b668f44c3fe8e9b323a24f8f1c19c647e29ec7a7cd9477615063a6961
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
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
|
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(
|
85
|
+
self.handle_write(offset)
|
81
86
|
|
82
|
-
Log.debug("Channel write
|
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(
|
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")
|
data/lib/rakie/http_server.rb
CHANGED
@@ -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
|
19
|
-
@
|
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
data/lib/rakie/websocket.rb
CHANGED
@@ -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
|
-
|
26
|
+
Log.debug("Rakie::Websocket recv: #{data}")
|
26
27
|
|
27
|
-
|
28
|
-
message = @recv_message
|
28
|
+
total_parsed = 0
|
29
29
|
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
end
|
30
|
+
while data.length > 0
|
31
|
+
# @type [WebsocketMessage] request
|
32
|
+
message = @recv_message
|
34
33
|
|
35
|
-
|
34
|
+
if message.parse_status == ParseStatus::COMPLETE
|
35
|
+
message = WebsocketMessage.new
|
36
|
+
@recv_message = message
|
37
|
+
end
|
36
38
|
|
37
|
-
|
39
|
+
len = message.parse(data)
|
40
|
+
total_parsed += len
|
38
41
|
|
39
|
-
|
40
|
-
response = WebsocketMessage.new
|
42
|
+
Log.debug("Rakie::Websocket receive message: #{message.to_s} parse with #{len}")
|
41
43
|
|
42
|
-
if message.
|
43
|
-
response
|
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
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
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
|
-
|
53
|
-
|
54
|
-
|
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
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
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
|
-
|
74
|
+
Log.debug("Rakie::Websocket response: #{response_data}")
|
65
75
|
|
66
|
-
|
76
|
+
channel.write(response_data) # Response data
|
67
77
|
|
68
|
-
|
69
|
-
|
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
|
-
|
72
|
-
return len
|
89
|
+
data = data[len .. -1]
|
73
90
|
end
|
74
91
|
|
75
|
-
return
|
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
|
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(
|
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.
|
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: '
|
48
|
+
version: '2.5'
|
49
49
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
50
50
|
requirements:
|
51
51
|
- - ">="
|