Class: IRCMachine::IRCSocketAdapter
- Inherits:
-
Object
- Object
- IRCMachine::IRCSocketAdapter
- Defined in:
- lib/ircmachine/ircsocketadapter.rb
Constant Summary collapse
- IRC_MAX_DATA =
As per RFC 2812:
512
Instance Attribute Summary collapse
-
#socket ⇒ Object
readonly
Returns the value of attribute socket.
Instance Method Summary collapse
-
#initialize(socket) ⇒ IRCSocketAdapter
constructor
A new instance of IRCSocketAdapter.
- #recv(blocking = true) ⇒ Object
- #recv_nonblock ⇒ Object
- #send(data) ⇒ Object
Constructor Details
#initialize(socket) ⇒ IRCSocketAdapter
Returns a new instance of IRCSocketAdapter.
8 9 10 11 12 13 14 |
# File 'lib/ircmachine/ircsocketadapter.rb', line 8 def initialize(socket) @socket = socket @messages = [] @messages_mtx = Mutex.new @buffer = '' @buffer_mtx = Mutex.new end |
Instance Attribute Details
#socket ⇒ Object (readonly)
Returns the value of attribute socket.
3 4 5 |
# File 'lib/ircmachine/ircsocketadapter.rb', line 3 def socket @socket end |
Instance Method Details
#recv(blocking = true) ⇒ Object
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 |
# File 'lib/ircmachine/ircsocketadapter.rb', line 16 def recv(blocking = true) # If we have data to return already, return it. @messages_mtx.synchronize { return @messages.shift unless @messages.empty? } got_msg = false msg = nil until got_msg data = if blocking @socket.recv(IRC_MAX_DATA) else @socket.recv_nonblock(IRC_MAX_DATA) end unless data.nil? @buffer_mtx.synchronize do data.each_char do |c| @buffer << c if c == "\n" @messages_mtx.synchronize do begin msg = IRCMachine::Message.parse(@buffer) @messages << msg rescue ArgumentError # => Not recognised; ignore it. end end @buffer = '' elsif @buffer.size > IRC_MAX_DATA # Too long. Drop it. @buffer = '' end end end end msg = @messages_mtx.synchronize { @messages.shift unless @messages.empty? } got_msg = !blocking || !msg.nil? end msg end |
#recv_nonblock ⇒ Object
60 61 62 |
# File 'lib/ircmachine/ircsocketadapter.rb', line 60 def recv_nonblock recv(false) end |
#send(data) ⇒ Object
64 65 66 67 68 69 |
# File 'lib/ircmachine/ircsocketadapter.rb', line 64 def send(data) msg = IRCMachine::Message.parse(data) fail ArgumentError, "Invalid IRC data." if msg.nil? @socket.send(msg.to_s) end |