Networking in C++ Part #1: MMO Client/Server, ASIO & Framework Basics
Vložit
- čas přidán 24. 07. 2024
- In this series, I use ASIO to create a portable, reusable, simple and flexible framework for general purpose client/server applications in C++. This video shows how to setup ASIO, then explores some of the challenges networking presents in general, before outlining the framework and starting to implement the basic components of it. Ultimately this series will yield a simple MMO game.
Source: github.com/OneLoneCoder/Javid...
Patreon: / javidx9
CZcams: / javidx9
/ javidx9extra
Discord: / discord
Twitter: / javidx9
Twitch: / javidx9
GitHub: www.github.com/onelonecoder
Homepage: www.onelonecoder.com - Věda a technologie
For all those unconvinced it can work, please download and try the prototype project here! onelonecoder.itch.io/meaningless-cog
Decided to provide some tea for this quality content ;) Keep up the good work.
Thank you Zaneris! Much appreciated!
Yeah asio comes with ssl objects you can use to negotiate https. A bit too complex for a simple introduction 😁
Ive always been interested in building a mmo project over a long period of time. Is it possible to use this method for high concurrency on a 3D client?
thanks for all the work you do! as someone learning c++, you are truly helpful thank you
I have no idea what i'm watching, but thank you bearded man for the knowledge.
Best comment ever XD
I work in IT, and the only thing I understood was the networking lol
HTTPS changed the strings being sent in plain text though
You have to see "Hands on Machine Learning whit python"...
@@LockeIProd and that does not count from 1 lmao
This man will always be the person I go to for difficult c++ programs
It's a real shame he refuses to use modern C++ idioms and techniques.
@@superscatboy Cniles are set in their ways
@@qx-jd9mh True. I've never understood why though, it's not exactly hard to keep up with the evolution of the language - it progresses at a glacial rate after all.
@@superscatboy Imagine not using RAII, smart pointers, l/r values/move semantics, lambdas, auto/delctype, default/deleted functions, initializer lists, strongly typed enums, static assertions, constexpr, or variadic templates/folding expressions. You had plenty of time to learn OO design patterns with C++03 and before. I guess people who learned systems programming with C don't want to learn higher level abstractions.
@@superscatboy Well, the best modern C++ idiom in most cases probably is to ditch it for Rust ;-)
Stop and realize for one moment how great the content OLC has been posting. Really amazing.
I'm a web developer, but always love to see the other side of programming and especially channels that produce advanced materials, not always the "Hello, world" examples.
Thank you for doing this.
this video is explanation of how web works under the hood.
For anyone else who's zip file did not match his, he made a small mistake at 3:04 and seemed to imply to download the boost_asio file, but in fact you want the non boost file.
still how can i make my vs work with that?
@@crazypeter113 check the properties files vscode creates, there must be some sort of parameter for includes iirc
this ^^^
Thank you, omg lol I was getting pissed lol
thanks bro, you saved me a lot of time and headache.
javidx9 - thanks so much for your videos. I haven't done C/C++ since college 20 years ago. You've definitely re-sparked my interest in C. It was fun putting together a working example of the message buffer from your video. I got started writing games in my teens back in the early 90s but have done business software in my professional career. In college over a Christmas break I recall writing a centipede clone in java. Great stuff! Much appreciated!
You always do something that not any ANY other CZcams programming channel would do. I love that! without you there would probably not be anyone who spends effort into their videos as much as you do. Your the best!
Everything on this channel is Gold. If I'm not interested in it right now, I'm certain I will be at some point.
Great content... Delivered well... With just the right level of detail... at the perfect pace.
I watched a lot of teaching videos in my life. Really a lot. But this one, it's the best of all. I like the language, intonation, and speaker's face, all of it brings live to the whole video. I'm impressed.
Thank you for the phenomenal video! + Thank you for not assuming that viewers already know what you know + Thank you for explaining things slowly, one small step at a time. That's the best method.
Hey thanks buddy! It's a formula that works.
This is explained in a really easy to digest manner and I'm really happy it showed up in my recommendations.
If all your videos are of this quality then I have a lot to sit through in the nearest future!
Great work!
I really appreciate the prep work that went into this video. Specifically I like that you first explore some of the dead ends to help drive home some of the subtleties of asyncronous behavior.
Coder with 10 years of experience here but in other languages such as C# etc. Learning about C++ now and I gotta say your videos are on a very high technical level and you go through them step by step which I really appreciate. Great quality stuff, keep going. Also highly respect your professional skill level - it kinda makes me feel like a dumbass xD
this was great, can't wait for the rest of the series.
A lot of mental effort was clearly put into making this video as informative as it could be. Thank you for your hard work.
I am so looking forward to the next parts. I enjoyed so much watching you opening new doors for so many possibilities for a game I have on my mind. Thank you sir! Much love. :)
Really great video!
Some suggestions/notes:
In net_message.h
- use is_trivially_copiable instead of is_standard_layout to check if a datatype can be safely "memcopied"
In net_tsqueue.h
- make muxQueue mutable add the 'const' keyword to the methods where applicable.
- fix push_front and push_back because you are moving a const T& which can't be moved (maybe add push_front(T&&) and push_back(&&) methods for this)
- (just to increase the portability) use std::lock_guard instead of std::scoped_lock
In net_connection.h
- when deriving from std::enable_shared_from_this it is best practice to make the ctor private and add a factory method to create an shared_ptr of that class to be sure you can always call the method shared_from_this without having undefined behaviour or an exception, depending on the c++ version
In general, I have noticed you always declare the destructor; be aware that when you declare the dtor, the move ctor and move assignment operator are deleted by default and you have to define them again so maybe defining them again could be a good idea.
thanks for the tips dude!
I don't think I've ever clicked on a video faster
I know, right?
@@filmcase1683 MAIAMAIAMIAMAIAMIAMAIMAIMAIAMIAMAIMA
For real, I don't think I'd be interested in any other channel posting a video with this title/description. However, here it's a 100% must watch.
[2]
same my dude
As someone that loves MMOs, this is something I've always wanted to learn more about, thanks for the video!
Reminiscent of an event driven pub sub. A very clean and simple approach to implementing, what is traditionally, a complex problem. As always and as expected, OLC describes everything in a very easy to understand and digest manner. Nicely done.
It took me a while to work it out so for those of you with an error that looks something like "undefined reference to `pthread_join`" (I'm on linux by the way).
I fixed it by using - pthread in the compiling command:
g++ Main.cpp -I [insert include directory] -o Main -pthread
Hope this helps (:
I am really interested to see this !!! Much needed !!!
Socket/networking tutorials are horrible for the most part with addressing how you should design it for real world applications. So happy this is being covered.
Your user name gives me anxiety
@@stewartzayat7526 Not the first time I have heard this))
@@SourceCodeDeleted you make me want to create another account named "Executable Reverse Engineered" lol.
Great vid !!! I've been using ASIO for many projects at work. I really love the way you introduced it.
Excellent indeed. One of the best modern C++ videos I have seen & you get ASIO thrown in as well. MANY thanks. You are a very cool guy 🙂
I can't keep away from these videos. I've watched and listened to so many of them.
I don't use C++.
C++ is for people who like to find problems for their solutions.
I have never clicked on part 2 of any video series as faster as I did for this one. Thank you so much.
Haven’t touched C/C++ in years to be honest - but the way you describe and explain makes me want to pick it up again for fun. Great videos!
Coming from a music production background, I was very confused by ASIO in the title...
Lol yes, its quite an unfortunate coincidence!
@@javidx9 I wish there were more tutorials on Steinberg's ASIO, lol
I thought the exact same thing when I saw the title come up.
Me too!
Me too. They aren't related? I thought the ASIO audio drivers are based on the concepts shown here from the ASIO library.
this dude makes everything looks simple
In my opinion really the best c++ and programming in general Tutorials out there, I know its a big statement, but I´ve watched a lot of tutorials and read a lot of them.
Once again you managed to amaze me. At the beginnig I thought you were going to show and explain some standard socket programming, and have in the end the typical mini-chat server and client program. However I was completely wrong, and I am glad. I did not know of ASIO at all, and I simply loved your net implementation. Of course, your explanations pure gold. Very clear. Probably the best channel I am subscribed to. Congratulations once again.
Hi wowLinh - yeah, i really want to avoid the traditional means of describing this architecture, its just boring :D
A short while back I reverse engineered an MMO protocol, implementing a replacement server using ASIO. Ended up making heavy use of strands and IO services in order to handle most of the basic queuing. ASIO comes in pretty handy for writing performant servers!
Prob my greatest frustration however was it's extremely difficult to find a good database library which works well with ASIOs async paradigm, so I ended up just using a threaded connection pool for the actual db queries. Will be interesting to see how you tackle that (if you are using a database in the backend).
I also felt that it was lacking a bit when it came to actually handling packet data - you've often got to keep track of a lot of state to properly handle rate limiting, writing out packet data, handling bad packets, authentication, and so on. But then again in the long run, probably best to handle that yourself.
Was the mmo protocol for wow? Watching this vid because I've been working on a wow emu recently and I really want to understand the underlying networking going into it. For some reason, mmo networking is fascinating.
love this youtube channel. I am a programmer but am not familiar with C++. You do such a great job explaining concepts for newer programmers! Keep it up!
Glad you enjoy it doublegdog!
Nowadays as i grow older im getting more interested in how the worlds that hooked me as a child were developed, specifically MMOS. I hope to take the time to analyze this video deeply in the future. Thanks for the video sir!
I'm installing VS community after 5 years away from coding just to follow this tutorial, i think you have read this a million times but the way you structure the information is not so simplified that it became boring but not so advance that you basically need to be a master in the topic beforehand, after 10 minutes of your video i subscribed to your channel
Golden content, yet it's free. Appreciate it people :-)
@Rusty Boomerton We know
thanks a lot for sharing this content. Info like this is very ward to find, and the majorit times people discourage another developers to do this because it is "hard" or saying "you will never gona do this in you entirely life, do only platform games, cya"
This!
I was looking for a quick intro to asio and this was great. Thank you.
The timing couldn't be perfect! I am making a raycaster with LAN. You are so helpful!
Thanks for the video, lots of useful stuff. One minor thing @ 55:04, m_connection.release() is used here. The release has a return value to return a raw pointer from the unique pointer which is not really used here. Maybe the reset() is good enough for destroying the connection? And I think the video is missing few seconds about adding Send() interface into client_interface.
For anyone following along with clion + mingw + cmak, be sure to add
if(MINGW)
target_link_libraries(learning_asio ws2_32)
endif()
to your CMakeLists.txt. Otherwise you'll get a bunch of "undefined reference to xxx..." errors when trying to compile around 9:00 mark.
*only on windows
Oh no! This video came out 3 days ago, and I was hoping to hop right into Part 2 to see how the rest of the code comes together. I am actually starting to write a client / server infrastructure and this is a VERY good introduction into ASIO.
I did not know ASIO until your video, it looks having a clean API. Nice !
Thanks a lot for making this video and that is incredibly helpful! However, I have some questions on the static_assert regarding the trivially copyable types (around 35:00 in the video).
The first question is that since you mentioned "trivially copyable", I assumed that "is_trivially_copyable" trait should be used to enforce memcpy-safety, but why is "is_standard_layout" used instead in this case?
The second question is that when you do the serialization/deserialization for data transmitted between different machines, how can we make sure the sizeof(header) or the bitwise representation of the custom data is the same on different platforms with the same code, given that the same structs may have different alignments and paddings? Although in the example you gave I guess a struct containing two uint32_t should generally be packed densely into a 64 bit chunk, I am worried that different compilers may treat other types of POD structs with different bit representation.
Thanks!
"Oh no _not Boost_"... I laughed out loud.
I want to thank you for this awesome tutorial. It's the first time that I was able to use and understand asio. Such easy examples are so rare on youtube. Thank you again, sir.
Keep up the good work! Im actually learning Java, but your videos help me extremely well to get a clear understanding what ways are to appraoch difficult situations! thanks for that!
I always missed that "one" library for c++ networking.. THANK you!
libcurl is nice but a pain to work with it's C syntax...
ASIO is great! Thanks for showing me this!
Also, thanks for taking the time for making all these videos. You truly are a gift for the c++ community.
Thanks Anonx2!
Also please note that C++ Networking TS is being specified on ASIO. There are lots of people holding thumbs that in the end we will have standard networking for C++23 (so 2023 :) )
Networking? WOOOOOOOOOOOO!
how isp is spying you?
I truly enjoy your content, keep up the work, I'll contribute as soon as this specific timeline that we are in right now comes to pass.
I had a passion for software since I was a kid, but it was very small for what was sold at that time. It was difficult to find information in Turkish. After asking, I realized that even English was scarce on the internet at that time. Thank you for giving a lot of simple graphic emulators now.
Every time, the two different audio recordings throw me for a loop. Although it works, hearing the ambient room noise when you're facing the camera suddenly makes me feel like I'm in the room with you. ...
I think that's the point. It's more personal. I don't see the issue
i dont have enough IQ to understanding these courses so i watch these when i need to sleep
And ASIO is Australia's government spy network. Australian Security Intelligence Organisation....
Holy sh- same lol
@@southernkatrina8161 ASIO is also music production program...
@@southernkatrina8161 ASIO is also 4/5ths of a calculator
I bet you are smart enough, just not interested. Which is a-okay. :) His voice is very soothing even for those of us who do watch for the content.
Thank you so much for this series!! And structuring it around an MMO will make it so easy to understand :)
Awesome, I made something similar in C# a while ago, based on SocketAsyncEventArgs, but it was mostly just to learn something new, I only used it for a few small turn based games. Currently focused on a new single player project, but one day I will continue multiplayer stuff. Watching this and taking notes to improve my own C# library in the future.
anyone know how to fix the problem of std::scoped_lock not begin defined in the tsqueue
Sure, enable c++17 for your compiler
@@javidx9 how do you enable it (i'm using vs 2019)
@@youtissoum project properties, language standard, c++17
@@javidx9 thanks it dont give me errors anymore
I don't understand how people could dislike this video 🙄
I liked the video because it's a really good and accessible intro to modern C++ networking and the inherent complexity in the domain. I use Asio myself. But the video does have a few problems that might irk some developers. The unsigned size, which is such a small thing, might be one of those things. But probably for the "thread-safe" queue. Consider a thread that calls q.front() and then inspects the result of that function. Meanwhile, another thread has called pop_front(), invalidating the reference that the first thread is inspecting. Adding locks in this way doesn't necessarily make an object thread-safe; it very much depends on the usage patterns.
They're either trolls or don't understand the code even after seeing the video. I always like javidx9 videos, even though I never code in C/C++ and there are parts I don't understand. I enjoy all his explanations and the approaches he has on various problems.
Thank you so much, i'm working on a (boost asio) TCP-UDP hybrid protocol, I've been stuck for the past three hours and you helped me figure out my problem ! You got a sub
I love how you make your code ultra-wide and readable for most viewers.
Teachers can use it in school.
I can see it from 5m away, cozy in my bed!
For MacOSX use
$ brew install asio
Cool, thanks!
corporate: "so which versions of visual studio do you want to install on your computer ?"
javidx9: "yes"
I just discovered your channel and I am loving your style of humor and presentation. Thanks for sharing!
I must say this is really an enjoyable video, it's like an sci-fi movie 😁, it's always amazing to see someone with really great skill.
Me trying to learn C++: 🤓
Me trying to learn Server-Client in C#: 😎
Me when I get this video recommended: 🥵
What I did is copy&paste functional code, and then modify it until i understand it;
3:03 he mistakenly says "boost asio" and hovers over boost_asio_1_18_0.zip
Just to clear up confusion it is just asio-1.18.0.zip
He means it. if ppl downloaded the non-boost version. The `#define ASIO_STANDALONE` is not required anymore.
@@wizardy6267 Writing #define ASIO_STANDALONE would make sense if he downloaded boost asio but he didn't. At 3:39 is his file "asio-1.18.zip" and not "boost_asio_1_18_0.zip" ?
@@neuro5261 I didn't pay attention to 3:39. But I used the asio-1.18. When I tried to build. The compiler complained the macro `ASIO_STANDALONE` redefined in the library. Since he still has the macro defined. I guess either the warning is suppressed or he is using the boost version. Or maybe the 3:39 is the mistaken spot. :p
@@wizardy6267 I don't get that warning with mine, and I'm using the non-boost version. With the boost version, even with standalone set, I was still having to tell it to use boost::asio:: instead of asio::
This series looks fantastic. Its very difficult to find information on standalone asio, and the examples provided with it are very lacking.
Looking forward to the rest of the videos!
quite a nice set of videos -- i usually implement my message collection queues when i'm doing my "day job" work using activemq which i'm particularly a fan of (i'm in the java world). this is making me want to dust off some of my older c++ applications and play around with them a bit :)
"We're not going to use sockets because...."
First thing we do is create a socket
Yes, exactly. Sockets suck ...until they don't.
hello
This is the 3rd time that I am going to see this videos to completely understand them and I will say that THIS IS PURE GOLD. This will save weeks of study and therefore THANK YOU.
That said, it will be I think be clarifying I think for everyone if you could dedicated some minutes to explain asio executors and completion handlers even tho I know that this can be out of the scope of this videos but just with the intention of clarifying some important concepts.
Thank you so much for your contribution.
This channel us just pure gold! Thank you javidx9, you are my personal hero.
Why did you have to do this after I just spent the last six months learning socket programming.
Six months for learning sockets?! How can you even spend that long on sockets?
@@CottidaeSEA most of it was just making data structs for it. I am building a scripting language and wanted it to work with socket programming.
@@gradyshastid3704 Now it makes a whole lot more sense why it'd take that long. Because normally I can't imagine it taking more than a month, but that's still in-depth and with a couple of smaller projects to gain experience using them.
As a side note, the two first things I made with sockets was a basic messenger and a battleships game. I already had the game ready, so I just slapped some multiplayer functionality onto it... though due to it being P2P one of the players could cheat by reading the memory, but I couldn't be bothered to add a server onto it. Even though I probably could've used the messenger server for it.
Edit: this side note became the majority of the reply... well, shit.
the Australian Security Intelligence Organisation? sus
I found this very useful and I enjoyed your presentation. It was fun, and has lots of value.
Pedagogical goodness!
(also, your chrono dig was funny!)
Thanks John!
Looking forward to this. Cheers, mate!
Every. Single. Video you make... Is helpful and entertaining. Thanks!
Hey thanks Otakutaru, glad to see they are appreciated!
Such amazing coding skills and teaching technique. I really like your content. THanks
I would vote this serious one of the best video series in youtube. Top 3 definitely.
I am using QT for all my needs but this is also good to know, thank you sir
I'm working exclusively in C#, but the thought process behind this is really useful. I've worked with networking on the small scale for a while, but I've wondered what the best practices were for handling communications between client/server like this for a while.
I have been thinking about creating a framework like this myself, but for C# instead of C++, since I have been using unity a lot lately and I want to get more into networking and communication between programs. I took a course in distributed systems at universiry last semester, but I think there is still a lot to learn and I am excited for future parts and your great explanations that come with them!
Excellent tutorial! You make an intimidating subject seem less intimidating.
The first 20 minutes are beyond gold, bearded man.
Really clear and detailed, keep it up!
I have been waiting a long while for this video
Well done Javid and thanks for making all these tutorials and great stuff
This video is awesome, I've always wondered how this kind of stuff is made.
I love that your video's doin't have distracting music because then I get to add my own distracting music while I continuously rewind your content run on sentence XD
yes! I was hoping you would touch on this subject!
This is what I wanted to see. Couldn't find anything about networking. Thank you!
BEST place on the internet for learning C++!
This is great, I was looking for something like this recently. Your lua tutorial helped me a lot before so I subscribed :)
Hey, guy. I'm so glad that I can see your video from a single CZcams Searching. It really impresses me when I have the first glance on the content of your video. I'm sure it's what I'm searching for. Don't be bothered by those negitive comments. Just let it go. BTW, I'm curious about do you have any serious about multi-threading of C++. I'm really eager to find such kind of materials. Since I want to switch my career from a SDET to C++ developer. They're so many things I need to learn and your course are just what I need. They're not just "hello world", they're valuable resources that can help me in my actual work and interview. Thank you so much again.
Looking forward to part 2 of this series! subbed and liked. keep it up!
Thanks Javidx9 for all those fantastic video of high technical quality, ..., simply congratulation
Great tutorial! I'm eager for the next part.
Thanks
This is an exciting series! I am looking forward to it :)
Thank you a lot! This stuff is brilliant! The lovest part where you describe the architecture. I will try to apply this way in my work, thank you!
More architecture :)
Thank you!
Finally an explanation for this kafkaesque and nebulous library in English, unlike the confusing "tutorials" on the Boost::Asio site.