Class: AlWorker::Tcp
- Inherits:
-
Object
- Object
- AlWorker::Tcp
- Defined in:
- lib/al_worker_tcp.rb
Overview
TCPサーバ
Constant Summary collapse
- @@servers =
Returns リスンサーバ.
[]
Instance Attribute Summary collapse
-
#address ⇒ String
リスンアドレス.
-
#banner ⇒ String
接続後のバナー表示.
-
#cb_prefix ⇒ String
コールバックメソッドのプレフィックス.
-
#external_encoding ⇒ Encoding
readonly
受信データのエンコーディング.
-
#mode_param ⇒ Symbol
パラメータのデータ形式 :json :text.
-
#mode_service ⇒ Symbol
動作モード :thread :process.
-
#port ⇒ Integer
リスンポート.
-
#server ⇒ TCPServer
readonly
リスンサーバ.
Instance Method Summary collapse
-
#close ⇒ Object
TCPサービス終了.
-
#initialize(address = "", port = 1944) ⇒ Tcp
constructor
constructor.
-
#run(obj) ⇒ Object
実行開始.
-
#set_encoding(encoding) ⇒ Object
受信データのエンコーディングを指定.
Constructor Details
#initialize(address = "", port = 1944) ⇒ Tcp
constructor
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
#address ⇒ String
Returns リスンアドレス.
21 22 23 |
# File 'lib/al_worker_tcp.rb', line 21 def address @address end |
#banner ⇒ String
Returns 接続後のバナー表示.
39 40 41 |
# File 'lib/al_worker_tcp.rb', line 39 def @banner end |
#cb_prefix ⇒ String
Returns コールバックメソッドのプレフィックス.
36 37 38 |
# File 'lib/al_worker_tcp.rb', line 36 def cb_prefix @cb_prefix end |
#external_encoding ⇒ Encoding (readonly)
Returns 受信データのエンコーディング.
42 43 44 |
# File 'lib/al_worker_tcp.rb', line 42 def external_encoding @external_encoding end |
#mode_param ⇒ Symbol
Returns パラメータのデータ形式 :json :text.
33 34 35 |
# File 'lib/al_worker_tcp.rb', line 33 def mode_param @mode_param end |
#mode_service ⇒ Symbol
Returns 動作モード :thread :process.
30 31 32 |
# File 'lib/al_worker_tcp.rb', line 30 def mode_service @mode_service end |
#port ⇒ Integer
Returns リスンポート.
24 25 26 |
# File 'lib/al_worker_tcp.rb', line 24 def port @port end |
#server ⇒ TCPServer (readonly)
Returns リスンサーバ.
27 28 29 |
# File 'lib/al_worker_tcp.rb', line 27 def server @server end |
Instance Method Details
#close ⇒ Object
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モードと同等の動作になる。
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
受信データのエンコーディングを指定
66 67 68 |
# File 'lib/al_worker_tcp.rb', line 66 def set_encoding( encoding ) @external_encoding = encoding end |