use shared_ptr
This commit is contained in:
		@@ -21,10 +21,10 @@ int main(int argc, char** argv)
 | 
				
			|||||||
    ix::WebSocketServer server(port);
 | 
					    ix::WebSocketServer server(port);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    server.setOnConnectionCallback(
 | 
					    server.setOnConnectionCallback(
 | 
				
			||||||
        [&server](ix::WebSocket& webSocket)
 | 
					        [&server](std::shared_ptr<ix::WebSocket> webSocket)
 | 
				
			||||||
        {
 | 
					        {
 | 
				
			||||||
            webSocket.setOnMessageCallback(
 | 
					            webSocket->setOnMessageCallback(
 | 
				
			||||||
                [&webSocket, &server](ix::WebSocketMessageType messageType,
 | 
					                [webSocket, &server](ix::WebSocketMessageType messageType,
 | 
				
			||||||
                   const std::string& str,
 | 
					                   const std::string& str,
 | 
				
			||||||
                   size_t wireSize,
 | 
					                   size_t wireSize,
 | 
				
			||||||
                   const ix::WebSocketErrorInfo& error,
 | 
					                   const ix::WebSocketErrorInfo& error,
 | 
				
			||||||
@@ -49,7 +49,7 @@ int main(int argc, char** argv)
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
                        for (auto&& client : server.getClients())
 | 
					                        for (auto&& client : server.getClients())
 | 
				
			||||||
                        {
 | 
					                        {
 | 
				
			||||||
                            if (client != &webSocket)
 | 
					                            if (client != webSocket)
 | 
				
			||||||
                            {
 | 
					                            {
 | 
				
			||||||
                                client->send(str);
 | 
					                                client->send(str);
 | 
				
			||||||
                            }
 | 
					                            }
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -132,13 +132,13 @@ namespace ix
 | 
				
			|||||||
    //
 | 
					    //
 | 
				
			||||||
    void WebSocketServer::handleConnection(int fd)
 | 
					    void WebSocketServer::handleConnection(int fd)
 | 
				
			||||||
    {
 | 
					    {
 | 
				
			||||||
        ix::WebSocket webSocket;
 | 
					        std::shared_ptr<WebSocket> webSocket(new WebSocket);
 | 
				
			||||||
        _onConnectionCallback(webSocket);
 | 
					        _onConnectionCallback(webSocket);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        _clients.insert(&webSocket);
 | 
					        _clients.insert(webSocket);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        webSocket.start();
 | 
					        webSocket->start();
 | 
				
			||||||
        auto status = webSocket.connectToSocket(fd);
 | 
					        auto status = webSocket->connectToSocket(fd);
 | 
				
			||||||
        if (!status.success)
 | 
					        if (!status.success)
 | 
				
			||||||
        {
 | 
					        {
 | 
				
			||||||
            std::cerr << "WebSocketServer::handleConnection() error: "
 | 
					            std::cerr << "WebSocketServer::handleConnection() error: "
 | 
				
			||||||
@@ -148,13 +148,13 @@ namespace ix
 | 
				
			|||||||
        }
 | 
					        }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        // We can probably do better than this busy loop, with a condition variable.
 | 
					        // We can probably do better than this busy loop, with a condition variable.
 | 
				
			||||||
        while (webSocket.isConnected())
 | 
					        while (webSocket->isConnected())
 | 
				
			||||||
        {
 | 
					        {
 | 
				
			||||||
            std::chrono::duration<double, std::milli> wait(10);
 | 
					            std::chrono::duration<double, std::milli> wait(10);
 | 
				
			||||||
            std::this_thread::sleep_for(wait);
 | 
					            std::this_thread::sleep_for(wait);
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        _clients.erase(&webSocket);
 | 
					        _clients.erase(webSocket);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        std::cerr << "WebSocketServer::handleConnection() done" << std::endl;
 | 
					        std::cerr << "WebSocketServer::handleConnection() done" << std::endl;
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -12,12 +12,13 @@
 | 
				
			|||||||
#include <thread>
 | 
					#include <thread>
 | 
				
			||||||
#include <mutex>
 | 
					#include <mutex>
 | 
				
			||||||
#include <functional>
 | 
					#include <functional>
 | 
				
			||||||
 | 
					#include <memory>
 | 
				
			||||||
 | 
					
 | 
				
			||||||
#include "IXWebSocket.h"
 | 
					#include "IXWebSocket.h"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
namespace ix 
 | 
					namespace ix 
 | 
				
			||||||
{
 | 
					{
 | 
				
			||||||
    using OnConnectionCallback = std::function<void(WebSocket&)>;
 | 
					    using OnConnectionCallback = std::function<void(std::shared_ptr<WebSocket>)>;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    class WebSocketServer {
 | 
					    class WebSocketServer {
 | 
				
			||||||
    public:
 | 
					    public:
 | 
				
			||||||
@@ -30,7 +31,7 @@ namespace ix
 | 
				
			|||||||
        void run();
 | 
					        void run();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        // FIXME: need mutex
 | 
					        // FIXME: need mutex
 | 
				
			||||||
        std::set<WebSocket*> getClients() { return _clients; }
 | 
					        std::set<std::shared_ptr<WebSocket>> getClients() { return _clients; }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    private:
 | 
					    private:
 | 
				
			||||||
        void handleConnection(int fd);
 | 
					        void handleConnection(int fd);
 | 
				
			||||||
@@ -43,6 +44,6 @@ namespace ix
 | 
				
			|||||||
        // socket for accepting connections
 | 
					        // socket for accepting connections
 | 
				
			||||||
        int _serverFd;
 | 
					        int _serverFd;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        std::set<WebSocket*> _clients;
 | 
					        std::set<std::shared_ptr<WebSocket>> _clients;
 | 
				
			||||||
    };
 | 
					    };
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user