cakedb 0.0.9 → 0.0.10
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/cakedb.rb +27 -32
- metadata +1 -1
data/lib/cakedb.rb
CHANGED
@@ -8,6 +8,7 @@ class CakeDB
|
|
8
8
|
@server = TCPSocket.new(server, port)
|
9
9
|
@sids = {}
|
10
10
|
@loggingLevel = lvl
|
11
|
+
@timeout = 30
|
11
12
|
end
|
12
13
|
def get_sid(stream)
|
13
14
|
if @sids[stream].nil?
|
@@ -39,6 +40,30 @@ class CakeDB
|
|
39
40
|
return details
|
40
41
|
end
|
41
42
|
|
43
|
+
def data_in(length)
|
44
|
+
data = nil
|
45
|
+
got = 0
|
46
|
+
timeOver = Time.now + @timeout
|
47
|
+
while got < length
|
48
|
+
data = @server.recv(length)
|
49
|
+
got += data.bytesize
|
50
|
+
if Time.now > timeOver
|
51
|
+
raise "Timeout on incomplete read, returning what I had"
|
52
|
+
if @loggingLevel == 1
|
53
|
+
puts "Timeout Hit. GT #{timeOver}"
|
54
|
+
end
|
55
|
+
return data
|
56
|
+
end
|
57
|
+
if @loggingLevel == 2 && got < length
|
58
|
+
puts "Incomplete read on payload receive"
|
59
|
+
puts "Raw Data: #{data}"
|
60
|
+
puts "Length: #{length} bytes"
|
61
|
+
puts "Raw Data length: #{data.bytesize}"
|
62
|
+
end
|
63
|
+
end
|
64
|
+
return data
|
65
|
+
end
|
66
|
+
|
42
67
|
def read(stream, ts, tsend=nil, mode=4)
|
43
68
|
#Issue the query
|
44
69
|
sid = get_sid(stream)
|
@@ -52,50 +77,20 @@ class CakeDB
|
|
52
77
|
result = Array.new
|
53
78
|
|
54
79
|
#How much data we gotta get?
|
55
|
-
|
56
|
-
recv_total = @server.recv(4).unpack("L>")[0]
|
80
|
+
recv_total = data_in(4).unpack("L>")[0]
|
57
81
|
#puts "total is #{recv_total}"
|
58
82
|
if recv_total > 0
|
59
83
|
counter = 0
|
60
84
|
#Recive the result
|
61
85
|
while recv_total > 0
|
62
86
|
#Get the header - 12 bytes
|
63
|
-
header_whole =
|
87
|
+
header_whole = data_in(12)
|
64
88
|
header = header_whole.unpack("Q>L>")
|
65
|
-
if header[0] == nil || header[1] == nil
|
66
|
-
if @loggingLevel == 1
|
67
|
-
puts "You have been eaten by a Grue"
|
68
|
-
end
|
69
|
-
raise "You have been eaten by a Grue"
|
70
|
-
end
|
71
|
-
if header_whole.bytesize !=12
|
72
|
-
if @loggingLevel == 1
|
73
|
-
puts "Incomplete header"
|
74
|
-
puts "Length: #{header[1]}"
|
75
|
-
puts "TS: #{header[0]}"
|
76
|
-
end
|
77
|
-
raise "Header Incomplete"
|
78
|
-
end
|
79
89
|
|
80
90
|
#recieve the data, header says how much
|
81
91
|
result[counter] = Hash.new
|
82
92
|
result[counter]["ts"] = header[0]
|
83
93
|
result[counter]["data"] = @server.recv(header[1])
|
84
|
-
|
85
|
-
#Keep reading till we have everything we expect
|
86
|
-
while(result[counter]["data"].bytesize < header[1])
|
87
|
-
result[counter]["data"] += @server.recv(header[1] - result[counter]["data"].bytesize)
|
88
|
-
puts "Incomplete read: #{result[counter]["data"].bytesize} / #{header[1]}"
|
89
|
-
end
|
90
|
-
if result[counter]["data"].bytesize != header[1]
|
91
|
-
if @loggingLevel == 2
|
92
|
-
puts "Incomplete read on payload receive"
|
93
|
-
puts "Raw Data: #{resultt[counter]}"
|
94
|
-
puts "Length: #{header[1]} bytes"
|
95
|
-
puts "Raw Data length: #{resultt[counter].bytesize - 12}"
|
96
|
-
puts "Total Length: #{recv_total}"
|
97
|
-
end
|
98
|
-
end
|
99
94
|
recv_total = recv_total - header[1] - 12
|
100
95
|
counter+=1
|
101
96
|
end
|