[Twisted-Python] raw socket IWriteDescriptor

Hi, I'm trying to implement an IWriteDescriptor using raw sockets. So far this is what I wrote... but it doesn't work: https://github.com/david415/hushTCP/blob/master/hush_writer.py Am I doing something obviously wrong here? I certainly am able to use raw socket + scapy correctly without Twisted. Cheers! David

On 10/01/2013 08:30 AM, David Stainton wrote:
Hi,
I'm trying to implement an IWriteDescriptor using raw sockets. So far this is what I wrote... but it doesn't work: https://github.com/david415/hushTCP/blob/master/hush_writer.py
Am I doing something obviously wrong here?
I don't see you trying to add the raw socket to the reactor anywhere; typically you'll want to do at least reactor.addWriter(hush), but for a proper implementation there's more - see the IReactorFDSet docs. You may want to look at the udp code in Twisted to see how this is done under the hood (although TBH it can be hard to follow!)

I just now realized that my fileno() method was incorrect. It now returns the descriptor instead of the python socket: return self.socket.fileno() I add the socket to the reactor in the constructor like this: reactor.addWriter(self) BTW is that OK? Or is it better to add it to the reactor outside of the constructor? On Tue, Oct 1, 2013 at 12:54 AM, Phil Mayers <p.mayers@imperial.ac.uk>wrote:
On 10/01/2013 08:30 AM, David Stainton wrote:
Hi,
I'm trying to implement an IWriteDescriptor using raw sockets. So far this is what I wrote... but it doesn't work: https://github.com/david415/**hushTCP/blob/master/hush_**writer.py<https://github.com/david415/hushTCP/blob/master/hush_writer.py>
Am I doing something obviously wrong here?
I don't see you trying to add the raw socket to the reactor anywhere; typically you'll want to do at least reactor.addWriter(hush), but for a proper implementation there's more - see the IReactorFDSet docs.
You may want to look at the udp code in Twisted to see how this is done under the hood (although TBH it can be hard to follow!)
______________________________**_________________ Twisted-Python mailing list Twisted-Python@twistedmatrix.**com <Twisted-Python@twistedmatrix.com> http://twistedmatrix.com/cgi-**bin/mailman/listinfo/twisted-**python<http://twistedmatrix.com/cgi-bin/mailman/listinfo/twisted-python>

On 01/10/13 09:05, David Stainton wrote:
I just now realized that my fileno() method was incorrect. It now returns the descriptor instead of the python socket: return self.socket.fileno()
I add the socket to the reactor in the constructor like this: reactor.addWriter(self)
Oops sorry didn't see that.
BTW is that OK? Or is it better to add it to the reactor outside of the constructor?
I guess... it's not something most people do.

On Tue, Oct 01, 2013 at 01:05:47AM -0700, David Stainton wrote:
I add the socket to the reactor in the constructor like this: reactor.addWriter(self) BTW is that OK? Or is it better to add it to the reactor outside of the constructor?
twisted.internet.stdio.StandardIO do that so it is not so bad.

On 07:30 am, dstainton415@gmail.com wrote:
Hi,
I'm trying to implement an IWriteDescriptor using raw sockets. So far this is what I wrote... but it doesn't work: https://github.com/david415/hushTCP/blob/master/hush_writer.py
Am I doing something obviously wrong here?
I certainly am able to use raw socket + scapy correctly without Twisted.
Please describe the problem in more detail than "it doesn't work". >:) Your hush_writer.py is a good <http://sscce.org/> but it's still important to know how its behavior differs from your expectations. As far as I can tell it does work - although there's one mistake that makes it a bit more inefficient than strictly necessary: def doWrite(self): if len(self.packets) > 0: self.socket.sendto(self.packets.pop(0), self.address) else: return When you're out of packets you should remove the writer from the reactor. This version of the code will perpetually dispatch writeable notification (in the form of a doWrite call) to your object as fast as possible. Once you fix this, don't forget to re-add the writer as soon as len(self.packets) rises above 0 again. Jean-Paul

Oh I was having trouble because my fileno() method was not returning the correct descriptor. But I fixed it... Thanks for the excellent suggestion for improving performance! I've implemented that change. It works great! On Tue, Oct 1, 2013 at 5:38 AM, <exarkun@twistedmatrix.com> wrote:
On 07:30 am, dstainton415@gmail.com wrote:
Hi,
I'm trying to implement an IWriteDescriptor using raw sockets. So far this is what I wrote... but it doesn't work: https://github.com/david415/**hushTCP/blob/master/hush_**writer.py<https://github.com/david415/hushTCP/blob/master/hush_writer.py>
Am I doing something obviously wrong here?
I certainly am able to use raw socket + scapy correctly without Twisted.
Please describe the problem in more detail than "it doesn't work". >:) Your hush_writer.py is a good <http://sscce.org/> but it's still important to know how its behavior differs from your expectations.
As far as I can tell it does work - although there's one mistake that makes it a bit more inefficient than strictly necessary:
def doWrite(self): if len(self.packets) > 0: self.socket.sendto(self.**packets.pop(0), self.address) else: return
When you're out of packets you should remove the writer from the reactor. This version of the code will perpetually dispatch writeable notification (in the form of a doWrite call) to your object as fast as possible.
Once you fix this, don't forget to re-add the writer as soon as len(self.packets) rises above 0 again.
Jean-Paul
______________________________**_________________ Twisted-Python mailing list Twisted-Python@twistedmatrix.**com <Twisted-Python@twistedmatrix.com> http://twistedmatrix.com/cgi-**bin/mailman/listinfo/twisted-**python<http://twistedmatrix.com/cgi-bin/mailman/listinfo/twisted-python>
participants (4)
-
David Stainton
-
exarkun@twistedmatrix.com
-
Marco Giusti
-
Phil Mayers