|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276 |
- /*
- MMO Client/Server Framework using ASIO
- "Happy Birthday Mrs Javidx9!" - javidx9
-
- Videos:
- Part #1: https://youtu.be/2hNdkYInj4g
- Part #2: https://youtu.be/UbjxGvrDrbw
-
- License (OLC-3)
- ~~~~~~~~~~~~~~~
-
- Copyright 2018 - 2020 OneLoneCoder.com
-
- Redistribution and use in source and binary forms, with or without
- modification, are permitted provided that the following conditions
- are met:
-
- 1. Redistributions or derivations of source code must retain the above
- copyright notice, this list of conditions and the following disclaimer.
-
- 2. Redistributions or derivative works in binary form must reproduce
- the above copyright notice. This list of conditions and the following
- disclaimer must be reproduced in the documentation and/or other
- materials provided with the distribution.
-
- 3. Neither the name of the copyright holder nor the names of its
- contributors may be used to endorse or promote products derived
- from this software without specific prior written permission.
-
- THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
- "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
- LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
- A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
- HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
- SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
- LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
- DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
- THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
- (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
- OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
-
- Links
- ~~~~~
- YouTube: https://www.youtube.com/javidx9
- Discord: https://discord.gg/WhwHUMV
- Twitter: https://www.twitter.com/javidx9
- Twitch: https://www.twitch.tv/javidx9
- GitHub: https://www.github.com/onelonecoder
- Homepage: https://www.onelonecoder.com
-
- Author
- ~~~~~~
- David Barr, aka javidx9, ?OneLoneCoder 2019, 2020
-
- */
-
- #pragma once
- #include "net_dequeue_ts.h"
- #include "net_common.h"
- #include "net_message.h"
- #include "net_connection.h"
- #include "easylogging++.h"
-
- namespace net
- {
- template<typename T>
- class ServerInterface
- {
- public: // Create a server, ready to listen on specified port
- ServerInterface(uint16_t port)
- : m_asioAcceptor(m_asioContext, asio::ip::tcp::endpoint(asio::ip::tcp::v4(), port)) { }
-
- virtual ~ServerInterface() { Stop(); }
-
- // Starts the server
- virtual bool Start()
- {
- try
- {
- // Issue a task to the asio context - This is important
- // as it will prime the context with "work", and stop it
- // from exiting immediately. Since this is a server, we
- // want it primed ready to handle clients trying to
- // connect.
- WaitForClientConnection();
- // Launch the asio context in its own thread
- m_threadContext = std::thread([this]() { m_asioContext.run(); });
- }
- catch (std::exception& e)
- {
- // Something prohibited the server from listening
- LOG(ERROR) << "[SERVER] Exception: " << e.what();
- return false;
- }
- return true;
- }
-
- bool isStopped()const {
- return m_asioContext.stopped();
- }
-
- // Stops the server!
- virtual void Stop()
- {
- // Request the context to close
- m_asioContext.stop();
-
- // Tidy up the context thread
- if (m_threadContext.joinable()) m_threadContext.join();
-
- // Inform someone, anybody, if they care...
- LOG(INFO) << "[SERVER] Stopped!\n";
- }
-
- // ASYNC - Instruct asio to wait for connection
- void WaitForClientConnection()
- {
- // Prime context with an instruction to wait until a socket connects. This
- // is the purpose of an "acceptor" object. It will provide a unique socket
- // for each incoming connection attempt
- m_asioAcceptor.async_accept(
- [this](std::error_code ec, asio::ip::tcp::socket socket)
- {
- // Triggered by incoming connection request
- if (!ec)
- {
- LOG(INFO) << "[SERVER] New Connection: " << socket.remote_endpoint();
-
- // Create a new connection to handle this client
- std::shared_ptr<Connection<T>> newconn =
- std::make_shared<Connection<T>>(Connection<T>::Owner::SERVER,
- m_asioContext, std::move(socket), m_qMessagesIn);
-
-
- // Give the user server a chance to deny connection
- if (OnClientConnect(newconn))
- {
- // Connection allowed, so add to container of new connections
- m_deqConnections.push_back(std::move(newconn));
-
- // Issue a task to the connection's
- // asio context to sit and wait for bytes to arrive!
- m_deqConnections.back()->ConnectToClient(nIDCounter++);
-
- LOG(INFO) << "[" << m_deqConnections.back()->GetID() << "] Connection Approved";
- }
- else
- {
- LOG(INFO) << "[SERVER] Connection Denied";
-
- // Connection will go out of scope with no pending tasks, so will
- // get destroyed automatically (smart pointer)
- }
- }
- else
- {
- // Error has occurred during acceptance
- LOG(INFO) << "[SERVER] New Connection Error: " << ec.message();
- }
-
- // Prime the asio context with more work - again simply wait for
- // another connection...
- WaitForClientConnection();
- });
- }
-
- // Send a message to a specific client
- void MessageClient(std::shared_ptr<Connection<T>> client, const Message<T>& msg)
- {
- // Check client is legitimate...
- if (client && client->IsConnected())
- {
- // ...and post the message via the connection
- client->Send(msg);
- }
- else
- {
- // If we cant communicate with client then we may as
- // well remove the client - let the server know, it may
- // be tracking it somehow
- OnClientDisconnect(client);
-
- // Off you go now, bye bye!
- client.reset();
-
- // Then physically remove it from the container
- m_deqConnections.erase(
- std::remove(m_deqConnections.begin(), m_deqConnections.end(), client), m_deqConnections.end());
- }
- }
-
- // Send message to all clients
- void MessageAllClients(const Message<T>& msg, std::shared_ptr<Connection<T>> pIgnoreClient = nullptr)
- {
-
- bool bInvalidClientExists = false;
-
- // Iterate through all clients in container
- for (auto& client : m_deqConnections)
- {
- // Check client is connected...
- if (client && client->IsConnected())
- {
- // ..it is!
- if(client != pIgnoreClient)
- client->Send(msg);
- }
- else
- {
- // The client couldnt be contacted, so assume it has
- // disconnected.
- OnClientDisconnect(client);
- client.reset();
-
- // Set this flag to then remove dead clients from container
- bInvalidClientExists = true;
- }
- }
-
- // Remove dead clients, all in one go - this way, we dont invalidate the
- // container as we iterated through it.
- if (bInvalidClientExists)
- m_deqConnections.erase(
- std::remove(m_deqConnections.begin(), m_deqConnections.end(), nullptr), m_deqConnections.end());
- }
-
- // Force server to respond to incoming messages
- // size_t nmaxMessages: Assign -1 to unsigned to unspecify max message count
- // bool bWait: if queue is empty, wait synchronously until message arrives
- void Update(size_t nMaxMessages = -1, bool bWait = false)
- {
- if (bWait) m_qMessagesIn.wait();
-
- // Process as many messages as you can up to the value
- // specified
- size_t nMessageCount = 0;
- while (nMessageCount < nMaxMessages && !m_qMessagesIn.empty())
- {
- // Grab the front message
- auto msg = m_qMessagesIn.pop_front();
-
- // Pass to message handler
- OnMessage(msg.remote, msg.msg);
-
- nMessageCount++;
- }
- }
-
- protected:
- //Overwritable functions to customize server behaviour
-
- // Called when a client connects, you can veto the connection by returning false
- virtual bool OnClientConnect(std::shared_ptr<Connection<T>> client) = 0;
- // Called when a client appears to have disconnected
- virtual void OnClientDisconnect(std::shared_ptr<Connection<T>> client) = 0;
-
- // Called when a message arrives
- virtual void OnMessage(std::shared_ptr<Connection<T>> client, Message<T>& msg) = 0;
-
- // Thread Safe Queue for incoming message packets
- ts_dequeue<OwnedMessage<T>> m_qMessagesIn;
-
- // Container of active validated connections
- std::deque<std::shared_ptr<Connection<T>>> m_deqConnections;
-
- // Order of declaration is important - it is also the order of initialisation
- asio::io_context m_asioContext;
- std::thread m_threadContext;
-
- // These things need an asio context
- asio::ip::tcp::acceptor m_asioAcceptor; // Handles new incoming connection attempts...
-
- // Clients will be identified in the "wider system" via an ID
- uint32_t nIDCounter = 10000;
- };
- }
|