Comparison

init.lua @ 411:db462d4feb44

verse: trim trailing whitespace
author Kim Alvefur <zash@zash.se>
date Tue, 18 Apr 2017 16:46:26 +0200
parent 399:82ad158714e5
child 417:d46a502955c0
comparison
equal deleted inserted replaced
410:6171ef2a4025 411:db462d4feb44
113 end 113 end
114 114
115 function stream:connect(connect_host, connect_port) 115 function stream:connect(connect_host, connect_port)
116 connect_host = connect_host or "localhost"; 116 connect_host = connect_host or "localhost";
117 connect_port = tonumber(connect_port) or 5222; 117 connect_port = tonumber(connect_port) or 5222;
118 118
119 -- Create and initiate connection 119 -- Create and initiate connection
120 local conn = socket.tcp() 120 local conn = socket.tcp()
121 conn:settimeout(0); 121 conn:settimeout(0);
122 conn:setoption("keepalive", true); 122 conn:setoption("keepalive", true);
123 local success, err = conn:connect(connect_host, connect_port); 123 local success, err = conn:connect(connect_host, connect_port);
124 124
125 if not success and err ~= "timeout" then 125 if not success and err ~= "timeout" then
126 self:warn("connect() to %s:%d failed: %s", connect_host, connect_port, err); 126 self:warn("connect() to %s:%d failed: %s", connect_host, connect_port, err);
127 return self:event("disconnected", { reason = err }) or false, err; 127 return self:event("disconnected", { reason = err }) or false, err;
128 end 128 end
129 129
145 return conn:write(data); 145 return conn:write(data);
146 end; 146 end;
147 end 147 end
148 148
149 function stream:close(reason) 149 function stream:close(reason)
150 if not self.conn then 150 if not self.conn then
151 verse.log("error", "Attempt to close disconnected connection - possibly a bug"); 151 verse.log("error", "Attempt to close disconnected connection - possibly a bug");
152 return; 152 return;
153 end 153 end
154 local on_disconnect = self.conn.disconnect(); 154 local on_disconnect = self.conn.disconnect();
155 self.conn:close(); 155 self.conn:close();
212 end 212 end
213 213
214 -- Listener factory 214 -- Listener factory
215 function new_listener(stream) 215 function new_listener(stream)
216 local conn_listener = {}; 216 local conn_listener = {};
217 217
218 function conn_listener.onconnect(conn) 218 function conn_listener.onconnect(conn)
219 if stream.server then 219 if stream.server then
220 local client = verse.new(); 220 local client = verse.new();
221 conn:setlistener(new_listener(client)); 221 conn:setlistener(new_listener(client));
222 client:set_conn(conn); 222 client:set_conn(conn);
224 else 224 else
225 stream.connected = true; 225 stream.connected = true;
226 stream:event("connected"); 226 stream:event("connected");
227 end 227 end
228 end 228 end
229 229
230 function conn_listener.onincoming(conn, data) 230 function conn_listener.onincoming(conn, data)
231 stream:event("incoming-raw", data); 231 stream:event("incoming-raw", data);
232 end 232 end
233 233
234 function conn_listener.ondisconnect(conn, err) 234 function conn_listener.ondisconnect(conn, err)
235 if conn ~= stream.conn then return end 235 if conn ~= stream.conn then return end
236 stream.connected = false; 236 stream.connected = false;
237 stream:event("disconnected", { reason = err }); 237 stream:event("disconnected", { reason = err });
238 end 238 end
239 239
240 function conn_listener.ondrain(conn) 240 function conn_listener.ondrain(conn)
241 stream:event("drained"); 241 stream:event("drained");
242 end 242 end
243 243
244 function conn_listener.onstatus(conn, new_status) 244 function conn_listener.onstatus(conn, new_status)
245 stream:event("status", new_status); 245 stream:event("status", new_status);
246 end 246 end
247 247
248 return conn_listener; 248 return conn_listener;
249 end 249 end
250 250
251 return verse; 251 return verse;