Class: Protocol::WebSocket::Extension::Compression::Deflate

Inherits:
Object
  • Object
show all
Defined in:
lib/protocol/websocket/extension/compression/deflate.rb

Overview

Compresses outgoing WebSocket frames using the DEFLATE algorithm.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(parent, level: Zlib::DEFAULT_COMPRESSION, memory_level: Zlib::DEF_MEM_LEVEL, strategy: Zlib::DEFAULT_STRATEGY, window_bits: 15, context_takeover: true, **options) ⇒ Deflate

Initialize a new deflate compressor.



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/protocol/websocket/extension/compression/deflate.rb', line 39

def initialize(parent, level: Zlib::DEFAULT_COMPRESSION, memory_level: Zlib::DEF_MEM_LEVEL, strategy: Zlib::DEFAULT_STRATEGY, window_bits: 15, context_takeover: true, **options)
	@parent = parent
	
	@deflate = nil
	
	@level = level
	@memory_level = memory_level
	@strategy = strategy
	
	# This is handled during negotiation:
	# if window_bits < MINIMUM_WINDOW_BITS
	# 	window_bits = MINIMUM_WINDOW_BITS
	# end
	
	@window_bits = window_bits
	@context_takeover = context_takeover
end

Instance Attribute Details

#context_takeoverObject (readonly)

Returns the value of attribute context_takeover.



65
66
67
# File 'lib/protocol/websocket/extension/compression/deflate.rb', line 65

def context_takeover
  @context_takeover
end

#The window size in bits used for compression.(windowsize) ⇒ Object (readonly)



63
# File 'lib/protocol/websocket/extension/compression/deflate.rb', line 63

attr :window_bits

#window_bitsObject (readonly)

Returns the value of attribute window_bits.



63
64
65
# File 'lib/protocol/websocket/extension/compression/deflate.rb', line 63

def window_bits
  @window_bits
end

Class Method Details

.client(parent, client_max_window_bits: 15, client_no_context_takeover: false, **options) ⇒ Object

Client writing to server.



15
16
17
18
19
20
21
# File 'lib/protocol/websocket/extension/compression/deflate.rb', line 15

def self.client(parent, client_max_window_bits: 15, client_no_context_takeover: false, **options)
	self.new(parent,
		window_bits: client_max_window_bits,
		context_takeover: !client_no_context_takeover,
		**options
	)
end

.server(parent, server_max_window_bits: 15, server_no_context_takeover: false, **options) ⇒ Object

Server writing to client.



24
25
26
27
28
29
30
# File 'lib/protocol/websocket/extension/compression/deflate.rb', line 24

def self.server(parent, server_max_window_bits: 15, server_no_context_takeover: false, **options)
	self.new(parent,
		window_bits: server_max_window_bits,
		context_takeover: !server_no_context_takeover,
		**options
	)
end

Instance Method Details

#pack_binary_frame(buffer, compress: false, **options) ⇒ Object

Pack a binary frame, optionally compressing the buffer.



89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/protocol/websocket/extension/compression/deflate.rb', line 89

def pack_binary_frame(buffer, compress: false, **options)
	if compress
		buffer = self.deflate(buffer)
	end
	
	frame = @parent.pack_binary_frame(buffer, **options)
	
	if compress
		frame.flags |= Frame::RSV1
	end
	
	return frame
end

#pack_text_frame(buffer, compress: true, **options) ⇒ Object

Pack a text frame, optionally compressing the buffer.



71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/protocol/websocket/extension/compression/deflate.rb', line 71

def pack_text_frame(buffer, compress: true, **options)
	if compress
		buffer = self.deflate(buffer)
	end
	
	frame = @parent.pack_text_frame(buffer, **options)
	
	if compress
		frame.flags |= Frame::RSV1
	end
	
	return frame
end

#to_sObject



58
59
60
# File 'lib/protocol/websocket/extension/compression/deflate.rb', line 58

def to_s
	"#<#{self.class} window_bits=#{@window_bits} context_takeover=#{@context_takeover}>"
end

#Whether the compression context is reused across messages.=(thecompressioncontextisreusedacrossmessages. = (value)) ⇒ Object



65
# File 'lib/protocol/websocket/extension/compression/deflate.rb', line 65

attr :context_takeover