Chinaunix首页 | 论坛 | 博客
  • 博客访问: 55964
  • 博文数量: 18
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 145
  • 用 户 组: 普通用户
  • 注册时间: 2017-02-03 22:58
文章分类

全部博文(18)

文章存档

2017年(18)

我的朋友

分类: Python/Ruby

2017-02-14 22:16:02




点击(此处)折叠或打开

  1. #coding:utf-8
  2. '''
  3. TLU-Location: server

  4. Created on 2017年2月14日

  5. @author: erain
  6. '''
  7. from ctypes import *
  8. import struct
  9. import select
  10. import socket
  11. import threading
  12. import binascii
  13. from time import sleep
  14. from Queue import Queue


  15. TLU_LOCATION_SERVER_LISTION_MAX=10
  16. TLU_LOCATION_QUEUE_MAX =51200 #50k



  17. class NetCommu_Header(Structure):
  18.      
  19.     _fields_=[
  20.                ('pkt_len',c_uint16),
  21.                ('dev_id',c_uint32),
  22.                ('pkt_type',c_uint8),
  23.                ('flags',c_uint8),
  24.                ('dev_type',c_uint8),
  25.                ]
  26.     
  27.     def get_format(self):
  28.         return '!'+'HIBBB'
  29.     def get_len(self):
  30.         return struct.calcsize(self.get_format())
  31.     def do_pack(self):
  32.         header=struct.pack(self.get_format(),self.pkt_len,self.dev_id,
  33.                     self.pkt_type,self.flags,self.dev_type)
  34.         return header
  35.     def do_unpack(self,header):
  36.         self.pkt_len,self.dev_id,self.pkt_type,self.flags,self.dev_type=struct.unpack(self.get_format(),header)
  37.     def do_showHeader(self):
  38.         print 'pkt_len = %x'%self.pkt_len
  39.         print 'dev_id = %x'%self.dev_id
  40.         print 'pkt_type = %x'%self.pkt_type
  41.         print 'flags = %x'%self.flags
  42.         print 'dev_types = %x'%self.dev_type


  43.         
  44. class TLU_LocationServer():
  45.     def __init__(self,ip='192.168.2.244',port=10000):
  46.         
  47.         self.OnLineClient=[]

  48.         
  49.         self.serv_sock=socket.socket(socket.AF_INET,socket.SOCK_STREAM)
  50.         self.serv_sock.bind((ip,port))
  51.         self.serv_sock.setblocking(False)
  52.         self.serv_sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, True)
  53.         self.serv_sock.listen(TLU_LOCATION_SERVER_LISTION_MAX)
  54.         self.serv_accept_thread=threading.Thread(target=self.server_accept_thread)
  55.         self.serv_accept_thread.start()
  56.         
  57.     def server_accept_thread(self):
  58.         w_list=[]
  59.         r_list=[]
  60.         err_list=[]
  61.         
  62.         r_list.append(self.serv_sock)
  63.         
  64.         
  65.         while True:
  66.             in_list,out_list,err_list=select.select(r_list, w_list, err_list,1)
  67.             
  68.             for s in in_list:
  69.                 if s is self.serv_sock:
  70.                     sock_clt,addr=s.accept()
  71. # print sock_clt
  72. # print addr
  73.                     queue=Queue(maxsize=TLU_LOCATION_QUEUE_MAX)
  74.                     threading.Thread(target=self.server_recv_thread,args=(sock_clt,queue,)).start()
  75.                     threading.Thread(target=self.do_msg_proccess,args=(sock_clt, queue,)).start()
  76. #
  77. # print sock_clt
  78. # print addr
  79. # sleep(1)
  80.         pass
  81.     
  82.     def server_recv_thread(self,sock,queue):
  83.         
  84.         print sock
  85.         self.OnLineClient.append(sock)
  86.         rlist=[]
  87.         wlist=[]
  88.         errlist=[]
  89.         rlist.append(sock)
  90.         isAlive=True
  91.         while isAlive:
  92.             in_list,out_list,err_list=select.select(rlist, wlist, errlist)
  93.             for s in in_list:
  94.                 if s is sock:
  95.                     data=s.recv(1024)
  96.                     if data:
  97. # print data
  98.                         if not queue.full():
  99.                             queue.put(data)
  100.                         #存到队列
  101.                     else:
  102.                         s.close()
  103.                         self.OnLineClient.remove(s)
  104.                         isAlive=False
  105.                         
  106.     def do_msg_proccess(self,sock,queue):
  107.         isRun=True
  108.         while isRun:
  109.             if not queue.empty():
  110.                 data=queue.get()
  111. # ============================================================
  112. # 此处做协议解析
  113. #============================================================
  114.                 print sock,data
  115.             else:
  116. # print "no data"
  117.                 sleep(0.1)
  118.             if sock not in self.OnLineClient:
  119.                 print "sock closed"
  120.                 isRun=False
  121.             
  122.                 
  123.                     
  124. '''
  125.     测试Header 数据结构
  126. '''
  127. def testHeader():
  128.     head=NetCommu_Header()
  129.     head.pkt_len=0x1234
  130.     head.dev_id=0x56784329
  131.     head.pkt_type=0x30
  132.     head.flags=0x1
  133.     head.dev_type=0xff
  134.     print binascii.b2a_hex( head.do_pack())
  135.     
  136.     header=binascii.a2b_hex('123456789123456789')
  137.     print len(header)
  138.     head.do_unpack(header)
  139.     head.do_showHeader()
  140. if __name__=='__main__':
  141.     
  142.     testHeader()
  143.     
  144.     serv=TLU_LocationServer('192.168.1.111',20000)
  145.     print "TLU Location Startting"

阅读(1239) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~