Class: Green::Socket

Inherits:
Socket
  • Object
show all
Includes:
Kgio::SocketMethods
Defined in:
lib/green/socket.rb

Overview

TODO puts

Direct Known Subclasses

TCPServer, TCPSocket

Constant Summary collapse

READ_BUFFER_SIZE =
65536

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.accept_socket_classObject



67
68
69
# File 'lib/green/socket.rb', line 67

def self.accept_socket_class
  self
end

Instance Method Details

#acceptObject



71
72
73
74
75
76
77
# File 'lib/green/socket.rb', line 71

def accept
  s, a = accept_nonblock
  [self.class.accept_socket_class.for_fd(s.fileno), a]
rescue Errno::EAGAIN
  waiter.wait_read
  retry
end

#connect(sock_addr) ⇒ Object



79
80
81
82
83
84
85
86
87
88
# File 'lib/green/socket.rb', line 79

def connect(sock_addr)
  connect_nonblock(sock_addr)
rescue Errno::EINPROGRESS
  error, = getsockopt(::Socket::SOL_SOCKET, ::Socket::SO_ERROR).unpack('i')
  if error != 0
    raise ERRORS[error]
  else
    waiter.wait_write
  end
end

#kgio_wait_readableObject



59
60
61
# File 'lib/green/socket.rb', line 59

def kgio_wait_readable
  waiter.wait_write
end

#read(length = nil, buffer = nil) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/green/socket.rb', line 23

def read(length = nil, buffer = nil)
  res = []
  readed = 0
  begin # begin ... while
    need_read = if length
      length - readed
    else
      READ_BUFFER_SIZE
    end        
    data = kgio_read(need_read)
    if data.nil? && length.nil? && readed == 0
      return ''
    elsif data.nil? && readed == 0
      return nil
    elsif data.nil?
      return buffer ? buffer.replace(res * '') : res * ''
    else
      readed += data.size
      res << data
    end
  end while length != readed
  return buffer ? buffer.replace(res * '') : res * ''
end

#recv(maxlen, flags = 0) ⇒ Object



47
48
49
50
51
52
# File 'lib/green/socket.rb', line 47

def recv(maxlen, flags = 0)
  recv_nonblock(maxlen)
rescue Errno::EAGAIN
  waiter.wait_read
  retry
end

#send(mesg, flags, dest_sockaddr = nil) ⇒ Object



54
55
56
57
# File 'lib/green/socket.rb', line 54

def send(mesg, flags, dest_sockaddr = nil)
  # FIXME
  write mesg
end

#waiterObject



90
91
92
# File 'lib/green/socket.rb', line 90

def waiter
  @waiter ||= Green.hub.socket_waiter(self)
end

#write(str) ⇒ Object



18
19
20
21
# File 'lib/green/socket.rb', line 18

def write(str)
  kgio_write(str)
  str.bytesize
end