Class: Hprose::TcpServer

Inherits:
Service show all
Defined in:
lib/hprose/tcpserver.rb

Constant Summary

Constants included from ResultMode

ResultMode::Normal, ResultMode::Raw, ResultMode::RawWithEndTag, ResultMode::Serialized

Constants included from Tags

Hprose::Tags::TagArgument, Hprose::Tags::TagBytes, Hprose::Tags::TagCall, Hprose::Tags::TagClass, Hprose::Tags::TagClosebrace, Hprose::Tags::TagDate, Hprose::Tags::TagDouble, Hprose::Tags::TagEmpty, Hprose::Tags::TagEnd, Hprose::Tags::TagError, Hprose::Tags::TagFalse, Hprose::Tags::TagFunctions, Hprose::Tags::TagGuid, Hprose::Tags::TagInfinity, Hprose::Tags::TagInteger, Hprose::Tags::TagList, Hprose::Tags::TagLong, Hprose::Tags::TagMap, Hprose::Tags::TagNaN, Hprose::Tags::TagNeg, Hprose::Tags::TagNine, Hprose::Tags::TagNull, Hprose::Tags::TagObject, Hprose::Tags::TagOpenbrace, Hprose::Tags::TagPoint, Hprose::Tags::TagPos, Hprose::Tags::TagQuote, Hprose::Tags::TagRef, Hprose::Tags::TagResult, Hprose::Tags::TagSemicolon, Hprose::Tags::TagString, Hprose::Tags::TagTime, Hprose::Tags::TagTrue, Hprose::Tags::TagUTC, Hprose::Tags::TagUTF8Char, Hprose::Tags::TagZero

Instance Attribute Summary collapse

Attributes inherited from Service

#debug, #on_after_invoke, #on_before_invoke, #on_send_error, #simple

Instance Method Summary collapse

Methods inherited from Service

#add, #add_block, #add_class_methods, #add_filter, #add_function, #add_functions, #add_instance_methods, #add_method, #add_methods, #add_missing_function, #filter, #filter=, #remove_filter

Constructor Details

#initialize(uri = nil) ⇒ TcpServer

Returns a new instance of TcpServer.



29
30
31
32
33
34
35
36
37
38
39
# File 'lib/hprose/tcpserver.rb', line 29

def initialize(uri = nil)
  super()
  @host = nil
  @port = 0
  unless uri.nil? then
    u = URI.parse(uri)
    @host = u.host
    @port = u.port
  end
  @sockets = nil
end

Instance Attribute Details

#hostObject

Returns the value of attribute host.



40
41
42
# File 'lib/hprose/tcpserver.rb', line 40

def host
  @host
end

#portObject

Returns the value of attribute port.



40
41
42
# File 'lib/hprose/tcpserver.rb', line 40

def port
  @port
end

Instance Method Details

#startObject



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/hprose/tcpserver.rb', line 41

def start
  begin
    @sockets = Socket.tcp_server_sockets(@host, @port)
    @sockets.each do
      Socket.accept_loop(@sockets) do |sock, client_addrinfo|
        Thread.start do
          begin
            loop do
              buf = sock.recv(4, 0)
              n = buf[0].ord << 24 | buf[1].ord << 16 | buf[2].ord << 8 | buf[3].ord
              data = handle(sock.recv(n, 0), client_addrinfo)
              n = data.size
              sock.send("" << (n >> 24 & 0xff) << (n >> 16 & 0xff) << (n >> 8 & 0xff) << (n & 0xff) << data, 0)
            end
          ensure
            sock.close
          end
        end
      end
    end
  rescue ::Interrupt => e
  ensure
    stop
  end
end

#stopObject



66
67
68
69
70
71
# File 'lib/hprose/tcpserver.rb', line 66

def stop
  unless @sockets.nil? then
     @sockets.each {|s| s.close if !s.closed? }
     @sockets = nil
  end
end