Esempio n. 1
0
def handle(uid):
    # client = TcpClient(Box, '192.168.1.67', 29000, timeout=None)
    client = TcpClient(Box, '115.28.224.64', 29000, timeout=None)
    client.connect()

    box = Box()
    box.cmd = 1
    box.set_json(dict(uid=uid))

    client.write(box)

    box = Box()
    box.cmd = 2

    box.set_json(dict(uid=uid))

    client.write(box)

    t1 = time.time()

    while True:
        # 阻塞
        box = client.read()
        print 'time past: ', time.time() - t1
        print box
        if not box:
            print 'server closed'
            break
Esempio n. 2
0
    def dataReceived(self, data):
        """
        当数据接受到时
        :param data:
        :return:
        """
        self._read_buffer += data

        while self._read_buffer:
            # 因为box后面还是要用的
            box = Box()
            ret = box.unpack(self._read_buffer)
            if ret == 0:
                # 说明要继续收
                return
            elif ret > 0:
                # 收好了
                self._read_buffer = self._read_buffer[ret:]
                safe_call(self._on_read_complete, box)
                continue
            else:
                # 数据已经混乱了,全部丢弃
                logger.error('buffer invalid. ret: %d, read_buffer: %r', ret,
                             self._read_buffer)
                self._read_buffer = ''
                return
Esempio n. 3
0
    def dataReceived(self, data):
        """
        当数据接受到时
        :param data:
        :return:
        """
        self._read_buffer += data

        while self._read_buffer:
            # 因为box后面还是要用的
            box = Box()
            ret = box.unpack(self._read_buffer)
            if ret == 0:
                # 说明要继续收
                return
            elif ret > 0:
                # 收好了
                self._read_buffer = self._read_buffer[ret:]
                safe_call(self._on_read_complete, box)
                continue
            else:
                # 数据已经混乱了,全部丢弃
                logger.error(
                    "buffer invalid. proxy: %s, ret: %d, read_buffer: %r", self.factory.proxy, ret, self._read_buffer
                )
                self._read_buffer = ""
                return
Esempio n. 4
0
File: proxy.py Progetto: ljdog/burst
 def _on_workers_reload_over(self):
     """
     当workers reload之后的操作
     需要给master通知,让master替换掉workers
     :return:
     """
     if self.master_conn and self.master_conn.transport:
         box = Box(dict(cmd=constants.CMD_MASTER_REPLACE_WORKERS))
         self.master_conn.transport.write(box.pack())
Esempio n. 5
0
 def _on_workers_reload_over(self):
     """
     当workers reload之后的操作
     需要给master通知,让master替换掉workers
     :return:
     """
     if self.master_conn and self.master_conn.transport:
         box = Box(dict(
             cmd=constants.CMD_MASTER_REPLACE_WORKERS
         ))
         self.master_conn.transport.write(box.pack())
Esempio n. 6
0
def index(request):
    print '\n', 'req', request.box
    request.box.ret = 100
    request.box.body = request.box.body[::-1]

    print 'rsp', request.box, '\n'
    request.write(request.box)

    box = Box()
    box.cmd = 15
    print 'event', box, '\n'
    request.write(box)
Esempio n. 7
0
def handle(uid):
    # client = TcpClient(Box, '192.168.1.67', 29000, timeout=None)
    client = TcpClient(Box, '115.28.224.64', 29000, timeout=None)
    client.connect()

    box = Box()
    box.cmd = 1
    box.set_json(dict(
        uid=uid
    ))

    client.write(box)

    box = Box()
    box.cmd = 2

    box.set_json(dict(
        uid=uid
    ))

    client.write(box)

    t1 = time.time()

    while True:
        # 阻塞
        box = client.read()
        print 'time past: ', time.time() - t1
        print box
        if not box:
            print 'server closed'
            break
Esempio n. 8
0
def main():
    # client = TcpClient(Box, '192.168.1.67', 29000, timeout=None)
    client = TcpClient(Box, '115.28.224.64', 29000, timeout=None)
    client.connect()

    box = Box()
    box.cmd = 3

    client.write(box)

    t1 = time.time()

    while True:
        # 阻塞
        box = client.read()
        print 'time past: ', time.time() - t1
        print box
        if not box:
            print 'server closed'
            break
Esempio n. 9
0
def handle(uid):
    client = TcpClient(Box, '127.0.0.1', 29000, timeout=None)
    client.connect()

    box = Box()
    box.cmd = 100
    box.set_json(dict(
        uid=uid
    ))

    client.write(box)

    t1 = time.time()

    while True:
        # 阻塞
        box = client.read()
        print 'time past: ', time.time() - t1
        print box
        if not box:
            print 'server closed'
            break
Esempio n. 10
0
def handle(uid):
    client = TcpClient(Box,
                       config.GATEWAY_OUTER_HOST,
                       config.GATEWAY_OUTER_PORT,
                       timeout=None)
    client.connect()

    box = Box()
    box.cmd = 2
    # box.cmd = 999
    box.set_json(dict(uid=uid))

    client.write(box)

    t1 = time.time()

    while True:
        # 阻塞
        box = client.read()
        print 'time past: ', time.time() - t1
        print box
        if not box:
            print 'server closed'
            break
Esempio n. 11
0
def handle():
    trigger = Trigger(forwarder_input_address_list=config.FORWARDER_INPUT_ADDRESS_LIST)

    box = Box()
    box.cmd = 3

    trigger.write_to_users([
        [(constants.CONNS_AUTHED,), box]
    ])

    box2 = Box()
    box2.cmd = 5
    trigger.write_to_worker(
        box2
    )
Esempio n. 12
0
def index(request):
    print request.box

    box = Box()
    # box = request.box
    box.ret = 100
    box.body = "wokao" * 1000
    print box.header_len
    print box.packet_len

    print box

    # request.write(box.pack())
    # return

    box.version = 219
    box.flag = 199
    buf = box.pack()
    print '1'
    request.write(buf[:10])
    time.sleep(2)
    print '2'
    request.write(buf[10:])
Esempio n. 13
0
            def spawn_read():
                message = self.stream.read_with_checker(Box().check)
                # print "message, len: %s, content: %r" % (len(message), message)

                if message:
                    req_box = Box(message)
                    rsp_box = Box()
                    print rsp_box
                    rsp_box.cmd = req_box.cmd
                    rsp_box.ret = 1000
                    rsp_box.version = 333
                    rsp_box.body = 'ok'

                    buf = rsp_box.pack()
                    self.stream.write(buf[:10])
                    self.stream.write(buf[10:])

                if self.stream.closed():
                    print 'client closed'
                    # 说明客户端断掉链接了
                    return
Esempio n. 14
0
# -*- coding: utf-8 -*-

from netkit.box import Box

box1 = Box()
print box1
print repr(box1.pack())

box2 = Box(box1.pack())

print box2
Esempio n. 15
0
# -*- coding: utf-8 -*-

import sys
sys.path.insert(0, '../../')

from netkit.contrib.tcp_client import TcpClient
from netkit.box import Box

import time

client = TcpClient(Box, '127.0.0.1', 9900, timeout=5)
client.connect()

box = Box()
box.cmd = 1
# box.cmd = 101
box.body = '我爱你'

client.write(box)

t1 = time.time()

while True:
    # 阻塞
    box = client.read()
    print 'time past: ', time.time() - t1
    print box
    if not box:
        print 'server closed'
        break
Esempio n. 16
0
import time
import logging
import socket

logger = logging.getLogger('netkit')
logger.addHandler(logging.StreamHandler())
logger.setLevel(logging.DEBUG)

address = ('127.0.0.1', 7777)
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect(address)

stream = Stream(s)

box = Box()
box.cmd = 32
box.body = '我爱你'

stream.write(box.pack())

while True:
    # 阻塞
    buf = stream.read_with_checker(Box().unpack)

    if buf:
        print Box(buf)

    if stream.closed():
        print 'server closed'
        break
Esempio n. 17
0
# -*- coding: utf-8 -*-

from netkit.box import Box

box1 = Box()
print box1
print repr(box1.pack())

box2 = Box()
box2.unpack(box1.pack())

print box2
Esempio n. 18
0
# -*- coding: utf-8 -*-

import sys

sys.path.insert(0, '../../')

import socket
from netkit.box import Box

sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)

box = Box()
box.cmd = 1
# box.cmd = 101
box.body = '我爱你'

sock.sendto(box.pack(), ('127.0.0.1', 9900))

while True:
    data, address = sock.recvfrom(1000)

    recv_box = Box()
    recv_box.unpack(data)

    print recv_box
Esempio n. 19
0
def write_to_users(request):
    tmp_box = Box()
    tmp_box.cmd = 99
    tmp_box.body = "bd"
    request.close_users([-2])
Esempio n. 20
0
# -*- coding: utf-8 -*-

from netkit.box import Box


box1 = Box()
box1.body = '我爱你'
print box1
print repr(box1.pack())
print box1.body

box2 = Box(box1.pack())
print box2
print box2.body
print box2.magic
Esempio n. 21
0
# -*- coding: utf-8 -*-

from netkit.box import Box


box1 = Box()
box1.body = '我爱你'
print box1
print repr(box1.pack())
print box1.body

box2 = Box()
box2.unpack(box1.pack())
print box2
print box2.body
print box2.magic
Esempio n. 22
0
# -*- coding: utf-8 -*-

from netkit.box import Box


box1 = Box()
box1.body = '我爱你'
print box1
print repr(box1.pack())
print box1.body

buf = box1.pack()

box2 = Box()
for i in range(0, len(buf)+1):
    tmp_buf = buf[0:i]

    if box2.unpack(tmp_buf) > 0:
        print box2
        break
Esempio n. 23
0
# -*- coding: utf-8 -*-

from netkit.box import Box


box1 = Box()
box1.values = dict(uin=1, name=u'我')
print box1
print repr(box1.pack())
print box1.body

buf = box1.pack()

box2 = Box()
for i in range(0, len(buf)+1):
    tmp_buf = buf[0:i]

    if box2.unpack(tmp_buf) > 0:
        print box2
        print box2.values
        break
Esempio n. 24
0
# -*- coding: utf-8 -*-

from netkit.box import Box

box1 = Box()
box1.body = '我爱你'
print box1
print repr(box1.pack())
print box1.body

buf = box1.pack()

box2 = Box()
for i in range(0, len(buf) + 1):
    tmp_buf = buf[0:i]

    if box2.unpack(tmp_buf) > 0:
        print box2
        break
Esempio n. 25
0
# -*- coding: utf-8 -*-

from netkit.box import Box


box1 = Box()
box1.set_json(dict(uin=1, name=u'我'))
print box1
print repr(box1.pack())
print box1.body

buf = box1.pack()

box2 = Box()
for i in range(0, len(buf)+1):
    tmp_buf = buf[0:i]

    if box2.unpack(tmp_buf) > 0:
        print box2
        print box2.get_json()
        break