Skip to content

Commit a8b80cd

Browse files
committed
simplify clientsocket lib
1 parent d41c9db commit a8b80cd

File tree

6 files changed

+171
-207
lines changed

6 files changed

+171
-207
lines changed

HISTORY.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ Dev version
22
-----------
33
* skynet.exit will quit service immediately.
44
* Add snax.gateserver, snax.loginserver, snax.msgserver
5+
* Simplify clientsocket lib
56

67
v0.4.2 (2014-7-14)
78
-----------

examples/client.lua

Lines changed: 52 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -2,43 +2,73 @@ package.cpath = "luaclib/?.so"
22

33
local socket = require "clientsocket"
44
local cjson = require "cjson"
5+
local bit32 = require "bit32"
56

6-
local fd = socket.connect("127.0.0.1", 8888)
7+
local fd = assert(socket.connect("127.0.0.1", 8888))
78

8-
local last
9-
local result = {}
9+
local function send_package(fd, pack)
10+
local size = #pack
11+
local package = string.format("%c%c%s",
12+
bit32.extract(size,8,8),
13+
bit32.extract(size,0,8),
14+
pack)
1015

11-
local function dispatch()
12-
while true do
13-
local status
14-
status, last = socket.recv(fd, last, result)
15-
if status == nil then
16-
error "Server closed"
17-
end
18-
if not status then
19-
break
20-
end
21-
for _, v in ipairs(result) do
22-
local session,t,str = string.match(v, "(%d+)(.)(.*)")
23-
assert(t == '-' or t == '+')
24-
session = tonumber(session)
25-
local result = cjson.decode(str)
26-
print("Response:",session, result[1], result[2])
27-
end
16+
socket.send(fd, package)
17+
end
18+
19+
local function unpack_package(text)
20+
local size = #text
21+
if size < 2 then
22+
return nil, text
23+
end
24+
local s = text:byte(1) * 256 + text:byte(2)
25+
if size < s+2 then
26+
return nil, text
2827
end
28+
29+
return text:sub(3,2+s), text:sub(3+s)
30+
end
31+
32+
local function recv_package(last)
33+
local result
34+
result, last = unpack_package(last)
35+
if result then
36+
return result, last
37+
end
38+
local r = socket.recv(fd)
39+
if not r then
40+
return nil, last
41+
end
42+
if r == "" then
43+
error "Server closed"
44+
end
45+
return unpack_package(last .. r)
2946
end
3047

3148
local session = 0
3249

3350
local function send_request(v)
3451
session = session + 1
3552
local str = string.format("%d+%s",session, cjson.encode(v))
36-
socket.send(fd, str)
53+
send_package(fd, str)
3754
print("Request:", session)
3855
end
3956

57+
local last = ""
58+
4059
while true do
41-
dispatch()
60+
while true do
61+
local v
62+
v, last = recv_package(last)
63+
if not v then
64+
break
65+
end
66+
local session,t,str = string.match(v, "(%d+)(.)(.*)")
67+
assert(t == '-' or t == '+')
68+
session = tonumber(session)
69+
local result = cjson.decode(str)
70+
print("Response:",session, result[1], result[2])
71+
end
4272
local cmd = socket.readstdin()
4373
if cmd then
4474
local args = {}

examples/login/client.lua

Lines changed: 78 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -2,44 +2,65 @@ package.cpath = "luaclib/?.so"
22

33
local socket = require "clientsocket"
44
local crypt = require "crypt"
5+
local bit32 = require "bit32"
56

6-
local last
77
local fd = assert(socket.connect("127.0.0.1", 8001))
8-
local input = {}
98

10-
local function readline()
11-
local line = table.remove(input, 1)
12-
if line then
13-
return line
9+
local function writeline(fd, text)
10+
socket.send(fd, text .. "\n")
11+
end
12+
13+
local function unpack_line(text)
14+
local from = text:find("\n", 1, true)
15+
if from then
16+
return text:sub(1, from-1), text:sub(from+1)
1417
end
18+
return nil, text
19+
end
1520

16-
while true do
17-
local status
18-
status, last = socket.readline(fd, last, input)
19-
if status == nil then
21+
local last = ""
22+
23+
local function unpack_f(f)
24+
local function try_recv(fd, last)
25+
local result
26+
result, last = f(last)
27+
if result then
28+
return result, last
29+
end
30+
local r = socket.recv(fd)
31+
if not r then
32+
return nil, last
33+
end
34+
if r == "" then
2035
error "Server closed"
2136
end
22-
if not status then
23-
socket.usleep(100)
24-
else
25-
local line = table.remove(input, 1)
26-
if line then
27-
return line
37+
return f(last .. r)
38+
end
39+
40+
return function()
41+
while true do
42+
local result
43+
result, last = try_recv(fd, last)
44+
if result then
45+
return result
2846
end
47+
socket.usleep(100)
2948
end
3049
end
3150
end
3251

52+
local readline = unpack_f(unpack_line)
53+
3354
local challenge = crypt.base64decode(readline())
3455

3556
local clientkey = crypt.randomkey()
36-
socket.writeline(fd, crypt.base64encode(crypt.dhexchange(clientkey)))
57+
writeline(fd, crypt.base64encode(crypt.dhexchange(clientkey)))
3758
local secret = crypt.dhsecret(crypt.base64decode(readline()), clientkey)
3859

3960
print("sceret is ", crypt.hexencode(secret))
4061

4162
local hmac = crypt.hmac64(challenge, secret)
42-
socket.writeline(fd, crypt.base64encode(hmac))
63+
writeline(fd, crypt.base64encode(hmac))
4364

4465
local token = {
4566
server = "sample",
@@ -56,7 +77,7 @@ end
5677

5778
local etoken = crypt.desencode(secret, encode_token(token))
5879
local b = crypt.base64encode(etoken)
59-
socket.writeline(fd, crypt.base64encode(etoken))
80+
writeline(fd, crypt.base64encode(etoken))
6081

6182
local result = readline()
6283
print(result)
@@ -71,8 +92,18 @@ print("login ok, subid=", subid)
7192
----- connect to game server
7293

7394
local function send_request(v, session)
74-
local s = string.char(bit32.extract(session,24,8), bit32.extract(session,16,8), bit32.extract(session,8,8), bit32.extract(session,0,8))
75-
socket.send(fd , v..s)
95+
local size = #v + 4
96+
local package = string.format("%c%c%s%c%c%c%c",
97+
bit32.extract(size,8,8),
98+
bit32.extract(size,0,8),
99+
v,
100+
bit32.extract(session,24,8),
101+
bit32.extract(session,16,8),
102+
bit32.extract(session,8,8),
103+
bit32.extract(session,0,8)
104+
)
105+
106+
socket.send(fd, package)
76107
return v, session
77108
end
78109

@@ -87,42 +118,43 @@ local function recv_response(v)
87118
return ok ~=0 , content, session
88119
end
89120

90-
local input = {}
91-
92-
local function readpackage()
93-
local line = table.remove(input, 1)
94-
if line then
95-
return line
121+
local function unpack_package(text)
122+
local size = #text
123+
if size < 2 then
124+
return nil, text
96125
end
97-
98-
while true do
99-
local status
100-
status, last = socket.recv(fd, last, input)
101-
if status == nil then
102-
error "Server closed"
103-
end
104-
if not status then
105-
socket.usleep(100)
106-
else
107-
local line = table.remove(input, 1)
108-
if line then
109-
return line
110-
end
111-
end
126+
local s = text:byte(1) * 256 + text:byte(2)
127+
if size < s+2 then
128+
return nil, text
112129
end
130+
131+
return text:sub(3,2+s), text:sub(3+s)
132+
end
133+
134+
local readpackage = unpack_f(unpack_package)
135+
136+
local function send_package(fd, pack)
137+
local size = #pack
138+
local package = string.format("%c%c%s",
139+
bit32.extract(size,8,8),
140+
bit32.extract(size,0,8),
141+
pack)
142+
143+
socket.send(fd, package)
113144
end
114145

115146
local text = "echo"
116147
local index = 1
117148

118149
print("connect")
119150
local fd = assert(socket.connect("127.0.0.1", 8888))
120-
input = {}
151+
last = ""
121152

122153
local handshake = string.format("%s@%s#%s:%d", crypt.base64encode(token.user), crypt.base64encode(token.server),crypt.base64encode(subid) , index)
123154
local hmac = crypt.hmac64(crypt.hashkey(handshake), secret)
124155

125-
socket.send(fd, handshake .. ":" .. crypt.base64encode(hmac))
156+
157+
send_package(fd, handshake .. ":" .. crypt.base64encode(hmac))
126158

127159
print(readpackage())
128160
print("===>",send_request(text,0))
@@ -136,12 +168,12 @@ index = index + 1
136168

137169
print("connect again")
138170
local fd = assert(socket.connect("127.0.0.1", 8888))
139-
input = {}
171+
last = ""
140172

141173
local handshake = string.format("%s@%s#%s:%d", crypt.base64encode(token.user), crypt.base64encode(token.server),crypt.base64encode(subid) , index)
142174
local hmac = crypt.hmac64(crypt.hashkey(handshake), secret)
143175

144-
socket.send(fd, handshake .. ":" .. crypt.base64encode(hmac))
176+
send_package(fd, handshake .. ":" .. crypt.base64encode(hmac))
145177

146178
print(readpackage())
147179
print("===>",send_request("fake",0)) -- request again (use last session 0, so the request message is fake)

examples/simpledb.lua

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,11 @@ end
1616
skynet.start(function()
1717
skynet.dispatch("lua", function(session, address, cmd, ...)
1818
local f = command[string.upper(cmd)]
19-
skynet.ret(skynet.pack(f(...)))
19+
if f then
20+
skynet.ret(skynet.pack(f(...)))
21+
else
22+
error(string.format("Unknown command %s", tostring(cmd)))
23+
end
2024
end)
2125
skynet.register "SIMPLEDB"
2226
end)

0 commit comments

Comments
 (0)