Source

udt4twisted / examples / client.py

Full commit
from socket import AF_INET, SOCK_DGRAM, AI_PASSIVE
from udt4 import socket, listen, UDTepoll, bind, recvmsg, UDTException
from udt4 import startup, cleanup, accept, UDT_EPOLL_IN, UDT_EPOLL_OUT, UDT_EPOLL_ERR
from udt4 import EASYNCRCV, sendmsg
from socket import AF_INET, SOCK_DGRAM, AI_PASSIVE
import udt4
from udt4.pyudt import UdtSocket
import gc
import random
from time import sleep

ok = True
startup()
poller = UDTepoll()
s = UdtSocket(AF_INET, SOCK_DGRAM, AI_PASSIVE)
s.setblocking(False)
print s.UDTSOCKET
counter = 0
poller.add_usock(s.UDTSOCKET, UDT_EPOLL_IN)

# try:
#     while 1:
#         sock, addr = s.accept()
#         sleep(20)
        
# except KeyboardInterrupt:
#     exit("Bye!")
connected = False
try:
    while True:
        print "---------------------------------------------"
        sets = [[], []]
        try:
            sets = poller.wait(True, True, 1000, False, False)
        except UDTException as ue:
            if ue[0] == 6003:
                pass
        print sets

        if connected:
            poller.add_usock(s.UDTSOCKET , UDT_EPOLL_OUT)
            connected = False

        if ok:
            try:
                connectResult = s.connect_ex(("127.0.0.1", 5000))
            except udt.UDTException as ue:
                connectResult = ue[0]

            if connectResult == 0:
                print "CONNECTED!"
                connected = True
                ok = False

        for socket in sets[0]:
            if (socket.UDTSOCKET == s.UDTSOCKET.UDTSOCKET):
                try:
                    mess = recvmsg(socket, 100)
                    print "READ:", socket,";", "RECV:", mess
                    if counter < 100:
                        poller.add_usock(socket , UDT_EPOLL_OUT)
                    #sendmsg(socket.UDTSOCKET, mess, len(mess))
                except udt4.UDTException as ue:
                    print "ERR:",ue
                    if ue[0] == 6002:
                        poller.remove_usock(socket)
                        poller.add_usock(socket, UDT_EPOLL_IN)
                        pass
            gc.collect()

        for socket in sets[1]:
            if s.UDTSOCKET.UDTSOCKET == socket.UDTSOCKET:
                mess = "HELLO!"
                mess = sendmsg(socket, mess, len(mess))
                poller.remove_usock(socket)
                poller.add_usock(socket, UDT_EPOLL_IN)

                counter += 1

            gc.collect()
except KeyboardInterrupt:
    poller.remove_usock(s.UDTSOCKET)
    s.close()
    cleanup()
    exit("Bye!")



# startup()
# s = UdtSocket(AF_INET, SOCK_DGRAM, AI_PASSIVE)
# print s.UDTSOCKET
# s.setblocking(True)
# s.setsockopt(udt4.UDT_RCVTIMEO, 1000)
# s.connect(("127.0.0.1", 5000))
# s.sendmsg("HELLO!", 7)
# try:
#     while True:
#         try:
#             mess = s.recvmsg(1000)
#         except udt4.UDTException as ue:
#             if ue[0] == 6003:
#                 pass
#             else:
#                 raise
#         else:
#             print repr(mess)
#             s.sendmsg(mess)
#             pass
# except udt4.UDTException as ue:
#     print ue
#     exit("Bye!")
# except KeyboardInterrupt:
#     pass

# print "NEXT"
# s.sendmsg("HELLO!", 7)

# try:
#     while True:
#         try:
#             mess = s.recvmsg(1000)
#         except udt4.UDTException as ue:
#             if ue[0] == 6003:
#                 print "TIMEOUT"
#                 pass
#             else:
#                 raise
#         else:
#             print repr(mess)
#             s.sendmsg(mess)
# except udt4.UDTException as ue:
#     print ue
#     exit("Bye!")

# except KeyboardInterrupt:
#     exit("Bye!")
# cleanup()