from twisted.internet.protocol import ClientFactory from twisted.protocols.basic import LineReceiver from twisted.internet import reactor, task
import sys import os import struct
from netMsg import *
headLen = struct.calcsize(Inn_HEAD)
def genfunc( data, msgType ): funcText = "self.body = struct.unpack(MSG_%i, data[:struct.calcsize(MSG_%i)])" return funcText%( msgType, msgType ) class CM_Client(LineReceiver): def __init__(self): self.buf = '' self.complete = True def request(self): self.transport.write(struct.pack(MSG_111, 6, 111, 1, 0)) def connectionMade(self): self.setRawMode() self.request_task = task.LoopingCall(self.request) self.request_task.start(3.0) # call every 3 second def rawDataReceived(self, data): self.buf += data self.netDataProc(self.buf) def netDataProc(self, data): print map(ord,self.buf) if self.complete: self.recvMsgHead(self.buf) self.recvMsgBody(self.buf) def recvMsgHead(self, data): if len(self.buf) >= headLen: self.head = struct.unpack(Inn_HEAD,self.buf[0:headLen]) else: self.head = None
def recvMsgBody(self, data): if self.head == None: return self.complete = False msgLen, msgType =self.head[0:2] print 'msgLen =%d | headLen = %d'%(msgLen, headLen) if len(self.buf) >= msgLen: try: exec( genfunc( self.buf, msgType ) ) self.buf = self.buf[msgLen:] self.complete = True self.dealMsg(self.body) self.netDataProc(self.buf) except NameError: print "类型%d没有定义无法读出"% msgType def dealMsg(self,msg): print 'Msg:', msg if(msg[1] == 112): self.request_task.stop() if(msg[1] == 101): pass if(msg[1] == 102): pass if(msg[1] == 121): pass
class CM_ClientFactory(ClientFactory): protocol = CM_Client
def clientConnectionFailed(self, connector, reason): print 'connection failed:', reason.getErrorMessage() reactor.stop()
def clientConnectionLost(self, connector, reason): print 'connection lost:', reason.getErrorMessage() reactor.stop()
def main(): reactor.connectTCP('localhost', 10050, CM_ClientFactory()) reactor.run()
if __name__ == '__main__': main()
|