D:/Programmation/Cpp/SFML/src/SFML/AdvancedNetwork/Server.cpp

00001 
00002 //
00003 // SFML - Simple and Fast Multimedia Library
00004 // Copyright (C) 2007 Laurent Gomila (laurent.gom@gmail.com)
00005 //
00006 // This software is provided 'as-is', without any express or implied warranty.
00007 // In no event will the authors be held liable for any damages arising from the use of this software.
00008 //
00009 // Permission is granted to anyone to use this software for any purpose,
00010 // including commercial applications, and to alter it and redistribute it freely,
00011 // subject to the following restrictions:
00012 //
00013 // 1. The origin of this software must not be misrepresented;
00014 //    you must not claim that you wrote the original software.
00015 //    If you use this software in a product, an acknowledgment
00016 //    in the product documentation would be appreciated but is not required.
00017 //
00018 // 2. Altered source versions must be plainly marked as such,
00019 //    and must not be misrepresented as being the original software.
00020 //
00021 // 3. This notice may not be removed or altered from any source distribution.
00022 //
00024 
00026 // Headers
00028 #include <SFML/AdvancedNetwork/Server.hpp>
00029 #include <SFML/Network/IPAddress.hpp>
00030 #include <SFML/Network/Packet.hpp>
00031 #include <SFML/Network/SocketTCP.hpp>
00032 
00033 
00037 sfServer::~sfServer()
00038 {
00039     // Stop the server
00040     Stop();
00041 }
00042 
00043 
00047 void sfServer::Start(unsigned int Port, std::size_t MaxClients)
00048 {
00049     // If the server is already running, we stop it first
00050     if (myIsRunning)
00051         Stop();
00052 
00053     // Store server settings
00054     myPort       = Port;
00055     myMaxClients = MaxClients;
00056     myIsRunning  = true;
00057 
00058     // Run the server logic in a thread
00059     Launch();
00060 }
00061 
00062 
00066 void sfServer::Stop()
00067 {
00068     // Clear the running flag
00069     myIsRunning = false;
00070 
00071     // And wait for the thread to terminate
00072     Wait();
00073 }
00074 
00075 
00079 void sfServer::SendTo(sfSocketTCP Client, sfPacket& Packet)
00080 {
00081     // Send the packet to the client
00082     if (!Client.Send(Packet))
00083         Disconnect(Client);
00084 }
00085 
00086 
00090 void sfServer::SendToAll(sfPacket& Packet, sfSocketTCP* Exclude)
00091 {
00092     // Send the packet to each connected client
00093     for (std::list<sfSocketTCP>::iterator i = myClients.begin(); i != myClients.end(); )
00094     {
00095         sfSocketTCP Client = *i++;
00096 
00097         if (!Exclude || (Client != *Exclude))
00098         {
00099             if (!Client.Send(Packet))
00100                 Disconnect(Client);
00101         }
00102     }
00103 }
00104 
00105 
00109 void sfServer::Disconnect(sfSocketTCP Client)
00110 {
00111     // Remove from the selector
00112     mySelector.Remove(Client);
00113 
00114     // Remove the client from the list of connected clients
00115     myClients.remove(Client);
00116 
00117     // Let the derived class do something useful
00118     OnClientDisconnected(Client);
00119 
00120     // Close the socket properly
00121     Client.Close();
00122 }
00123 
00124 
00128 void sfServer::Run()
00129 {
00130     // Create a TCP socket for listening to clients connections
00131     sfSocketTCP Listener;
00132 
00133     // Listen to a port for incoming connections
00134     Listener.Listen(myPort);
00135 
00136     // Add the listener socket to the selector
00137     mySelector.Add(Listener);
00138 
00139     std::vector<sfSocketTCP> Ready;
00140     while (myIsRunning)
00141     {
00142         // Get sockets ready for reading
00143         mySelector.GetSocketsReady(Ready);
00144 
00145         // Read from every socket that is ready
00146         for (std::vector<sfSocketTCP>::iterator i = Ready.begin(); i != Ready.end(); ++i)
00147         {
00148             if (*i == Listener)
00149             {
00150                 // The listening socket is ready, meaning that we have a new connection
00151                 sfIPAddress Address;
00152                 sfSocketTCP NewClient = Listener.Accept(&Address);
00153 
00154                 // Accept it only if we haven't reached the maximum number of clients
00155                 if (myClients.size() < myMaxClients)
00156                 {
00157                     // Add it to the selector
00158                     mySelector.Add(NewClient);
00159 
00160                     // Let the derived class do something useful
00161                     OnClientConnected(Address, NewClient);
00162 
00163                     // Add the client to the list of connected clients
00164                     myClients.push_back(NewClient);
00165                 }
00166                 else
00167                 {
00168                     // Server is full : close the connection (sorry !)
00169                     NewClient.Close();
00170                 }
00171             }
00172             else
00173             {
00174                 // A client has just sent a message : read it
00175                 sfPacket Packet;
00176                 if (i->Receive(Packet))
00177                 {
00178                     // Let the derived class do something useful
00179                     OnPacketReceived(*i, Packet);
00180                 }
00181                 else
00182                 {
00183                     // Receive() returned false : that means the client has been disconnected
00184                     Disconnect(*i);
00185                 }
00186             }
00187         }
00188     }
00189 
00190     // Close all the remaining connections
00191     for (std::list<sfSocketTCP>::iterator i = myClients.begin(); i != myClients.end(); ++i)
00192         i->Close();
00193 
00194     // Close the listener
00195     Listener.Close();
00196 }
00197 
00198 
00202 void sfServer::OnClientConnected(const sfIPAddress& Client, sfSocketTCP Socket)
00203 {
00204     // Does nothing by default
00205 }
00206 
00207 
00211 void sfServer::OnPacketReceived(sfSocketTCP Socket, sfPacket& Packet)
00212 {
00213     // Does nothing by default
00214 }
00215 
00216 
00220 void sfServer::OnClientDisconnected(sfSocketTCP Socket)
00221 {
00222     // Does nothing by default
00223 }

Generated for SFML by  doxygen 1.5.2