Skip to content

Commit

Permalink
split more code away from protocol: move compression related code
Browse files Browse the repository at this point in the history
git-svn-id: https://xpra.org/svn/Xpra/trunk@6951 3bb7dfac-3a0b-4e04-842a-767bc560f471
  • Loading branch information
totaam committed Jul 25, 2014
1 parent 0f80d3f commit d2e9c95
Show file tree
Hide file tree
Showing 10 changed files with 89 additions and 64 deletions.
2 changes: 1 addition & 1 deletion src/xpra/client/ui_client_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
from xpra.codecs.loader import codec_versions, has_codec, get_codec, PREFERED_ENCODING_ORDER, ALL_NEW_ENCODING_NAMES_TO_OLD, OLD_ENCODING_NAMES_TO_NEW
from xpra.codecs.video_helper import getVideoHelper, ALL_VIDEO_DECODER_OPTIONS, NO_GFX_CSC_OPTIONS
from xpra.simple_stats import std_unit
from xpra.net.protocol import Compressed, use_lz4
from xpra.net.compression import Compressed, use_lz4
from xpra.daemon_thread import make_daemon_thread
from xpra.os_util import thread, Queue, os_info, platform_name, get_machine_id, get_user_uuid, bytestostr
from xpra.util import nn, nonl, std, AtomicInteger, AdHocStruct, log_screen_sizes, typedict
Expand Down
2 changes: 1 addition & 1 deletion src/xpra/clipboard/clipboard_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
from xpra.gtk_common.gobject_util import n_arg_signal
from xpra.gtk_common.gtk_util import GetClipboard, PROPERTY_CHANGE_MASK
from xpra.gtk_common.nested_main import NestedMainLoop
from xpra.net.protocol import compressed_wrapper
from xpra.net.compression import compressed_wrapper


MAX_CLIPBOARD_PACKET_SIZE = 256*1024
Expand Down
78 changes: 78 additions & 0 deletions src/xpra/net/compression.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
# This file is part of Xpra.
# Copyright (C) 2011-2014 Antoine Martin <[email protected]>
# Copyright (C) 2008, 2009, 2010 Nathaniel Smith <[email protected]>
# Xpra is released under the terms of the GNU GPL v2, or, at your option, any
# later version. See the file COPYING for details.

import os
import sys
from zlib import compress

from xpra.log import Logger
log = Logger("network", "protocol")
debug = log.debug


ZLIB_FLAG = 0x00
LZ4_FLAG = 0x10


try:
from xpra.os_util import builtins
_memoryview = builtins.__dict__.get("memoryview")
from lz4 import LZ4_compress, LZ4_uncompress #@UnresolvedImport
has_lz4 = True
def lz4_compress(packet, level):
if _memoryview and isinstance(packet, _memoryview):
packet = packet.tobytes()
return level + LZ4_FLAG, LZ4_compress(packet)
except Exception, e:
log("lz4 not found: %s", e)
LZ4_compress, LZ4_uncompress = None, None
has_lz4 = False
def lz4_compress(packet, level):
raise Exception("lz4 is not supported!")
use_lz4 = has_lz4 and os.environ.get("XPRA_USE_LZ4", "1")=="1"


#stupid python version breakage:
if sys.version > '3':
long = int #@ReservedAssignment
unicode = str #@ReservedAssignment
def zcompress(packet, level):
if type(packet)!=bytes:
packet = bytes(packet, 'UTF-8')
return level + ZLIB_FLAG, compress(packet, level)
else:
def zcompress(packet, level):
return level + ZLIB_FLAG, compress(str(packet), level)


class Compressed(object):
def __init__(self, datatype, data):
self.datatype = datatype
self.data = data
def __len__(self):
return len(self.data)
def __repr__(self):
return "Compressed(%s: %s bytes)" % (self.datatype, len(self.data))

class LevelCompressed(Compressed):
def __init__(self, datatype, data, level, algo):
Compressed.__init__(self, datatype, data)
self.algorithm = algo
self.level = level
def __len__(self):
return len(self.data)
def __repr__(self):
return "LevelCompressed(%s: %s bytes as %s/%s)" % (self.datatype, len(self.data), self.algorithm, self.level)

def compressed_wrapper(datatype, data, level=5, lz4=False):
if lz4:
assert use_lz4, "cannot use lz4"
algo = "lz4"
cl, cdata = lz4_compress(data, level & LZ4_FLAG)
else:
algo = "zlib"
cl, cdata = zcompress(data, level)
return LevelCompressed(datatype, cdata, cl, algo)
58 changes: 2 additions & 56 deletions src/xpra/net/protocol.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,14 @@
import binascii
from threading import Lock

ZLIB_FLAG = 0x00
LZ4_FLAG = 0x10


from xpra.log import Logger
log = Logger("network", "protocol")
debug = log.debug
from xpra.os_util import Queue, strtobytes, get_hex_uuid
from xpra.util import repr_ellipsized
from xpra.net.bytestreams import ABORT
from xpra.net.compression import zcompress, has_lz4, LZ4_FLAG, lz4_compress, LZ4_uncompress, Compressed, LevelCompressed


try:
Expand All @@ -36,22 +34,7 @@
log("pycrypto is missing: %s", e)


from zlib import compress, decompress
try:
from xpra.os_util import builtins
_memoryview = builtins.__dict__.get("memoryview")
from lz4 import LZ4_compress, LZ4_uncompress #@UnresolvedImport
has_lz4 = True
def lz4_compress(packet, level):
if _memoryview and isinstance(packet, _memoryview):
packet = packet.tobytes()
return level + LZ4_FLAG, LZ4_compress(packet)
except Exception, e:
log("lz4 not found: %s", e)
LZ4_compress, LZ4_uncompress = None, None
has_lz4 = False
def lz4_compress(packet, level):
raise Exception("lz4 is not supported!")
from zlib import decompress
use_lz4 = has_lz4 and os.environ.get("XPRA_USE_LZ4", "1")=="1"

rencode_dumps, rencode_loads, rencode_version = None, None, None
Expand Down Expand Up @@ -100,13 +83,6 @@ def lz4_compress(packet, level):
if sys.version > '3':
long = int #@ReservedAssignment
unicode = str #@ReservedAssignment
def zcompress(packet, level):
if type(packet)!=bytes:
packet = bytes(packet, 'UTF-8')
return level + ZLIB_FLAG, compress(packet, level)
else:
def zcompress(packet, level):
return level + ZLIB_FLAG, compress(str(packet), level)

if sys.version_info[:2]>=(2,5):
def unpack_header(buf):
Expand Down Expand Up @@ -198,36 +174,6 @@ class ConnectionClosedException(Exception):
pass


class Compressed(object):
def __init__(self, datatype, data):
self.datatype = datatype
self.data = data
def __len__(self):
return len(self.data)
def __repr__(self):
return "Compressed(%s: %s bytes)" % (self.datatype, len(self.data))

class LevelCompressed(Compressed):
def __init__(self, datatype, data, level, algo):
Compressed.__init__(self, datatype, data)
self.algorithm = algo
self.level = level
def __len__(self):
return len(self.data)
def __repr__(self):
return "LevelCompressed(%s: %s bytes as %s/%s)" % (self.datatype, len(self.data), self.algorithm, self.level)

def compressed_wrapper(datatype, data, level=5, lz4=False):
if lz4:
assert use_lz4, "cannot use lz4"
algo = "lz4"
cl, cdata = lz4_compress(data, level & LZ4_FLAG)
else:
algo = "zlib"
cl, cdata = zcompress(data, level)
return LevelCompressed(datatype, cdata, cl, algo)


class Protocol(object):
CONNECTION_LOST = "connection-lost"
GIBBERISH = "gibberish"
Expand Down
2 changes: 1 addition & 1 deletion src/xpra/server/picture_encode.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
from xpra.log import Logger
log = Logger("window", "encoding")

from xpra.net.protocol import compressed_wrapper, Compressed, use_lz4
from xpra.net.compression import compressed_wrapper, Compressed, use_lz4
try:
from xpra.codecs.argb.argb import bgra_to_rgb, bgra_to_rgba, argb_to_rgb, argb_to_rgba #@UnresolvedImport
except Exception, e:
Expand Down
3 changes: 2 additions & 1 deletion src/xpra/server/proxy_instance_process.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@

from xpra.server.server_core import get_server_info, get_thread_info
from xpra.scripts.server import deadly_signal
from xpra.net.protocol import Protocol, Compressed, compressed_wrapper, new_cipher_caps, get_network_caps
from xpra.net.compression import Compressed, compressed_wrapper
from xpra.net.protocol import Protocol, new_cipher_caps, get_network_caps
from xpra.codecs.image_wrapper import ImageWrapper
from xpra.codecs.video_helper import getVideoHelper, PREFERRED_ENCODER_ORDER
from xpra.os_util import Queue, SIGNAMES
Expand Down
2 changes: 1 addition & 1 deletion src/xpra/server/shadow_server_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
from xpra.log import Logger
log = Logger("shadow")

from xpra.net.protocol import Compressed
from xpra.net.compression import Compressed
from xpra.server.batch_config import DamageBatchConfig
from xpra.util import AdHocStruct, prettify_plug_name

Expand Down
2 changes: 1 addition & 1 deletion src/xpra/server/source.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
from xpra.codecs.loader import get_codec, has_codec, OLD_ENCODING_NAMES_TO_NEW, NEW_ENCODING_NAMES_TO_OLD
from xpra.codecs.video_helper import getVideoHelper
from xpra.codecs.codec_constants import codec_spec
from xpra.net.protocol import compressed_wrapper, Compressed
from xpra.net.compression import compressed_wrapper, Compressed
from xpra.daemon_thread import make_daemon_thread
from xpra.os_util import platform_name, StringIOClass, thread, Queue, get_machine_id, get_user_uuid
from xpra.server.background_worker import add_work_item
Expand Down
2 changes: 1 addition & 1 deletion src/xpra/server/window_video_source.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import operator
from threading import Lock

from xpra.net.protocol import Compressed
from xpra.net.compression import Compressed
from xpra.codecs.codec_constants import get_avutil_enum_from_colorspace, get_subsampling_divs, get_default_csc_modes, \
TransientCodecException, RGB_FORMATS, PIXEL_SUBSAMPLING, LOSSY_PIXEL_FORMATS
from xpra.server.window_source import WindowSource, MAX_PIXELS_PREFER_RGB, STRICT_MODE
Expand Down
2 changes: 1 addition & 1 deletion src/xpra/x11/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@
from xpra.util import nonl
from xpra.os_util import StringIOClass
from xpra.x11.x11_server_base import X11ServerBase, mouselog
from xpra.net.protocol import compressed_wrapper, Compressed
from xpra.net.compression import compressed_wrapper, Compressed


class DesktopManager(gtk.Widget):
Expand Down

0 comments on commit d2e9c95

Please sign in to comment.