java中定长报文补充与拆解_完成定长头报文的封装和解封

1.[代码]lmcc_common.py

#!/usr/bin/env python

"Common Tools module for LMCC project"

import struct

import ctypes

def fieldcode(fieldname):

"return field code by field name"

field_2_code = {}

field_2_code['SESSION_KEY'] = '2'

field_2_code['STATUS'] = '3'

field_2_code['USER_ID'] = '4'

field_2_code['NAS_IP'] = '5'

field_2_code['NAS_PORT'] = '6'

field_2_code['NAS_PORT_TYPE'] = '7'

field_2_code['SERVICE_TYPE'] = '8'

field_2_code['SERVICE_PORT'] = '9'

field_2_code['SERVICE_IP'] = '10'

field_2_code['SESSION_START_TIME'] = '11'

field_2_code['SESSION_STATUS'] = '12'

field_2_code['SESSION_TIME'] = '13'

field_2_code['MAX_CONNS'] = '14'

field_2_code['USER_TYPE'] = '15'

field_2_code['SESSION_ID'] = '16'

field_2_code['CALLER'] = '17'

field_2_code['CALLEE'] = '18'

field_2_code['RESERVED1'] = '19'

field_2_code['RESERVED2'] = '20'

field_2_code['RESERVED3'] = '21'

field_2_code['RESERVED4'] = '22'

field_2_code['LAST_ALIVE_TIME'] = '23'

return field_2_code.get(fieldname)

def htons(num):

return struct.pack('!H', num)

def htonl(num):

return struct.pack('!I', num)

def ntohs(data):

return struct.unpack('!H', data)[0]

def ntohl(data):

return struct.unpack('!I', data)[0]

def ntohb(data):

return struct.unpack('B', data)[0]

def wraptlv(tlvs):

"wrap tlv"

if len(tlvs) == 0:

return [0, '']

bodylen = 0

body = ''

for item in tlvs:

attrtype = int(item[0])

attrvalue = item[1]

attrlen = len(attrvalue)

formatstr1 = "%ds" % attrlen

buf = ctypes.create_string_buffer(attrlen + 3)

offset = 0

struct.pack_into("B", buf, offset, attrtype)

offset = offset + 1

struct.pack_into("!H", buf, offset, attrlen + 3)

offset = offset + 2

struct.pack_into(formatstr1, buf, offset, attrvalue)

body = body + buf.raw

bodylen = bodylen + len(buf.raw)

return [bodylen, body]

def unwrapfieldtlv(data):

"unwrap field tlv"

return data

def unwraprecordtlv(data):

"unwrap record tlv"

record = []

offset = 0

while offset < len(data):

attrtypedata = data[offset:offset + 1]

attrtype = ntohb(attrtypedata)

offset = offset + 1

attrlendata = data[offset:offset + 2]

attrlen = ntohs(attrlendata)

offset = offset + 2

if attrlen - 3 > 0:

attrvaluedata = data[offset:offset + attrlen - 3]

offset = offset + attrlen - 3

if attrtype == 0:

attrvalue = unwraprecordtlv(attrvaluedata)

else:

attrvalue = unwrapfieldtlv(attrvaluedata)

record.append((str(attrtype), attrvalue))

else:

record.append((str(attrtype), ''))

return record

def unwraptlv(data):

"unwrap tlv"

result = []

offset = 0

while offset < len(data):

attrtypedata = data[offset:offset + 1]

attrtype = ntohb(attrtypedata)

offset = offset + 1

attrlendata = data[offset:offset + 2]

attrlen = ntohs(attrlendata)

offset = offset + 2

if attrlen - 3 > 0:

attrvaluedata = data[offset:offset + attrlen - 3]

offset = offset + attrlen - 3

if attrtype == 0:

attrvalue = unwraprecordtlv(attrvaluedata)

else:

attrvalue = unwrapfieldtlv(attrvaluedata)

result.append((str(attrtype), attrvalue))

else:

result.append((str(attrtype), ''))

return result

def sendpacket(sock, serialno, msgtype, errcode, tlvs):

"wrap data packet and send it"

totollen = 0

bodylen = 0

offset = 0

body = ''

wraptlvdata = wraptlv(tlvs)

bodylen = wraptlvdata[0]

body = wraptlvdata[1]

totollen = bodylen + 12

formatstr = "%ds" % bodylen

data = ctypes.create_string_buffer(totollen)

offset = 0

struct.pack_into("!I", data, offset, totollen)

offset = offset + 4

struct.pack_into("!I", data, offset, serialno)

offset = offset + 4

struct.pack_into("B", data, offset, msgtype)

offset = offset + 1

struct.pack_into("!H", data, offset, errcode)

offset = offset + 2

struct.pack_into("B", data, offset, 0)

offset = offset + 1

if bodylen > 0:

struct.pack_into(formatstr, data, offset, body)

sock.send(data)

def receivepacket(sock):

"receive packet and unwrap it"

data = sock.recv(4)

totallen = ntohl(data)

data = sock.recv(4)

serialno = ntohl(data)

data = sock.recv(1)

msgtype = ntohb(data)

data = sock.recv(2)

errcode = ntohs(data)

data = sock.recv(1)

attrnum = ntohb(data)

bodylen = totallen - 12

if bodylen > 0:

data = sock.recv(bodylen)

wraptlvdata = unwraptlv(data)

return [serialno, msgtype, errcode, attrnum, wraptlvdata]

return [serialno, msgtype, errcode, attrnum]

if __name__ == '__main__':

print 'test!'

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值