title: Detect when TCP is congested python twisted socket server
body: <p>I'm working on a realtime MMO game, and have a working TCP server (along with game client), but now I'm <strong>considering using UDP</strong> for constantly updating other player's postions(to greatly reduce random game stuffer from TCP congestion control!) 
  I'd love some help from people smarter than me in this stuff (I'm new to python/twisted, and couldn't find this info elsewhere ;)  )</p>
  
  <p><strong>Currently, my server accepts connections with a simple Twisted Protocol. Eg.</strong></p>
  
  <pre><code>''' TCP reciever '''
  class TCPProtocol(Protocol):
  
      def connectionMade(self):
          #add to list of connected clients
          factory.clients.append(self)
  
      def dataReceived(self, data):
          pass
  
  
  #setup factory and TCP protocol class
  factory = Factory()
  factory.protocol = TCPProtocol
  factory.clients = []
  reactor.listenTCP(1959, factory)
  </code></pre>
  
  <p><strong>@@@@@@@@@@@@@@   UPDATE @@@@@@@@@@@@@ : 
  How can I implement congestion checking for each Protocol instance seperately? Please start me off with something, inside the code sample below: (where it says 'HELP HERE PLEASE'!)</strong>
  Am I thinking of this wrongly? Any guidance would be awesome, thanks!</p>
  
  <pre><code>from twisted.internet.protocol import DatagramProtocol
  from twisted.internet import reactor
  
  KServerIP='127.0.0.1'
  KServerPortTCP=8000
  #KClientPortUDP=7055
  
  ''' TCP reciever '''
  class TCPProtocol(Protocol):
  
      def connectionMade(self):
          #add to list of connected clients
          factory.clients.append(self)
  
          #set client variables (simplified)
          self.pos_x=100
          self.pos_y=100
  
          #add to game room (to recieve updates)
          mainGameRoom.clientsInRoom.append(self)
  
      def dataReceived(self, data):
          pass
  
      #send custom byte message to client (I have special class to read it)
      def sendMessageToClient(self, message, isUpdate):
  
  ''' @@@@@@@@@@@@@@@@@  HELP HERE PLEASE!  @@@@@@@@@@@      
          if isUpdate and (CHECK IF CLIENT IS CONGESTED??? )
              return  (and send next update when not congested)'''
       '''@@@@@@@@@@@@@@@@@  HELP HERE PLEASE!  @@@@@@@@@@@   '''
  
          #if not congested, write update!
          msgLen = pack('!I', len(message.data))
          self.transport.write(msgLen) #add length before message
          self.transport.write(message.data)
  
  #simplified version of my game room
  #this room runs the game, and clients recieve pos updates for 
  #everyone in this room (up to 50 people)
  dt_gameloop=1.0/60 #loop time difference
  dt_sendClientUpdate=0.1 #update intervar
  class GameRoom(object):
      #room constants
      room_w=1000
      room_h=1000
  
      def __init__(self):
          super(GameRoom, self).__init__()
          #schedule main game loop
          l=task.LoopingCall(self.gameLoop)
          l.start(dt_gameloop) # call every X seconds
          #schedule users update loop
          l=task.LoopingCall(self.sendAllClientsPosUpdate)
          l.start(dt_sendClientUpdate) # call every X seconds
  
      def gameLoop(self):
          #game logic runs here (60 times a second),  EG.
          for anUpdateClient in self.clientsInRoom:
              anUpdateClient.pos_x+=10
              anUpdateClient.pos_y+=10
  
      #send position update every 0.1 seconds, 
      #send all player positions to all clients
      def sendAllClientsPosUpdate(self):
  
          message = MessageWriter()
          message.writeByte(MessageGameLoopUpdate) #message type
  
          #create one byte message containing info for all players
          message.writeInt(len(self.clientsInRoom)) #num items to read
          for aClient in self.clientsInRoom:
              message.writeInt(aClient.ID)
              message.writeFloat( aCell.pos_x )#pos
              message.writeFloat( aCell.pos_y )
  
          #send message to all clients
          for aClient in self.clientsInRoom:
              aClient.sendMessageToClient(message, True)
  
  #setup factory and TCP protocol class
  factory = Factory()
  factory.protocol = TCPProtocol
  factory.clients = []
  reactor.listenTCP(KServerPortTCP, factory)
  
  #simple example, one game room
  mainGameRoom=GameRoom()
  
  
  print "Server started..."
  reactor.run()
  </code></pre>