Class: EaseEngine::Socket

Inherits:
Object
  • Object
show all
Defined in:
lib/ease_engine/socket.rb

Direct Known Subclasses

TCPSocket, UDPSocket

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*args) ⇒ Socket

Returns a new instance of Socket.



11
12
13
14
15
16
17
18
# File 'lib/ease_engine/socket.rb', line 11

def initialize( *args )
  @read_buf = EaseEngine::Buffer.new
  @write_buf = EaseEngine::Buffer.new
  @err = nil
  @read_max_size = 2048
  @dst_addr = nil
  @is_disable = false
end

Instance Attribute Details

#dst_addrObject

Returns the value of attribute dst_addr.



8
9
10
# File 'lib/ease_engine/socket.rb', line 8

def dst_addr
  @dst_addr
end

#errObject

Returns the value of attribute err.



6
7
8
# File 'lib/ease_engine/socket.rb', line 6

def err
  @err
end

#is_disableObject

Returns the value of attribute is_disable.



9
10
11
# File 'lib/ease_engine/socket.rb', line 9

def is_disable
  @is_disable
end

#read_bufObject

Returns the value of attribute read_buf.



6
7
8
# File 'lib/ease_engine/socket.rb', line 6

def read_buf
  @read_buf
end

#read_max_sizeObject

Returns the value of attribute read_max_size.



7
8
9
# File 'lib/ease_engine/socket.rb', line 7

def read_max_size
  @read_max_size
end

#socketObject

Returns the value of attribute socket.



6
7
8
# File 'lib/ease_engine/socket.rb', line 6

def socket
  @socket
end

#write_bufObject

Returns the value of attribute write_buf.



6
7
8
# File 'lib/ease_engine/socket.rb', line 6

def write_buf
  @write_buf
end

Instance Method Details

#bind(host, port) ⇒ Object



20
21
22
# File 'lib/ease_engine/socket.rb', line 20

def bind( host, port )
  @socket.bind( host, port )
end

#closeObject



82
83
84
85
86
87
# File 'lib/ease_engine/socket.rb', line 82

def close
  if ! @socket.nil?
    @socket.close
    @socket = nil
  end
end

#errnoObject



93
94
95
# File 'lib/ease_engine/socket.rb', line 93

def errno
  @socket.getsockopt( ::Socket::SOL_SOCKET, ::Socket::SO_ERROR ).unpack( "i" )[ 0 ]
end

#recv(max, flags = 0) ⇒ Object



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
# File 'lib/ease_engine/socket.rb', line 40

def recv( max, flags = 0 )
  # 1回で全てのデータを読み込めるとは限らないので、複数回に分けてデータを全て読み込む
  @err = nil
  bufs = []
  while true
    begin
      buf = @socket.recv_nonblock( max, flags )
    rescue IO::EAGAINWaitReadable => err
      buf = ""
    rescue Errno::ECONNRESET => err
      buf = ""
    rescue => err
      @err = err
      buf = ""
    end
    break if buf.empty?
    
    bufs.push buf
  end
  
  buf = bufs.join
  @read_buf << buf
  @is_disable = true if bufs.empty?
  
  buf
end

#recvfrom(max, flags = 0) ⇒ Object



67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/ease_engine/socket.rb', line 67

def recvfrom( max, flags = 0 )
  # 最大サイズが決まっているので、1回で読み込む
  @err = nil
  begin
    result = @socket.recvfrom_nonblock( max, flags )
  rescue => err
    @err = err
    result = [ "", nil ]
  end
  
  @read_buf << result[ 0 ]
  
  result
end

#send(msg, flags, *args) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/ease_engine/socket.rb', line 24

def send( msg, flags, *args )
  @write_buf << msg
  @err = nil
  begin
    result = @socket.send( @write_buf.value, flags, *args )
  rescue => err
    @err = err
    result = 0
  end
  
  @write_buf >> result
  @is_disable = true if 0 == result
  
  result
end

#to_iObject



89
90
91
# File 'lib/ease_engine/socket.rb', line 89

def to_i
  @socket.to_i
end