From 230cf2f7feab697ddcb0394b07e9f6d4d0d6ecf7 Mon Sep 17 00:00:00 2001 From: speyrefitte Date: Thu, 19 Jun 2014 17:53:10 +0200 Subject: [PATCH] refactor examples and client factory --- README.md | 11 ++++- rdpy/display/qt.py | 3 +- rdpy/display/rle.py | 6 +++ rdpy/examples/client.py | 93 +++++++++++++++++++++++++++++++++++----- rdpy/protocol/rdp/rdp.py | 16 +++---- rdpy/protocol/rfb/rfb.py | 11 +---- 6 files changed, 106 insertions(+), 34 deletions(-) create mode 100644 rdpy/display/rle.py diff --git a/README.md b/README.md index e30f691..0ee9e87 100644 --- a/README.md +++ b/README.md @@ -1,2 +1,11 @@ -depend : python-qt4, python-twisted, python-qt4reactor +# RDPY Remote Desktop Protoc in Python + +## Requirements +* python2.7 +* python-twisted + +## Requirements for examples +* python-qt4 +* python-qt4reactor + this project is still in progress. diff --git a/rdpy/display/qt.py b/rdpy/display/qt.py index daa63c1..7ada0b1 100644 --- a/rdpy/display/qt.py +++ b/rdpy/display/qt.py @@ -34,7 +34,6 @@ class QAdaptor(object): @param e: qEvent ''' pass - class RfbAdaptor(RfbObserver, QAdaptor): ''' @@ -159,7 +158,7 @@ class QRemoteDesktop(QtGui.QWidget): #because we can update image only in paint #event function. When protocol receive image #we will stock into refresh list - #and in paiont event paint list of all refresh iomages + #and in paiont event paint list of all refresh images self._refresh = [] #bind mouse event self.setMouseTracking(True) diff --git a/rdpy/display/rle.py b/rdpy/display/rle.py new file mode 100644 index 0000000..25bc603 --- /dev/null +++ b/rdpy/display/rle.py @@ -0,0 +1,6 @@ +''' +@author: citronneur +@file: implement run length encoding algorithm use in RDP protocol to compress bit +@see: http://msdn.microsoft.com/en-us/library/dd240593.aspx +''' + diff --git a/rdpy/examples/client.py b/rdpy/examples/client.py index c68f52f..03febeb 100644 --- a/rdpy/examples/client.py +++ b/rdpy/examples/client.py @@ -11,6 +11,84 @@ from rdpy.display.qt import RDPAdaptor, RfbAdaptor, QRemoteDesktop from rdpy.protocol.rdp import rdp from rdpy.protocol.rfb import rfb +class RDPClientQtFactory(rdp.ClientFactory): + ''' + Factory create a RDP GUI client + ''' + def __init__(self): + ''' + ctor that init qt context and protocol needed + ''' + #create qt widget + self._w = QRemoteDesktop() + self._w.resize(1024, 800) + self._w.setWindowTitle('rdpyclient-rdp') + self._w.show() + #build protocol + rdp.ClientFactory.__init__(self, RDPAdaptor(self._w)) + + def startedConnecting(self, connector): + pass + + def clientConnectionLost(self, connector, reason): + ''' + connection lost event + @param connector: twisted connector use for rdp connection (use reconnect to restart connection) + @param reason: str use to advertise reason of lost connection + ''' + QtGui.QMessageBox.warning(self._w, "Warning", "Lost connection : %s"%reason) + reactor.stop() + app.exit() + + def clientConnectionFailed(self, connector, reason): + ''' + connection failed event + @param connector: twisted connector use for rdp connection (use reconnect to restart connection) + @param reason: str use to advertise reason of lost connection + ''' + QtGui.QMessageBox.warning(self._w, "Warning", "Connection failed : %s"%reason) + reactor.stop() + app.exit() + +class RFBClientQtFactory(rfb.ClientFactory): + ''' + Factory create a VNC GUI client + ''' + def __init__(self): + ''' + ctor that init qt context and protocol needed + ''' + #create qt widget + self._w = QRemoteDesktop() + self._w.resize(1024, 800) + self._w.setWindowTitle('rdpyclient-vnc') + self._w.show() + + rfb.ClientFactory.__init__(self, RfbAdaptor(self._w)) + + def startedConnecting(self, connector): + pass + + def clientConnectionLost(self, connector, reason): + ''' + connection lost event + @param connector: twisted connector use for vnc connection (use reconnect to restart connection) + @param reason: str use to advertise reason of lost connection + ''' + QtGui.QMessageBox.warning(self._w, "Warning", "Lost connection : %s"%reason) + reactor.stop() + app.exit() + + def clientConnectionFailed(self, connector, reason): + ''' + connection failed event + @param connector: twisted connector use for vnc connection (use reconnect to restart connection) + @param reason: str use to advertise reason of lost connection + ''' + QtGui.QMessageBox.warning(self._w, "Warning", "Connection failed : %s"%reason) + reactor.stop() + app.exit() + if __name__ == '__main__': #create application app = QtGui.QApplication(sys.argv) @@ -19,18 +97,13 @@ if __name__ == '__main__': import qt4reactor qt4reactor.install() - #create widget - w = QRemoteDesktop() - w.resize(1024, 800) - w.setWindowTitle('rdpyclient') - w.show() - if sys.argv[3] == 'rdp': - factory = rdp.Factory(RDPAdaptor(w)) + factory = RDPClientQtFactory() else: - factory = rfb.ClientFactory(RfbAdaptor(w)) + factory = RFBClientQtFactory() from twisted.internet import reactor reactor.connectTCP(sys.argv[1], int(sys.argv[2]), factory) - reactor.run() - sys.exit(app.exec_()) \ No newline at end of file + reactor.runReturn() + app.exec_() + reactor.stop() \ No newline at end of file diff --git a/rdpy/protocol/rdp/rdp.py b/rdpy/protocol/rdp/rdp.py index fe6abbf..04b7c78 100644 --- a/rdpy/protocol/rdp/rdp.py +++ b/rdpy/protocol/rdp/rdp.py @@ -33,30 +33,24 @@ class RDP(object): for rectangle in bitmapUpdateData.rectangles._array: observer.notifyBitmapUpdate(rectangle.destLeft.value, rectangle.destTop.value, rectangle.destRight.value, rectangle.destBottom.value, rectangle.width.value, rectangle.height.value, rectangle.bitsPerPixel.value, (rectangle.flags & pdu.BitmapFlag.BITMAP_COMPRESSION).value, rectangle.bitmapDataStream.value) -class Factory(protocol.Factory): +class ClientFactory(protocol.Factory): ''' Factory of Client RDP protocol ''' def __init__(self, observer): ''' ctor - @param observer: observer use by rdp protocol to handle events + @param observer: observer use by rdp protocol to handle events must implement RDPObserver ''' self._observer = observer def buildProtocol(self, addr): + ''' + Function call from twisted and build rdp protocol stack + ''' rdp = RDP() rdp.addObserver(self._observer) return tpkt.TPKT(tpdu.TPDU(mcs.MCS(pdu.PDU(LayerMode.CLIENT, rdp)))); - - def startedConnecting(self, connector): - print 'Started to connect.' - - def clientConnectionLost(self, connector, reason): - print 'Lost connection. Reason:', reason - - def clientConnectionFailed(self, connector, reason): - print 'Connection failed. Reason:', reason class RDPObserver(object): ''' diff --git a/rdpy/protocol/rfb/rfb.py b/rdpy/protocol/rfb/rfb.py index 6842953..dc8bfec 100644 --- a/rdpy/protocol/rfb/rfb.py +++ b/rdpy/protocol/rfb/rfb.py @@ -425,21 +425,12 @@ class ClientFactory(protocol.Factory): def buildProtocol(self, addr): ''' function call by twisted on connection - @param addr: adresse where client try to connect + @param addr: adress where client try to connect ''' protocol = Rfb(LayerMode.CLIENT) protocol.addObserver(self._observer) self._observer.setProtocol(protocol) return protocol - - def startedConnecting(self, connector): - print 'Started to connect.' - - def clientConnectionLost(self, connector, reason): - print 'Lost connection. Reason:', reason - - def clientConnectionFailed(self, connector, reason): - print 'Connection failed. Reason:', reason class RfbObserver(object): '''