Source

udt4twisted / examples / client.py

Full commit
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

# startup()
# s = UdtSocket(AF_INET, SOCK_DGRAM, AI_PASSIVE)
# print s.UDTSOCKET
# #s.setblocking(True)
# s.connect(("127.0.0.1", 5000))
# try:
#     while True:
#         mess = s.recvmsg(1000)
#         print 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()