1. Martin Gergov
  2. udt4twisted

Source

udt4twisted / examples / client.py

from udt4 import startup, cleanup, accept, UDT_EPOLL_IN, UDT_EPOLL_OUT, UDT_EPOLL_ERR
from socket import AF_INET, SOCK_DGRAM, AI_PASSIVE
import udt4
from udt4.pyudt import UdtSocket
import gc
from time import sleep

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()

# import sys
# sys.path.append("../")

# from udt4twisted import udtepollreactor
# udtepollreactor.install()

# from twisted.python import log
# from twisted.internet import reactor, defer
# from twisted.internet.protocol import DatagramProtocol
# from twisted.python import log

# log.startLogging(sys.stdout)


# class UDTClient(DatagramProtocol):

#     identifier = 0

#     def doSend1(self):
#         self.transport.write("?>!{0}".format(self.identifier),
#                              ("127.0.0.1", 5000))

#     def datagramReceived(self, data, (host, port)):
#         print "Received %r" % data
#         self.transport.write("bomb!!!{0}".format(self.identifier),
#                                                 ("127.0.0.1", 5000))

# udtclient = UDTClient()
# udtclient2 = UDTClient()
# udtclient2.identifier = 1
# print udtclient2.identifier
# reactor.connectUDT("127.0.0.1", 5000, udtclient)
# reactor.connectUDT("127.0.0.1", 5000, udtclient2)
# reactor.callLater(1, udtclient.doSend1)
# reactor.callLater(1, udtclient2.doSend1)
# reactor.run()

# 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
# from time import sleep 

# startup()
# s = UdtSocket(AF_INET, SOCK_DGRAM, AI_PASSIVE)
# print s.UDTSOCKET
# s.setblocking(False)
# s.connect_ex(("127.0.0.1", 5000))
# poller = UDTepoll()
# poller.add_usock(s.UDTSOCKET, UDT_EPOLL_IN)

# try:
#     while True:
#         sets = poller.wait(True, True, 1000, False, False)
#         #print sets
#         for socket in sets[0]:
#             if (socket.UDTSOCKET == s.UDTSOCKET.UDTSOCKET):
#                 print 
#                 try:
#                     mess = recvmsg(socket.UDTSOCKET, 200)
#                     print "READ:", socket, ";RECV:", mess
#                     sendmsg(socket.UDTSOCKET, mess, len(mess))
#                 except udt4.UDTException as ue:
#                     print "ERR:",ue
#                     poller.remove_usock(socket, UDT_EPOLL_IN)
#                     if ue[0] == 6002:
#                         poller.add_usock(socket, UDT_EPOLL_IN)
#             gc.collect()

# except KeyboardInterrupt:
#     exit("Bye!")
# poller.remove_usock(s.UDTSOCKET)
# cleanup()