-
Notifications
You must be signed in to change notification settings - Fork 4
/
eventmach.rb
99 lines (91 loc) · 2.82 KB
/
eventmach.rb
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
89
90
91
92
93
94
95
96
97
98
99
# RubIRCd - An IRC server written in Ruby
# Copyright (C) 2013 Lloyd Dilley (see authors.txt for details)
# http://www.rubircd.rocks/
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License along
# with this program; if not, write to the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
require_relative 'log'
require_relative 'options'
require_relative 'server'
# Check if EventMachine is available
begin
gem 'eventmachine', '>=1.0.3'
require 'eventmachine'
rescue Gem::LoadError
puts 'EventMachine gem not found!'
Log.write(4, 'EventMachine gem not found!')
exit!
end
# Handles plain-text connections
module PlainServer
def post_init
Server.increment_clients
# TODO: A lot...
end
def receive_data(data)
puts "Received data: #{data}"
send_data(data)
end
def unbind
puts 'Plain user disconnected.'
end
end
# Handles SSL connections
module SecureServer
def post_init
start_tls(private_key_file: 'cfg/key.pem', cert_chain_file: 'cfg/cert.pem', verify_peer: false)
Server.increment_clients
# TODO: A lot...
end
def unbind
puts 'SSL user disconnected.'
end
end
# Handles event-driven I/O for network communication
# using EventMachine
# This class should offer better performance than
# native select() since it provides access to epoll
# and kqueue where available
# This class is currently just a stub
class EventMach
def self.start
if RUBY_PLATFORM == 'java' && !Options.ssl_port.nil?
puts 'EventMachine does not support SSL/TLS when using JRuby!'
Log.write(4, 'EventMachine does not support SSL/TLS when using JRuby!')
exit!
end
EventMachine.epoll
# Check if kqueue is available on this platform
EventMachine.kqueue = true if EM.kqueue?
EventMachine.run do
begin
if Options.listen_host.nil?
listen_host = '0.0.0.0'
else
listen_host = Options.listen_host
end
EventMachine.start_server(listen_host, Options.listen_port, PlainServer)
unless Options.ssl_port.nil?
EventMachine.start_server(listen_host, Options.ssl_port, SecureServer)
end
rescue => e
puts 'Unable to listen on socket.'
puts e
Log.write(4, 'Unable to listen on socket.')
Log.write(4, e)
exit!
end
end
end
end