Repository navigation
Expand file tree
/
Copy pathmessage.v
More file actions
88 lines (75 loc) · 1.65 KB
/
Copy pathmessage.v
File metadata and controls
88 lines (75 loc) · 1.65 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
module cdv
import x.json2 as json
pub enum MessageType {
command
event
}
@[params]
pub struct ParamRef {
pub:
ref voidptr
}
@[params]
pub struct ParamTimeout {
pub:
timeout i64 = def_timeout
}
@[params]
pub struct ParamRefTimeout {
pub:
timeout i64 = def_timeout
ref voidptr
}
pub type EventFunc = fn (mut msg Message, ref voidptr) !bool
@[params]
pub struct MessageParams {
pub:
id int = -1
method string
params ?map[string]json.Any
session_id ?string
timeout ?i64
typ MessageType = .command
cb EventFunc = unsafe { nil }
ref voidptr
}
pub struct Message {
pub:
method string
session_id string
pub mut:
params map[string]json.Any
page &Page = unsafe { nil }
}
pub fn (msg Message) has(m string) bool {
return m != '' && (m == msg.method || map_method[m] == msg.method)
}
pub fn (msg Message) done() bool {
return cdv_msg_done
}
pub fn (msg Message) next() bool {
return cdv_msg_next
}
pub fn (mut msg Message) get_request() !Request {
params := msg.params.clone()
if request := params['request'] {
mut info := json.decode[RequestInfo](params.str())!
mut req := json.decode[Request](request.json_str())!
req.has_post_data = req.has_post_data_ or { false }
req.info = &info
req.page = msg.page
return req
}
return error('cannot find "params.request"')
}
pub fn (mut msg Message) get_response() !Response {
params := msg.params.clone()
if response := params['response'] {
mut info := json.decode[ResponseInfo](params.str())!
mut res := json.decode[Response](response.json_str())!
res.info = &info
res.page = msg.page
return res
}
return error('cannot find "params.response"')
}