16
20
# [message_type::number, arg0::string, arg1::string, ...]
18
22
# import the message types
19
include(find_in_path("webrepl_msgtypes_h"))
23
include("webrepl_msgtypes_h.jl")
21
25
###########################################
22
26
# set up the socket connection
23
27
###########################################
25
29
# open a socket on any port
26
__ports = [int16(4444)]
27
__sockfd = ccall(:open_any_tcp_port, Int32, (Ptr{Int16},), __ports)
29
# couldn't open the socket
30
println("could not open server socket on port 4444.")
30
(port,sock) = Base.open_any_tcp_port(4444)
34
32
# print the socket number so the server knows what it is
33
println(STDOUT,int16(port))
37
35
# wait for the server to connect to the socket
38
__connectfd = ccall(:accept, Int32, (Int32, Ptr{Void}, Ptr{Void}), __sockfd, C_NULL, C_NULL)
40
# create an io object from the file descriptor
41
__io = fdio(__connectfd)
36
__io = Base.wait_accept(sock)
37
Base.start_reading(__io)
43
39
###########################################
44
40
# protocol implementation
59
56
arg_length = read(__io, Uint32)
60
57
arg = ASCIIString(read(__io, Uint8, arg_length))
63
60
return __Message(msg_type, args)
64
const __io_out_buf = PipeString()
67
65
function __write_message(msg)
66
if DEBUG show(msg); println() end
68
67
write(__io, uint8(msg.msg_type))
69
68
write(__io, uint8(length(msg.args)))
71
write(__io, uint32(length(arg)))
70
write(__io_out_buf, arg)
71
data = takebuf_array(__io_out_buf)
72
write(__io, uint32(length(data)))
77
78
# print a message (useful for debugging)
123
125
for i=1:length(__lines)
124
126
# add the next line of input
125
__input_so_far = strcat(__input_so_far, __lines[i], "\n")
127
__input_so_far = string(__input_so_far, __lines[i], "\n")
127
129
# try to parse it
128
130
__expr = parse_input_line(__input_so_far)
173
175
# event handler for socket input
174
add_fd_handler(__connectfd, __socket_callback)
176
enq_work(@task while true __socket_callback(__io) end)
176
178
web_show(user_id, ans) =
177
179
__Message(__MSG_OUTPUT_EVAL_RESULT, {user_id, sprint(repl_show, ans)})