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?
@@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.
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.
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.
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 (:
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.
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
@javidx9 Im getting an error from asio::io_context context saying that it has asio has no memeber? ive triple checked the install and made sure its linked properly and it all checks out, pretty frustrating! Any ideas?
windows: go to a website, download a zip file, extract it somewhere, set up your project files to include them... linux: hehe `pacman -S asio` go brrrr
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.
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.
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.
Thanx for your awesome crystal explanations! That is SOOO great and useful. 46:27 Does make any sense that you use std::move() with a const variable in the push functions though? 55:02 Calling .release() on a unique_ptr would relinquish the inner C pointer! Maybe you wanted to use .reset() instead to release the memory?
@conatcha it doesn't make any sense to use std::move on a const object (especially not in combination with emplace_back) i had a debate with javidx9 1 or 2 months ago about this issue and he basically told me to "f*ck off" take a look at Jason Turner's C++ weekly episode about emplace_back and push_back ;)
@@herrdingenz6295 F'ng YT is randomly deleting my comments, I don't know why. In my previous message I said thank you fo your answer and that I was sorry to read that you couldn't get to any conclusion with the channel's owner.
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 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.
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.
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!
You are using threads for non-blocking communication. Does ASIO contain some way not to use threads like a portable version of the linux system call "select" ? imho using threads complicates the code and makes you create thread-safe data structures (like the queue) which not only adds overhead but can lead to deadlocks/starvation in bigger projects if not well coded. I am a bit frustrated seeing you using threads and closures. Of course your use of closures here is rigorous and will potentialy not get you in trouble, but I see multiple problems with using them. One of them is the use after free problem we see too many times with cpp closures. Anyway your video is very interesting for understanding how to think about and create a framework :)
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"
Thank you for the video. Unfortunatly, I cannot create the asio::io_context variable: "asio has no member io_context". The code till 7:16 min comprising the asio::error_code compiles. So, I looked into the external dependencies io_context.hpp file and there are several compiler errors. Did you make further setting within the project settings of visual studio? Btw despite your explaination, your folder structure of asio looks more like the non-boost version (i downloaded both).
@@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::
@Bård S good catch. Call reset method with nullptr value will be right here. Alternative - save return of the release method and call destructor of the returned object.
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.
I love your videos .. I want a tip, what site or material can I use to get to your level? Or at least a little closer .. lol I want to learn how to make money with C++
lol thanks Bruno - Im not aware of any shortcuts sadly, other than boring practice. It helps to write things you actually care about, rather than text book style demonstration programs. I always recommend to people to try and write a program that can be used by one of their other hobbies - it doesnt need to be perfect or original, but it should hold your interest long enough to actually see it finished, which is a rare thing among learners.
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!
actually i didnt like the series because of you are just writing and not explaining well everyting . there was tons of things that i dont know in this video you are not explaining well
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. ...
@@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.
Yeeeeees, networking dumb terminology that makes you more confused, yeeeeees (I've worked for a network testing equipment company for some time, so context, sockets, endpoints, were part of the daily job)
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!
In this series, are you going to go into the complications of multiplayer games in particular, like server validated player movement that doesn't feel choppy, for example?
Dear David Barr, Thanks for the video. I've uploaded some mistakes I made and how to solve them so that others can benefit. PROBLEM1: "C2857 '#include' statement specified with the /Ycpch.h command-line option was not found in the source file NetClient SOLUTION1: Right click the project NetClient->Properties->C/C++->All Options->Precompiled Header->"Choose the option", Not Using Precompiled Header PROBLEM2: I ran into an issue with memcpy in the video "C2664 'void *memcpy(void *,const void *,size_t)': cannot convert argument 1 from 'const main:: (*)[5]' to 'void *' SOLUTION2: Remove the "const" keyword for data extraction. When you push data out, there is no const. friend Message& operator >> (Message& msg, DataType& data)
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.
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.
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 :) )
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.
Being a C++ GUI developer, i find the concepts quiet tough to digest initially. I got to work very hard understanding and assimilating all of these network concepts. Plus the C++ here is quiet advanced and i feel like a complete novice, while watching the video. However, OLC has done a fantastic job on his part trying to explain such complex stuff in a simple manner.
totally tubular video. if you want to lower level with networking, get familiar with the gnu c library's sockets implementation (sys/socket.h) or winsock (gotta be familiar a lil with w32). always interesting to send crafted ipv4 packets.
17:22 Be careful with capturing by reference. By the time the lambda runs, whatever references it captured might have become invalid. If you wanna keep data around it's best to wrap it into a shared_ptr and capture that by value.
@@javidx9 Thank you for answering (and so quick) I don't know why, but I fixed it by making "NetCommon" from scratch. I also removed "SimpleExample" from the solution, it was causing exception for some reason (I guess 2 "main()"s)
hi, thank for your really useful info on Asio. some time ago I tried to follow this example, but I was too scared of lambda functions and multithreading, so I went back, learned a lot, and implemented my own multithreaded client server application using windows sockets. then I returned to your example and I can see that there are a lot of similar ideas for example I receive bytes using recv() in cycle using some small buffer, or for example sending and receiving in a different threads. Also I use mutexes and shared memory. I still work on my project, and now I think maybe I should rewrite it in more modern way, like your example, with lambdas and asio? I write in C++ 03, because i studied an old book "Accelerated C++"
Hey ilyas, sounds like you're making great progress! Yeah, I'd get used to c++17 if you can, 03 lacks a lot of the new toys to play with. But also as libraries mature they will require the features of 17. I've noticed this, a real push towards keeping c++ fresh.
@@javidx9 thanks for your answer, now I watched this video and understood about 85% of code, which is great for me. If you know any book on modern C++, can you please share the name of it. Thanks.
Well damn this could have saved me a lot of work if I had waited a month, I just started writing a MMO using ASIO and I did a terrible job compared to this _and_ it took me ages :p
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
*Summary* This video is the first in a series to create a custom C++ networking framework based on ASIO (Asynchronous Input Output) library, aiming for simplified client/server communication. Here's a breakdown: *Why ASIO and a Custom Framework?* (0:00) * Sockets are platform-dependent and complex to handle. * ASIO offers portability and error handling for networking. * The framework simplifies ASIO's asynchronous nature for the user. *Networking Challenges (and How This Framework Tackles Them):* (14:43) * *Unpredictable Timing:* (14:45) Network operations are asynchronous, meaning they don't happen instantly. This framework utilizes ASIO's asynchronous capabilities to run network tasks in the background without blocking the main program. * *Unknown Data Sizes:* (14:57) We don't know how much data a server will send. This framework mandates using messages with headers: * Header: Contains a unique ID (using enum class for compile-time validation) and the size of the entire message (including header). * Body: Contains the actual data (payload) of varying size. *Framework Components:* (22:48) * *Threadsafe Queue (`net_tsqueue.h`):* (45:02) Stores messages being sent and received. Utilizes mutexes to prevent data corruption from simultaneous access by multiple threads. * *Message Structure (`net_message.h`):* (27:46) * Contains the message header (ID and size) and a body (vector of bytes). * Allows easy serialization/deserialization for transmitting data. * Provides overloaded operators to push and pop data like a stack. * *Connection Object (`net_connection.h`)* (49:14) * Represents the link between client and server, handling sending and receiving messages through its socket. * Manages outgoing and incoming message queues specific to the connection. * *Client Interface (`net_client.h`):* (51:23) * Provides a simplified way for applications to connect to servers and send/receive messages. * Owns the ASIO context and the socket before a connection is established. * *Server (Discussed, Implementation in Future Videos):* (40:36) * Accepts multiple client connections and processes messages from all clients through a single incoming queue (facilitating single-threaded server design). *Key Points:* * Modern C++ features (like templates, enum classes) ensure code readability, type safety, and compiler assistance. * The goal is to abstract away the complexities of ASIO and asynchronous programming for users of this framework. * This video lays the groundwork for the series, focusing on architecture and initial implementations. The following parts will demonstrate a fully functional framework and eventually its use in a game. i used gemini 1.5 pro to summarize the youtube transcript
If you ever encounter something like the following PROBLEM 3: Error C7568 argument list missing after assumed function template 'Tsqueue' SOLUTION The issue is in the brackets TsQueue(const Tsqueue & = delete); //WRONG TsQueue(const Tsqueue &) = delete; //CORRECT
When somebody who writes C++ uses the word "framework" I know I am not going to like it. I watched this in full, and yes, I do not like it but I guess that is why am not a C++ programmer. All you have here is a TCP state machine buried in C++isms. It might have been simpler to write the state machine as a thread and simply talk to the thread. I assume you are using TCP for security, for lowest latency and simpler coding UDP would be a better bet, but as you talk about hosting the game server I do understand that TCP is simpler to manage. It is good practice if the header specifies a size to use something with a bounded size to represent the data that follows. Unsigned 16 bit ensures would ensure that the data following would always be
I have a requirement to build a TCP client-server based application which should be built using POSIX API from UNIX C standard library, is ASIO compliant to POSIX?
Invaluable video, liked and subscribed. How much work would it be to repurpose this library/framework using UDP instead of TCP? Also, when would you want to use UDP and why? Sorry if you already answer this later in the series.
The upside of Boost, is being like a verbose application-generator. Yet, while... Boost is a bloat of: 1. Utter-complications. 2. Unreadable-code marred by nested lambdas and unadjusted curly-braces, spotted with templates. Thus spending long times glaring at 6 to 10 lines long snippets, hoping to reverse-engineer them. 3. Futures seem to break away from finite state-machines, resulting with uncertainties. Horrid. 4. 'experimental' code = Undue for distribution.
ASIO has been "Audio Stream Input Output for many decades. You can call it Async IO or whatever but just deciding to use a different technology's name is confusing.