Building a Bittorrent Client in C#
-
https://cheatdeath.github.io/research-bittorrent-doc/
Pretty in-depth how-to.
-
private void HandleMessage(byte[] bytes) { LastActive = DateTime.UtcNow; MessageType type = GetMessageType(bytes); if (type == MessageType.Unknown) { return; } else if (type == MessageType.Handshake) { byte[] hash; string id; if (DecodeHandshake(bytes, out hash, out id)) { HandleHandshake(hash, id); return; } } else if (type == MessageType.KeepAlive && DecodeKeepAlive(bytes)) { HandleKeepAlive(); return; } else if (type == MessageType.Choke && DecodeChoke(bytes)) { HandleChoke(); return; } else if (type == MessageType.Unchoke && DecodeUnchoke(bytes)) { HandleUnchoke(); return; } else if (type == MessageType.Interested && DecodeInterested(bytes)) { HandleInterested(); return; } else if (type == MessageType.NotInterested && DecodeNotInterested(bytes)) { HandleNotInterested(); return; } else if (type == MessageType.Have) { int index; if (DecodeHave(bytes, out index)) { HandleHave(index); return; } } else if (type == MessageType.Bitfield) { bool[] isPieceDownloaded; if (DecodeBitfield(bytes, IsPieceDownloaded.Length, out isPieceDownloaded)) { HandleBitfield(isPieceDownloaded); return; } } else if (type == MessageType.Request) { int index; int begin; int length; if (DecodeRequest(bytes, out index, out begin, out length)) { HandleRequest(index, begin, length); return; } } else if (type == MessageType.Piece) { int index; int begin; byte[] data; if (DecodePiece(bytes, out index, out begin, out data)) { HandlePiece(index, begin, data); return; } } else if (type == MessageType.Cancel) { int index; int begin; int length; if (DecodeCancel(bytes, out index, out begin, out length)) { HandleCancel(index, begin, length); return; } } else if (type == MessageType.Port) { Log.WriteLine(this, " <- port: " + String.Join("", bytes.Select(x => x.ToString("x2")))); return; } Log.WriteLine(this, " Unhandled incoming message " + String.Join("", bytes.Select(x => x.ToString("x2")))); Disconnect(); }
Elegant.
Filed under: Huge if-else chain? Check. Many exit points? Check. Processing happening inside if conditions? Check...
-
Has anyone successfully downloaded a boat yet?
-
@ben_lubar I thought it was a car?
-
@ben_lubar I'm sure there are boat schematics compatible with 3D printers.
-
@LB_ said in Building a Bittorrent Client in C#:
@ben_lubar I'm sure there are boat schematics compatible with 3D printers.
I just want the words "pirated ship" to mean something nerdy.
-
@ben_lubar said in Building a Bittorrent Client in C#:
@LB_ said in Building a Bittorrent Client in C#:
@ben_lubar I'm sure there are boat schematics compatible with 3D printers.
I just want the words "pirated ship" to mean something nerdy.
https://storage.googleapis.com/vidsums/1bd49652-742f-469e-a718-1cbb46c1318d_text.gif
-
@error LOL...finally watched that just yesterday.
-
@boomzilla said in Building a Bittorrent Client in C#:
@error LOL...finally watched that just yesterday.
Darn, I was going to say the same thing! Except Sunday, because raisins.
Note to self: Attempting to impromptu IronMan is a Terrible Idea.
-
@boomzilla said in Building a Bittorrent Client in C#:
@error LOL...finally watched that just yesterday.
Yeah, I watched it this past weekend as well, because it finally showed up free on HBO Go.
I've been hearing about it a lot at work because we have a partnership with NASA to reach kids for STEM education.
-
@error it's a good film, and quite true to the book but still not as good as it
-
@Jaloopa said in Building a Bittorrent Client in C#:
@error it's a good film, and quite true to the book but still not as good as it
HE LEANED FORWARD, his breath the smell of whiskey drunk straight from the bottle. His mouth never all the way closed. His blue eyes never more than half open. His one hand held a coiled loop of rope, the old hemp kind, blond as his hair. Yellow as his cowboy hat. The cowboy kind of rope, and he shook the rope in my face as he talked. Behind him, an open door showed a flight of stairs that went down into the dark.
He was young with a flat stomach, wearing a white T-shirt and brown cowboy boots with thick heels. His hair, blond under the straw cowboy hat. A belt with a big metal buckle holding up blue jeans. His skinny white arms, tanned smooth as the pointed toe on each cowboy boot.
His eyes veined with a forest of little red lines, he says to grab hold of the rope and grip it—tight. And pulling the rope, he starts down, his cowboy heels hammer a step, then another hard wooden knock into the dark basement. There, in the dark, dragging me, his breath the whiskey smell, the same as the cotton ball in a doctor's office, the cold touch of rubbing alcohol the moment before an injection.
There, another step into the dark, the cowboy says, "The first rule of the Haunted Tunnel Tour is you don't talk about the Haunted Tunnel Tour."
And I stop. The rope still a loose sagging smile between us. "And the second rule of the Haunted Tunnel Tour," the cowboy, his whiskey smell says, "is you don't talk about the Haunted Tunnel Tour...."
The rope, the feeling of braided fibers, is twisted hard and greasy smooth in my hand. And still stopped, pulling back on the rope, I tell him: Hey...
From the dark, the cowboy says, "Hey, what?"
I say, I wrote that book.
The rope between us going tighter, tighter, tight.
And the rope stops the cowboy. From the dark, he says,
"Wrote what?"
Fight Club, I tell him.
And there, the cowboy takes a step back up. The knock of his boot on a step, closer. He tilts his hat back for a better look and pushes his eyes at me, blinking fast, his breath boilermaker strong, breathalyzer strong, he says:
"There was a book?"
-
@lucas1 Oh, cool! I've been thinking of using torrent techjology for some of my hobby projects, but had no idea where to start. This will come in handy.
-
@error said in Building a Bittorrent Client in C#:
Elegant.
Except he should embrace early exit style. Once you
return
, you don't needelse
.
-
@cartman82 said in Building a Bittorrent Client in C#:
@error said in Building a Bittorrent Client in C#:
Elegant.
Except he should embrace early exit style. Once you
return
, you don't needelse
.No, some of those
return
s are nested in innerif
s.
-
@cartman82 That's probably a few pages after switch is introduced.
-
@error give me an example where you NEED else after return. Impossinle by definition.
-
@cartman82 said in Building a Bittorrent Client in C#:
@error give me an example where you NEED else after return. Impossinle by definition.
if foo, err := SomeFunction(); err != nil { return nil, err } else { return foo.Bar, nil }
-
@ben_lubar said in Building a Bittorrent Client in C#:
if foo, err := SomeFunction(); err != nil { return nil, err } else { return foo.Bar, nil }
What language is that? It's certainly not C#, which is what's being discussed here.
If I had to guess, I'd say Go, but that's purely a guess; I really have no idea.
-
@masonwheeler said in Building a Bittorrent Client in C#:
@ben_lubar said in Building a Bittorrent Client in C#:
if foo, err := SomeFunction(); err != nil { return nil, err } else { return foo.Bar, nil }
What language is that? It's certainly not C#, which is what's being discussed here.
The post I quoted didn't say the example had to be C#.
Filed under: Go, by the way
-
@cartman82 said in Building a Bittorrent Client in C#:
@error give me an example where you NEED else after return. Impossinle by definition.
The example has
if( foo ) { var bar = doStuff(); if( bar ) { doMoreStuff(); return; // return here is conditional } } else { // else here is necessary // whatever }
If you mean NEED as in strictly NEED, then you only need one opcode.
-
@error said in Building a Bittorrent Client in C#:
you only need one opcode.
what about INT? How do you do system calls without INT?
-
@ben_lubar said in Building a Bittorrent Client in C#:
@cartman82 said in Building a Bittorrent Client in C#:
@error give me an example where you NEED else after return. Impossinle by definition.
if foo, err := SomeFunction(); err != nil { return nil, err } else { return foo.Bar, nil }
No one cares what happens in that barbaric language 👹
-
@error said in Building a Bittorrent Client in C#:
@cartman82 said in Building a Bittorrent Client in C#:
@error give me an example where you NEED else after return. Impossinle by definition.
The example has
if( foo ) { var bar = doStuff(); if( bar ) { doMoreStuff(); return; // return here is conditional } } else { // else here is necessary // whatever }
If you mean NEED as in strictly NEED, then you only need one opcode.
Obviously you need the else there. I was refering to this.
if (type == MessageType.Unknown) { return; } else if (type == MessageType.Handshake) {
-
@ben_lubar said in Building a Bittorrent Client in C#:
what about INT? How do you do system calls without INT?
do our C code use int directly, or are the INTs all in libc? that could explain.
the link say movs are turing complete, wtf
-
there is more than one mov opcode anyway
-
@error said in Building a Bittorrent Client in C#:
I've been hearing about it a lot at work because we have a partnership with NASA to reach kids for STEM education.
My brother works at JPL. He's going to give us a tour this summer while we're visiting. The kids (and the adults) are pretty excited about that.
-
@ben_lubar said in Building a Bittorrent Client in C#:
what about INT? How do you do system calls without INT?
You stop using DOS and use SYSCALL/SYSENTER
-
@CreatedToDislikeThis not mov either. madagascar lemurs not happy
-
@cartman82 said in Building a Bittorrent Client in C#:
I was refering to this.
ARGH!!! That indenting - kill it with fire!!!
-
-
@Jaloopa said in Building a Bittorrent Client in C#:
@cartman82 said in Building a Bittorrent Client in C#:
} else {
Dude, trigger warning please
That's our official coding style here. sigh.
-
@Jaloopa I like that style. That it annoys some people pleases me even more.
-
@dkf You evil cunt :D
It is a little odd in C#, but perfectly find in other C like language (java, javascript etc).
-
@candlejack1 said in Building a Bittorrent Client in C#:
do our C code use int directly, or are the INTs all in libc?
They're in libc, though
syscall()
is a very thin wrapper.
-
@dkf I also like the brace style.
Those parentheses and spaces, though( who the fuck puts the parentheses on the wrong side of the spaces? ) ...it's fucking stupid.
-
@cartman82 IIRC in some compilers, and if..else chain can be converted into a jump table. Perhaps they're not smart enough the other way?
-
@FrostCat said in Building a Bittorrent Client in C#:
if..else chain can be converted into a jump table
That's quite a common optimisation, but only where the tests are suitable (typically comparing the same unchanging local variable against compiled-to-integer values).
-
@dkf OP sounded like it'd fit that description.
-
@FrostCat Might do. Still got a whiff of dumb about it because dispatch probably ought to be driven more by some sort of lookup that delegates to a more focused handler, instead of using a big-ass chain of tests…
-
-
@Yamikuronue I would've started off that way, yeah. Partly it depends on how smart the compiler is!
I've been hearing for 15 years, maybe longer, that "a smart compiler could do these kind of things". I don't know if real-world ones actually are.
-
@FrostCat I mean, if I want the compiler to compile down to a jump table, generally I'd write the structure that most resembles a jump table, especially when it's cleaner and easier to read. But maybe that's just me.
-
@Yamikuronue said in Building a Bittorrent Client in C#:
I mean, if I want the compiler to compile down to a jump table
You realize I agree with you, right? :)
-
@anotherusername said in Building a Bittorrent Client in C#:
Those parentheses and spaces, though( who the fuck puts the parentheses on the wrong side of the spaces? ) ...it's fucking stupid.
Many people, it seems. You, for instance, based on your comment.
-
@boomzilla I was trying it to see if I liked it. I didn't, so I pushed Submit to see if anyone else liked it.
-
@anotherusername You got the parentheses wrong for English but right for code.
-
@boomzilla said in Building a Bittorrent Client in C#:
@anotherusername You got the parentheses wrong for English but right for code.
How does one misspell "wrong" so badly, after spelling it correctly the first time?
-
@anotherusername said in Building a Bittorrent Client in C#:
How does one misspell "wrong" so badly, after spelling it correctly the first time?
You'll have to ask someone who did that, heathen.
-
@boomzilla I did.