Class: Cod::SocketServer
- Inherits:
-
Object
- Object
- Cod::SocketServer
- Includes:
- Callbacks
- Defined in:
- lib/cod/socket_server.rb
Overview
Abstract base class for all kinds of socket based servers. Useful for all types of channels that know an #accept.
Direct Known Subclasses
Instance Attribute Summary collapse
-
#serializer ⇒ Object
readonly
Returns the value of attribute serializer.
-
#socket ⇒ Object
readonly
Returns the value of attribute socket.
Instance Method Summary collapse
-
#close ⇒ Object
Closes the channel.
-
#connections ⇒ Object
Returns the number of clients that are connected to this server currently.
-
#get(opts = {}) ⇒ Object
Receives one object from the channel.
-
#get_ext(opts = {}) ⇒ Array<Object, TcpClient>
Receives one object from the channel.
-
#initialize(serializer, socket) ⇒ SocketServer
constructor
A new instance of SocketServer.
-
#request_close(socket) ⇒ Object
Notifies the TcpServer that one of its connections needs to be closed.
-
#to_read_fds ⇒ Object
Returns an array of IOs that Cod.select should select on.
Methods included from Callbacks
#callbacks_enabled?, #register_callback, #using_callbacks
Constructor Details
#initialize(serializer, socket) ⇒ SocketServer
Returns a new instance of SocketServer.
14 15 16 17 18 19 20 |
# File 'lib/cod/socket_server.rb', line 14 def initialize(serializer, socket) @socket = socket @client_sockets = [] @round_robin_index = 0 = Array.new @serializer = serializer end |
Instance Attribute Details
#serializer ⇒ Object (readonly)
Returns the value of attribute serializer.
12 13 14 |
# File 'lib/cod/socket_server.rb', line 12 def serializer @serializer end |
#socket ⇒ Object (readonly)
Returns the value of attribute socket.
11 12 13 |
# File 'lib/cod/socket_server.rb', line 11 def socket @socket end |
Instance Method Details
#close ⇒ Object
Closes the channel.
59 60 61 62 |
# File 'lib/cod/socket_server.rb', line 59 def close @socket.close @client_sockets.each { |io| io.close } end |
#connections ⇒ Object
Returns the number of clients that are connected to this server currently.
73 74 75 |
# File 'lib/cod/socket_server.rb', line 73 def connections @client_sockets.size end |
#get(opts = {}) ⇒ Object
Receives one object from the channel. This will receive one message from one of the connected clients in a round-robin fashion.
31 32 33 34 |
# File 'lib/cod/socket_server.rb', line 31 def get(opts={}) msg, socket = _get(opts) return msg end |
#get_ext(opts = {}) ⇒ Array<Object, TcpClient>
Receives one object from the channel. Returns a tuple of <message,channel> where channel is a tcp channel that links back to the client that sent message.
Using this method, the server can communicate back to its clients individually instead of collectively.
50 51 52 53 54 55 |
# File 'lib/cod/socket_server.rb', line 50 def get_ext(opts={}) msg, socket = _get(opts) return [ msg, back_channel(socket)] end |
#request_close(socket) ⇒ Object
Notifies the TcpServer that one of its connections needs to be closed. This can be triggered by using #get_ext to obtain a handle to connections and then calling #close on that connection.
- void
86 87 88 89 |
# File 'lib/cod/socket_server.rb', line 86 def request_close(socket) @client_sockets.delete(socket) socket.close end |
#to_read_fds ⇒ Object
Returns an array of IOs that Cod.select should select on.
66 67 68 |
# File 'lib/cod/socket_server.rb', line 66 def to_read_fds @client_sockets + [@socket] end |