Class: Server
- Inherits:
-
Object
- Object
- Server
- Defined in:
- lib/server.rb
Instance Method Summary collapse
- #read_int ⇒ Object
- #read_message ⇒ Object
- #run ⇒ Object
- #send_command(command, msg) ⇒ Object
- #send_file(file) ⇒ Object
- #send_message(msg) ⇒ Object
- #stop ⇒ Object
Instance Method Details
#read_int ⇒ Object
31 32 33 34 35 36 37 38 39 40 41 42 43 |
# File 'lib/server.rb', line 31 def read_int raise "client not connect #{@host}:#{@port}, please check" if @client == nil begin = @client.recvfrom(4)[0] msgInt = .unpack("L")[0] rescue StandardError => e puts e. puts e.backtrace.inspect end msgInt end |
#read_message ⇒ Object
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 |
# File 'lib/server.rb', line 45 def raise "client not connect #{@host}:#{@port}, please check" if @client == nil begin len = read_int if len == ~0 return nil end = @client.recvfrom(len)[0] rescue StandardError => e puts e. puts e.backtrace.inspect end end |
#run ⇒ Object
10 11 12 13 14 15 16 17 |
# File 'lib/server.rb', line 10 def run @server = Socket.new(AF_INET, SOCK_STREAM, 0) ip = Socket.ip_address_list.detect{|intf| intf.ipv4_private?} sockaddr = Socket.sockaddr_in(Config::PORT, ip.ip_address) @server.bind(sockaddr) @server.listen(1) @client, _ = @server.accept end |
#send_command(command, msg) ⇒ Object
77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 |
# File 'lib/server.rb', line 77 def send_command(command, msg) raise "client not connect #{@host}:#{@port}, please check" if @client == nil begin @client.write([command].pack("L")) if msg (msg) end rescue StandardError => e puts e. puts e.backtrace.inspect end true end |
#send_file(file) ⇒ Object
93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 |
# File 'lib/server.rb', line 93 def send_file(file) raise "client not connect #{@host}:#{@port}, please check" if @client == nil begin (file) command = read_int if command != HRCommandTransferFileNameFinished puts "send file name failed, #{file}" end size = File.size(file) ("#{size}") command = read_int if command != HRCommandTransferFileSizeFinished puts "send file size failed, #{size}" end content = "" File.open(file, "r") do |f| f.each_line do |line| content += line if content.bytesize >= 102400 @client.write(content) puts "read size #{content.bytesize} of file:#{file}" content = "" end end end if content.bytesize >= 0 @client.write(content) puts "read size #{content.bytesize} of file:#{file}" end command = read_int if command != HRCommandTransferFileFinished puts "send file failed, #{file}" end rescue StandardError => e puts e. puts e.backtrace.inspect end end |
#send_message(msg) ⇒ Object
63 64 65 66 67 68 69 70 71 72 73 74 75 |
# File 'lib/server.rb', line 63 def (msg) raise "client not connect #{@host}:#{@port}, please check" if @client == nil begin @client.write([msg.bytesize].pack("L")) @client.write(msg) rescue StandardError => e puts e. puts e.backtrace.inspect end true end |
#stop ⇒ Object
19 20 21 22 23 24 25 26 27 28 29 |
# File 'lib/server.rb', line 19 def stop if @client != nil @client.close @client = nil end if @server != nil @server.close @server = nil end end |