Class: AlWorker::Tcp

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

Overview

TCPサーバ

Constant Summary collapse

@@servers =

Returns リスンサーバ.

Returns:

  • (Array<TCPServer>)

    リスンサーバ

[]

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(address = "", port = 1944) ⇒ Tcp

constructor

Parameters:

  • address (String) (defaults to: "")

    リスンアドレス

  • port (Integer) (defaults to: 1944)

    リスンポート



51
52
53
54
55
56
57
58
# File 'lib/al_worker_tcp.rb', line 51

def initialize( address = "", port = 1944 )
  @address = address
  @port = port
  @mode_service = :thread
  @mode_param = :json
  @cb_prefix = "tcp"
  @external_encoding = Encoding::UTF_8
end

Instance Attribute Details

#addressString

Returns リスンアドレス.

Returns:

  • (String)

    リスンアドレス



21
22
23
# File 'lib/al_worker_tcp.rb', line 21

def address
  @address
end

Returns 接続後のバナー表示.

Returns:

  • (String)

    接続後のバナー表示



39
40
41
# File 'lib/al_worker_tcp.rb', line 39

def banner
  @banner
end

#cb_prefixString

Returns コールバックメソッドのプレフィックス.

Returns:

  • (String)

    コールバックメソッドのプレフィックス



36
37
38
# File 'lib/al_worker_tcp.rb', line 36

def cb_prefix
  @cb_prefix
end

#external_encodingEncoding (readonly)

Returns 受信データのエンコーディング.

Returns:

  • (Encoding)

    受信データのエンコーディング



42
43
44
# File 'lib/al_worker_tcp.rb', line 42

def external_encoding
  @external_encoding
end

#mode_paramSymbol

Returns パラメータのデータ形式 :json :text.

Returns:

  • (Symbol)

    パラメータのデータ形式 :json :text



33
34
35
# File 'lib/al_worker_tcp.rb', line 33

def mode_param
  @mode_param
end

#mode_serviceSymbol

Returns 動作モード :thread :process.

Returns:

  • (Symbol)

    動作モード :thread :process



30
31
32
# File 'lib/al_worker_tcp.rb', line 30

def mode_service
  @mode_service
end

#portInteger

Returns リスンポート.

Returns:

  • (Integer)

    リスンポート



24
25
26
# File 'lib/al_worker_tcp.rb', line 24

def port
  @port
end

#serverTCPServer (readonly)

Returns リスンサーバ.

Returns:

  • (TCPServer)

    リスンサーバ



27
28
29
# File 'lib/al_worker_tcp.rb', line 27

def server
  @server
end

Instance Method Details

#closeObject

TCPサービス終了



121
122
123
# File 'lib/al_worker_tcp.rb', line 121

def close()
  @server.close()  if !@server.closed?()
end

#run(obj) ⇒ Object

Note:

実行開始

プロセスモードで動作時は、syncモードと同等の動作になる。

Parameters:

  • obj (AlWorker)

    ワーカオブジェクト



78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/al_worker_tcp.rb', line 78

def run( obj )
  @me = obj
  @server = TCPServer.new( @address, @port )
  @@servers << @server

  case @mode_service
  when :process
    Thread.start {
      while true
        sock = @server.accept
        pid = AlWorker.mutex_sync.synchronize { Process.fork() }

        # child
        if !pid
          @@servers.each {|server| server.close }
          _start_service( sock )
          exit!
        end

        # parent
        sock.close
        Process.detach( pid )
      end
    }

  when :thread
    Thread.start {
      while true
        Thread.start( @server.accept ) { |sock|
          _start_service( sock )
        }
      end
    }

  else
    raise "Illegal mode_service"
  end
end

#set_encoding(encoding) ⇒ Object

受信データのエンコーディングを指定

Parameters:

  • encoding (Encoding)

    エンコーディング



66
67
68
# File 'lib/al_worker_tcp.rb', line 66

def set_encoding( encoding )
  @external_encoding = encoding
end