Be a Supporter!

Programming Regs Lounge

  • 549,044 Views
  • 16,584 Replies
New Topic Respond to this Topic
Diki
Diki
  • Member since: Jan. 31, 2004
  • Offline.
Forum Stats
Moderator
Level 13
Programmer
Response to Programming Regs Lounge 2014-01-31 14:02:40 Reply

At 1/31/14 11:19 AM, Tree-SkyLark-BCE wrote: I may not understand the stated situation correctly, but the chances of winning the lottery are much higher than 1 out of 13^1000000.

Indeed. I was tired when I thought up that hypothetical situation, and forgot that the odds picking the ace sequentially will increase exponentially, which makes what I said rather ironic. Oh well.

Diki
Diki
  • Member since: Jan. 31, 2004
  • Offline.
Forum Stats
Moderator
Level 13
Programmer
Response to Programming Regs Lounge 2014-02-02 18:43:30 Reply

At 1/30/14 11:01 PM, Diki wrote: I'll probably be posting updates on that as it progresses, but right now I'm still learning the inner-workings of the boost library, so I don't have anything to post (other than what currently basically amounts to an echo server).

Time for an update (and another wall of text).

I've been doing a lot of reading and tinkering, and I've gotten to the point that I'm comfortable working with Boost's Asio library. I used this example as a basic template for my code, and after tweaking the formatting to my preferences, and inserting the necessary functionality for communicating with Flash Player via sockets (e.g. responding to the cross-domain policy request) I now have this working code. It's still a proof of concept which is why I have it all in one file instead of separated into header and source files. The implementation is the same as the example found on Boost's website that I linked:

int main() {
    try {
        boost::asio::io_service io_service;
        Server server(io_service);
        io_service.run();
    } catch (std::exception& e) {
        std::cerr << e.what() << std::endl;
    }

    return 0;
}

And here's a breakdown of how it works:

After the server object is initialised it calls the start_accept() function, which allocates memory for a new client. When a client connection is called the handle_accept() function is called because of this line:

__acceptor.async_accept(pclient->socket(),
    boost::bind(&Server::handle_accept, this,
        pclient,
        boost::asio::placeholders::error
    )
);

Without going into too much detail, that is simply waiting for a connection on the socket object that is returned by pclient->socket(), and calls the callback, which is the second argument. The boost::bind function is kind of like a function pointer. The first argument to boost::bind() is the function that will be called, the second argument is a pointer to the object that holds the function, and the last two arguments are what will be passed to Server::handle_accept(). The Server::handle_accept() function simply initialises the client object, and tells it to start listening for incoming data, and then tells the server to once again listen for incoming connections.

The client listens for incoming data by this function being called:

inline void socket_read(size_t nbytes) {
    boost::asio::async_read(__socket, boost::asio::buffer(__data,nbytes),
        boost::bind(&Client::handle_read, shared_from_this(),
            boost::asio::placeholders::error,
            boost::asio::placeholders::bytes_transferred
        )
    );
}

It works pretty much the same way that async_accept() does. There are really only two notable differences. The first is that the second argument is boost::asio::buffer(__data,nbytes), which returns a buffer object that will store its data inside the __data variable (which is a char array) and expects the data to be of size nbytes. The second difference is the shared_from_this() function. It is used to return a pointer to the current object. The this pointer could technically be used there, but since the client object was allocated on the heap and stored in a shared_ptr object, that function must be used so that the Boost library knows that a reference to the pointer still exists, otherwise it could be popped off the heap. The shared_from_this() function exists because the Client class extends boost::enable_shared_from_this.

Once the socket receives data that is of size equal to or greater than nbytes it will call the Client::handle_read function that simply determines what type of data has been sent (i.e. either a header or a message). Because of the nature of Boost.Asio, and socket programming in C/C++ in general, you need to know how much data is going to be received before you receive it. I have it setup such that ever header message is 22 bytes and is in the format "NNP/1.0 LENGTH=125". It is unlikely that the header will ever actually be 22 bytes in size, so if it's not I simply pad the data until it is. The length determines the size of the message that will follow the header. NNP/1.0 stands for "NoName Protocol version 1.0", which is just a working title until I can think of something proper to put there.

So if I wanted to send "hello world" to the server I would first send:

NNP/1.0 LENGTH=11\0\0\0\0\0

And then immediately afterwards send:

hello world

Since the server received the header it knew that a message that is 11 bytes in size will follow. After receiving that message the server then listens for another piece of data that is 22 bytes in size, which will be another header.

One might wonder why I specifically chose to have a header size that is 22 bytes. The reason is that when Flash Player connects to a server over a socket that is hosted on a different domain than what Flash Player is running on it sends a policy request before anything else. The policy request is just the text "<policy-file-request/>", which is 22 bytes. After receiving a response to that request it disconnects the socket that that data was sent on, and opens a new one. Since I have no way of having Flash Player send a header before sending that policy request I opted to use 22 bytes as my header size for two reasons. Firstly 22 bytes is more than enough to store the necessary header information that I require. Secondly it's much simpler than trying to treat the policy request as some sort of special message. I could use the async_read_until() function, but it has the caveat of possibly receiving extra data after the specified delimiter, which would need to be parsed out. It made much more sense to just treat the policy request as a regular message and simply put an if statement in the handle_read() function:

void handle_read_message(size_t nbytes) {
	if (memcmp(__data, POLICY_REQUEST, POLICYREQUEST_LEN) == 0) {
		socket_write(POLICY, POLICY_LEN);
	}

	socket_read(PROTOHEAD_BUFFER_LEN);
}

I intend to write some code to optimise that function call such that it won't call memcmp() unless it's necessary to do so (i.e. only if it's possible for the data that has been received to be a policy request).

And that's pretty much it.

I haven't fully recreated the proof of concept that I wrote with my crappy Python server since the protocol I'm using for this C++ server is significantly different, but now that I have all the connecting/disconnecting and sending/receiving functionality coded in that won't really be that hard.

I hope someone actually reads this post because it took me like 40 minutes to write. ;(

Diki
Diki
  • Member since: Jan. 31, 2004
  • Offline.
Forum Stats
Moderator
Level 13
Programmer
Response to Programming Regs Lounge 2014-02-03 16:28:47 Reply

At 2/3/14 02:51 PM, FistofGreedo wrote: does anyone here ever used/tried LOLCODE ?
I'm currently learning it rather an awkward language if I do say so myself

I've never used it or tried to learn it, but I do know it being awkward is the point. It's an esoteric language, like Brainfuck. It's a lot more readable than Brainfuck, though, which truly lives up to its name.

Diki
Diki
  • Member since: Jan. 31, 2004
  • Offline.
Forum Stats
Moderator
Level 13
Programmer
Response to Programming Regs Lounge 2014-02-03 16:42:02 Reply

I see your LOLCODE hello world and raise you Brainfuck's:

+++++ +++++
[
    > +++++ ++
    > +++++ +++++
    > +++
    > +
    <<<< -
]
> ++ .
> + .
+++++ ++ .
.
+++ .
> ++ .
<< +++++ +++++ +++++ .
> .
+++ .
----- - .
----- --- .
> + .
> .

I know how it works (for the most part) and I still can't wrap my head around that syntax.

Dean
Dean
  • Member since: Feb. 16, 2008
  • Offline.
Forum Stats
Moderator
Level 52
Gamer
Response to Programming Regs Lounge 2014-02-04 13:53:22 Reply

Hey guys. Been a while since I last posted in here.

Since I last posted I was offered 3 different jobs: Analyst Programmer, Applications Support Analyst and IT Support Assistant. I went with the IT support role, for a few reasons. Mostly because I've always been unsure if programming 40 hours a week is something I could cope with and I'm still convinced that I wouldn't enjoy it. Plus, I've always been interested in picking up random bits of knowledge about computing so an IT support role definitely seemed like a great learning experience. Been there about a month now and definitely feel like I made the right decision. The guys I'm working with are great and it seems like the kind of job I'll enjoy. So far there haven't been any mornings where I've dreaded the thought of going to work.

Anyway, its been a while since I last did any programming but I'm not wanting to lose that skill altogether. I'm hoping that I can come up with a few little projects that I can tinker with now and then so that I don't get too rusty. Problem is, as usual, I'm struggling to come up with any ideas.

I decided to order myself a Raspberry Pi with the intention of using it to act as a server for any client/server projects I come up with, which is something I'd like to play around with. Also because I wanted to try and better familiarize myself with the Linux environment, and it seemed like a fun way of doing so for whatever reason. It's hard to explain why, but the Pi just seems like a nice little educational tool. In theory anyway. Still haven't received mine yet.

Right now I'm thinking I'll probably try writing a simple instant messaging server/client and have various people in my house running the client. In reality it's probably not that useful a project but ever since I was developing an ad-hoc audio chat application for my university dissertation I've wanted to have a go at developing something that required a server.

Web development is something I want to get back into too. A while back I was working on a website that me and a few other people thought would be useful but I ended up feeling like I bit off more than I could chew and struggled to remain motivated. I still don't have any ideas what I want to work on in relation to web development but hopefully I'll come up with some ideas.


BBS Moderator - Feel free to send me a PM if you have a problem!

BBS Signature
Diki
Diki
  • Member since: Jan. 31, 2004
  • Offline.
Forum Stats
Moderator
Level 13
Programmer
Response to Programming Regs Lounge 2014-02-04 20:17:41 Reply

At 2/4/14 01:53 PM, Dean wrote: Hey guys. Been a while since I last posted in here.

Nice to see another regular 'round these parts. :)

At 2/4/14 01:53 PM, Dean wrote: Since I last posted I was offered 3 different jobs: Analyst Programmer, Applications Support Analyst and IT Support Assistant. I went with the IT support role, for a few reasons. Mostly because I've always been unsure if programming 40 hours a week is something I could cope with and I'm still convinced that I wouldn't enjoy it.

Programming full-time can be pretty rough, that's for damn sure. I've pretty much always hated PHP, but at my last job I had to write it full-time and that made me absolutely loathe PHP, and dislike web development in general. All of my hobby projects now have little to nothing to do with web development as a result.

At 2/4/14 01:53 PM, Dean wrote: Web development is something I want to get back into too. A while back I was working on a website that me and a few other people thought would be useful but I ended up feeling like I bit off more than I could chew and struggled to remain motivated. I still don't have any ideas what I want to work on in relation to web development but hopefully I'll come up with some ideas.

Not to discourage you from doing web dev stuff, though. :)

Anyway, on to what I've been up to. A few posts up I described a server I am working on, and as of a few minutes ago I got the implementation of the WebSocket handshake protocol working.

Given that this is C++ it was rather tricky, and required a fair amount of reading and tinkering (as usual). Since C++, or the STL, has no way of splitting a string by delimiter I had to write a function to do that for me, as well as another function to use that to split the arguments of an HTTP request into a map. The source for those two functions is here. I still need to do some more thorough testing with them, and see if I can optimise them, but so far they work just fine.

The real trouble came with hashing the WebSocket key with SHA1, and base64 encoding the result of that. I ultimately had to use code written by other people, which I found on StackOverflow, to get that to work. The Boost libraries for SHA1 and base64 are very complex, so I need to do even more reading before I can hope to write my own functions for that (or optimise the ones that I found). The source for the two functions is here, which can be used to encode the WebSocket key with this tiny function. I ended up sticking all that into a utility header (i.e. just a header that will contain utility functions) of which the source code is here.

All that allows me to very easily parse an HTTP request into a class (source here) and encode the WebSocket key. Using my server code that I posted above, with some slight tweaks to make it work with HTTP, this is all the code required to allow a WebSocket connection.

Like before this is still just a proof of concept, which is why I'm building the response in such a cumbersome manner (among other things).

I also figure that there's no harm in explaining why I'm making all of this. I'm going to be using this server, and the JavaScript client outlined in this post, to create a web-based online hacking simulation game. It's a game inspired by Slavehack, Uplink and Hacker Evolution. I'm making the game web-based simply for the accessibility that it offers. Pretty much everything is going to be powered by WebSockets to allow for push-style updates and notifications, something that Slavehack is lacking. It will be much closer to Uplink and Hacker Evolution in style, but unlike those games it will be entirely multiplayer. I have no idea what I'm going to call it, but it has the working title "Trojan Bit".

Now that my server is progressing nicely it won't be long until I'll have something that is actually usable in web browsers. I still need to implement my protocol for sending/receiving messages over WebSocket, but that won't be very difficult considering I already have an implementation written in Python, so it's just a matter of translating the language.

Once I get my VPS setup, and all the required development tools installed on it, I'll have something up and running that people can actually use/test. For now everything is just running on my home computer, so I can't really leave a server running.

thetechnation
thetechnation
  • Member since: Apr. 24, 2013
  • Offline.
Forum Stats
Member
Level 03
Programmer
Response to Programming Regs Lounge 2014-02-08 21:22:49 Reply

At 2/3/14 04:42 PM, Diki wrote: I see your LOLCODE hello world and raise you Brainfuck's:



I know how it works (for the most part) and I still can't wrap my head around that syntax.

Bad Code bad lol

coln
coln
  • Member since: Aug. 1, 2011
  • Offline.
Forum Stats
Member
Level 01
Musician
Response to Programming Regs Lounge 2014-02-17 21:32:49 Reply

Just learned that pixel is short for "picture element!" How did I never know this? Learn something every day


BBS Signature
Antilogical
Antilogical
  • Member since: Aug. 28, 2013
  • Offline.
Forum Stats
Member
Level 01
Game Developer
Response to Programming Regs Lounge 2014-02-18 10:02:55 Reply

Slightly relevant: there's something that amuses me about the word 'texel'. Not sure what.

coln
coln
  • Member since: Aug. 1, 2011
  • Offline.
Forum Stats
Member
Level 01
Musician
Response to Programming Regs Lounge 2014-03-07 00:23:25 Reply

Hard drive failure...never a fun thing. But always a good excuse to get more storage!


BBS Signature
NinoGrounds
NinoGrounds
  • Member since: Nov. 28, 2005
  • Offline.
Forum Stats
Member
Level 20
Programmer
Response to Programming Regs Lounge 2014-03-16 04:08:35 Reply

Hello guys. I just wanted to drop by and say hi basically :P
I've been playing a game SWTOR on a professional level in the last 2 years (well, not on pro level for 2 years, but I've been obsessed with it for 2 years), thats why I am not active here anymore.
aI've been doing some sites, for some money here and there, nothing big tho. I am working on a small project now for me at the moment, if I manage to finish it, I'll post the link here.

That's all folks

kiwi-kiwi
kiwi-kiwi
  • Member since: Mar. 6, 2009
  • Offline.
Forum Stats
Member
Level 09
Programmer
Response to Programming Regs Lounge 2014-05-18 11:25:13 Reply

It's been a while since I've posted here, a lot of things have happened since last time, but right now I have found some time to work on some older projects of mine. Currently I'm making an option and GUI system for a game called snake fight, it's a remake of the first game I submitted to newgrounds that I've done in C++ using Qt.
Right now I have managed to get it working on all major pc platforms and Android, I'm pretty sure it works on iOS as well, but I haven't tried yet. The game adapts to the resolution of the platform it runs on, so hopefully I can support as many devices as possible.

Here's a demo video of the game in action: https://www.youtube.com/watch?v=Hh2Yl6hliqY , will post more as I progress.
Let me know if you want a build, I'd be happy to send one to anyone.

Shakyjake
Shakyjake
  • Member since: May. 7, 2005
  • Offline.
Forum Stats
Member
Level 22
Programmer
Response to Programming Regs Lounge 2014-05-20 09:49:41 Reply

At 2/8/14 04:39 PM, Captain-Slugworth wrote: bloody hell, that's a tricky one.

I've got to check it out sometime :3

At 2/3/14 04:42 PM, Diki wrote: I see your LOLCODE hello world and raise you Brainfuck's:

To continue the metaphor, I'm going all-in.

Welcome to HodorScript

Diki
Diki
  • Member since: Jan. 31, 2004
  • Offline.
Forum Stats
Moderator
Level 13
Programmer
Response to Programming Regs Lounge 2014-07-13 10:59:39 Reply

I made a thing!

I've been working on a CLI program that binds input from XInput-enabled devices (such as Xbox 360 controllers) to keyboard and mouse macros. Basically it's an alternative to Xpadder, which I find to be unintuitive, barely customisable, and overpriced. I am, of course, using my EasyXInput framework to handle the XInput side of things. I might add support for non-XInput-enabled devices at some point.

I'll probably add a GUI to it eventually but that's not a priority right now (plus I hate coding GUIs). The program is current in a proof-of-concept phase, so it's not actually what this post is about. I've just finished a large chunk of the API that will be used for the CLI portion of the program (i.e. the API that converts text commands to functions). After playing around with different designs, this is what I've decided on:

#include "cli.hpp"

bool alive = true;

void cmd_echo(cli::util::String name, cli::util::StringVectorP data) {
    std::cout << cli::util::Join(data) << std::endl;
}

void cmd_quit(cli::util::String name, cli::util::StringVectorP data) {
    alive = false;
}

int main(int argc, char** argv) {
    cli::cmd::Add("echo", cmd_echo);
    cli::cmd::Add("quit", cmd_quit);

    while (alive) {
        if (cli::cmd::Call(cli::util::GetInput()) == cli::cmd::error) {
            std::cout << "Unknown command: " << cli::cmd::GetPreviousInputArgsP()->at(0) << std::endl;
        }
    }
}

All that does is allow you to type out "echo hello world", which will output the text after the echo command, and shutdown the program with the quit command. The cli::util::StringVectorP parameter is just a pointer to a vector of strings, which represent the arguments given to the command (delimited by a space, by default), which is why the echo command has to join them back into a string.

A more comprehensive example can be found here, which has more commands, such as the ability to define variables which can have their values output using echo.

I'm striving for something that is intuitive and very easy to use, and I think I've done that.

The API is still in its infancy and is missing a lot of features (all features I've shown here are finished), so I won't be posting the source code just yet, but when it's finished (or near finished) I'll be throwing it onto GitHub.

That's all for now. When I finish the program that I'm making this API for, which has the working title XtoKey, I'll also be throwing it onto GitHub and releasing it for free.

coln
coln
  • Member since: Aug. 1, 2011
  • Offline.
Forum Stats
Member
Level 01
Musician
Response to Programming Regs Lounge 2014-07-13 18:56:10 Reply

At 7/13/14 10:59 AM, Diki wrote: I made a thing!

Genuinely curious: why? What is the use of turning XInput into keyboard/mouse macros and essentially controlling a computer, I assume?


BBS Signature
Diki
Diki
  • Member since: Jan. 31, 2004
  • Offline.
Forum Stats
Moderator
Level 13
Programmer
Response to Programming Regs Lounge 2014-07-14 03:56:46 Reply

At 7/13/14 06:56 PM, coln wrote:
At 7/13/14 10:59 AM, Diki wrote: I made a thing!
Genuinely curious: why? What is the use of turning XInput into keyboard/mouse macros and essentially controlling a computer, I assume?

Lots of games that are controlled primarily with a keyboard don't have gamepad support or simply never can have gamepad support (such as Flash and HTML5 games). Having the gamepad simulate mouse and keyboard input allows you to use it for pretty much anything.

Simulating mouse movement isn't super accurate, so it's not that great for games that require a mouse (e.g. shooters), but I sometimes like using the gamepad in place of my mouse while just browsing around as I can use it pretty much from anywhere in my room (i.e. not be forced to be sitting directly in front of my computer).

kiwi-kiwi
kiwi-kiwi
  • Member since: Mar. 6, 2009
  • Offline.
Forum Stats
Member
Level 09
Programmer
Response to Programming Regs Lounge 2014-07-14 14:17:12 Reply

It's been a while, and development has been slow but steady. Now the game has evolved into something more complex. I managed to implement all the customizations and the android version has several new control schemes, you can now use buttons and the accelerometer. Now all that remains for a beta is a proper tutorial.

Here's a few screenshots that showcase the menu, customizations and gameplay: firstsecondthird.

gonzored1
gonzored1
  • Member since: Sep. 27, 2011
  • Offline.
Forum Stats
Member
Level 01
Blank Slate
Response to Programming Regs Lounge 2014-07-14 22:32:46 Reply

Is anyone here a programmer interested in helping make a game?

Diki
Diki
  • Member since: Jan. 31, 2004
  • Offline.
Forum Stats
Moderator
Level 13
Programmer
Response to Programming Regs Lounge 2014-07-14 23:51:01 Reply

At 7/14/14 10:32 PM, gonzored1 wrote: Is anyone here a programmer interested in helping make a game?

You'll be more likely to find help if you give more information in the thread you posted.

Generally speaking, most people won't work for free, so if you're offering compensation you should mention that. It would also be a good idea to give some information regarding what style of game it is (single-player, multi-player, 2D, 3D, shooter, RPG, et cetera) so people will know how much work will be involved. One of the most important things is to explain what your role in project will be so people can know that they won't be doing everything on their own.

I personally cannot help you (I'm not a game developer) but doing what I described will certainly make it more likely that you will find someone, but there is no guarantee.

coln
coln
  • Member since: Aug. 1, 2011
  • Offline.
Forum Stats
Member
Level 01
Musician
Response to Programming Regs Lounge 2014-09-06 11:33:42 Reply

Ahhhh! I've been writing in Python so long that when I go back to C (or any other language), I forget to type semi-colons! Oh the pains....


BBS Signature
WoogieNoogie
WoogieNoogie
  • Member since: Jun. 26, 2005
  • Offline.
Forum Stats
Member
Level 15
Programmer
Response to Programming Regs Lounge 2014-09-06 11:52:07 Reply

I enjoy semicolons and braces. One of the projects I work on is in Coffeescript, and it annoys the hell out of me.

coln
coln
  • Member since: Aug. 1, 2011
  • Offline.
Forum Stats
Member
Level 01
Musician
Response to Programming Regs Lounge 2014-09-06 21:45:05 Reply

At 9/6/14 11:52 AM, WoogieNoogie wrote: I enjoy semicolons and braces. One of the projects I work on is in Coffeescript, and it annoys the hell out of me.

Don't get me wrong, I like them too. I have just created a habit of not using them since I've been on a Python kick recently. I haven't played with Coffeescript yet though. What is the purpose of it (genuinely asking)?


BBS Signature
WoogieNoogie
WoogieNoogie
  • Member since: Jun. 26, 2005
  • Offline.
Forum Stats
Member
Level 15
Programmer
Response to Programming Regs Lounge 2014-09-06 22:22:03 Reply

At 9/6/14 09:45 PM, coln wrote: What is the purpose of it (genuinely asking)?

It actually just compiles down to Javascript, so the point of it is to speed up writing code. The time it takes me to basically remember the correct syntax of...no syntax...kinda slows me down. The writing part is pretty quick, but if I have to go back through and debug code, especially someone else's, it gets a little hard.

I'm sure if I were to switch to 100% using it, it would work better, but if I were to switch how I write Javascript, I would just go to Dart.

coln
coln
  • Member since: Aug. 1, 2011
  • Offline.
Forum Stats
Member
Level 01
Musician
Response to Programming Regs Lounge 2014-09-15 18:13:25 Reply

Anyone familiar with cluster computing or slave/master computing? I have a few old computers lying around (they aren't too terribly powerful) and I wanted to get some use out of them. I've been looking around and the best solution I've come up with so far is Synergy, but that's just a KM switch, so they're still 3 separate computers working independently.

I don't necessarily need to do heaving computations on it, but I would like to combine their efforts to turn 3 crappy computers into 1 decent 3-core computer. I know from research that this is quite difficult with such a small scale and not really worth it unless I want to render video or figure out genetic folding.

Any thoughts?


BBS Signature
Hopje
Hopje
  • Member since: Dec. 17, 2007
  • Offline.
Forum Stats
Member
Level 04
Blank Slate
Response to Programming Regs Lounge 2014-09-18 13:21:23 Reply

At 5/20/14 09:49 AM, Shakyjake wrote:
At 2/8/14 04:39 PM, Captain-Slugworth wrote:
To continue the metaphor, I'm going all-in.

Welcome to HodorScript

If you still want to be able to create something with the silliness of the above languages: ArnoldC http://lhartikk.github.io/ArnoldC/

Urp, smilies are broken for me :'(


Hops...Bops...Nops...Sops...

BBS Signature