SHOW:
|
|
- or go back to the newest paste.
1 | local HOST = "bridge.computercraft.ru" | |
2 | local PORT = 1111 | |
3 | local RC = {} | |
4 | local socket, internet | |
5 | local ready = false | |
6 | local computer = require('computer') | |
7 | ||
8 | -- SOCKET -- | |
9 | ||
10 | local function localInit() | |
11 | internet = require("internet") | |
12 | socket = internet.socket(HOST, PORT) | |
13 | end | |
14 | ||
15 | function RC.init() | |
16 | local ok = pcall(localInit) | |
17 | ready = ok | |
18 | return ok | |
19 | end | |
20 | ||
21 | function RC.finish() | |
22 | socket:close() | |
23 | ready = false | |
24 | end | |
25 | ||
26 | local function localSend(s) | |
27 | socket:write(s.."\0") | |
28 | --socket:flush() | |
29 | end | |
30 | ||
31 | function RC.send(msg) | |
32 | if ready then | |
33 | local ok = pcall(localSend, msg) | |
34 | ready = ok | |
35 | return ok | |
36 | end | |
37 | return false | |
38 | end | |
39 | ||
40 | function RC.auth(key) | |
41 | return RC.send("KOC-"..key) | |
42 | end | |
43 | ||
44 | local function localReceive() | |
45 | local sym = socket:read(1) | |
46 | if sym == '' then | |
47 | return false | |
48 | else | |
49 | local message = '' | |
50 | while sym ~= '\n' do | |
51 | message = message..sym | |
52 | sym = socket:read(1) | |
53 | end | |
54 | return true, message | |
55 | end | |
56 | end | |
57 | ||
58 | function RC.receive() | |
59 | if ready then | |
60 | local ok, state, msg = pcall(localReceive) | |
61 | return state, msg | |
62 | end | |
63 | return false | |
64 | end | |
65 | ||
66 | -- split message to words and params | |
67 | local function split(message) | |
68 | local tokens = {} | |
69 | for token in message:gmatch("%S+") do | |
70 | table.insert(tokens, token) | |
71 | end | |
72 | return tokens | |
73 | end | |
74 | ||
75 | -- HANDLERS -- | |
76 | local handlers = {} | |
77 | ||
78 | function RC.pull() | |
79 | local state,tmp = localReceive() | |
80 | if state then | |
81 | -- Отправляем эвент. | |
82 | computer.pushSignal('bridge_message', tmp) | |
83 | -- Чекаем все хендлеры, на случай, если кто-то отвечает за полученную команду. | |
84 | local tokens = split(tmp) | |
85 | if handlers[tokens[1]] ~= nil then | |
86 | -- отделяем аргументы | |
87 | local args = {} | |
88 | for c=2, #tokens do table.insert(args, tokens[c]) end | |
89 | -- вызываем подопытные функции передав им полученные аргументы | |
90 | for k, callback in pairs(handlers[tokens[1]]) do | |
91 | state, error_message = pcall(callback, table.unpack(args)) | |
92 | if (not state) then | |
93 | print('Нефиг корявые функции писать!!! by NEO and Totoro ') | |
94 | error(error_message) | |
95 | end | |
96 | end | |
97 | end | |
98 | end | |
99 | end | |
100 | ||
101 | function RC.addHandler(command, callback) | |
102 | if command ~= nil and command ~= "" then | |
103 | if callback ~= nil and type(callback) == 'function' then | |
104 | if handlers[command] == nil then handlers[command] = {} end | |
105 | table.insert(handlers[command], callback) | |
106 | end | |
107 | end | |
108 | end | |
109 | ||
110 | function RC.removeHandler(command) | |
111 | handlers[command] = nil | |
112 | end | |
113 | ||
114 | ||
115 | return RC |