Class: EventMachine::Synchrony::TCPSocket

Inherits:
Connection
  • Object
show all
Defined in:
lib/em-synchrony/tcpsocket.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

._old_newObject



5
# File 'lib/em-synchrony/tcpsocket.rb', line 5

alias_method :_old_new, :new

.new(*args) ⇒ Object Also known as: open



6
7
8
9
10
11
12
13
14
# File 'lib/em-synchrony/tcpsocket.rb', line 6

def new(*args)
  if args.size == 1
    _old_new(*args)
  else
    socket = EventMachine::connect(*args[0..1], self)
    raise SocketError unless socket.sync(:in)  # wait for connection
    socket
  end
end

Instance Method Details

#closeObject



50
51
52
53
# File 'lib/em-synchrony/tcpsocket.rb', line 50

def close
  close_connection true
  @in_req = @out_req = nil
end

#closed?Boolean

Returns:

  • (Boolean)


23
24
25
# File 'lib/em-synchrony/tcpsocket.rb', line 23

def closed?
  @in_req.nil? && @out_req.nil?
end

#connection_completedObject

EventMachine interface



56
57
58
# File 'lib/em-synchrony/tcpsocket.rb', line 56

def connection_completed
  @in_req.succeed self
end

#post_initObject



18
19
20
21
# File 'lib/em-synchrony/tcpsocket.rb', line 18

def post_init
  @in_buff, @out_buff = '', ''
  @in_req = @out_req = nil
end

#read(num_bytes = 16*1024, dest = nil) ⇒ Object Also known as: read_nonblock, recv



44
45
46
# File 'lib/em-synchrony/tcpsocket.rb', line 44

def read(num_bytes = 16*1024, dest = nil)
  read_data(num_bytes, dest) or sync(:in) or raise(IOError)
end

#receive_data(data) ⇒ Object



66
67
68
69
70
71
# File 'lib/em-synchrony/tcpsocket.rb', line 66

def receive_data(data)
  @in_buff << data
  if @in_req && (data = read_data)
    @in_req.succeed data
  end
end

#send(msg, flags = 0) ⇒ Object Also known as: write



36
37
38
39
40
41
# File 'lib/em-synchrony/tcpsocket.rb', line 36

def send(msg, flags = 0)
  raise "Unknown flags in send(): #{flags}" if flags.nonzero?
  len = msg.bytesize
  write_data(msg) or sync(:out) or raise(IOError)
  len
end

#setsockopt(level, name, value) ⇒ Object

TCPSocket interface



34
# File 'lib/em-synchrony/tcpsocket.rb', line 34

def setsockopt(level, name, value); end

#sync(direction) ⇒ Object

direction must be one of :in or :out



28
29
30
31
# File 'lib/em-synchrony/tcpsocket.rb', line 28

def sync(direction)
  req = self.instance_variable_set "@#{direction.to_s}_req", EventMachine::DefaultDeferrable.new
  EventMachine::Synchrony.sync req
end

#unbindObject



60
61
62
63
64
# File 'lib/em-synchrony/tcpsocket.rb', line 60

def unbind
  @in_req.fail  nil if @in_req
  @out_req.fail nil if @out_req
  close
end