Update lua versions
[ryzomcore.git] / nel / samples / net / chat / server.cpp
blob1372f75a100dca7b79aa9a59d996dbed2fa9ec26
1 // NeL - MMORPG Framework <http://dev.ryzom.com/projects/nel/>
2 // Copyright (C) 2010 Winch Gate Property Limited
3 //
4 // This source file has been modified by the following contributors:
5 // Copyright (C) 2014 Jan BOON (Kaetemi) <jan.boon@kaetemi.be>
6 //
7 // This program is free software: you can redistribute it and/or modify
8 // it under the terms of the GNU Affero General Public License as
9 // published by the Free Software Foundation, either version 3 of the
10 // License, or (at your option) any later version.
12 // This program is distributed in the hope that it will be useful,
13 // but WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 // GNU Affero General Public License for more details.
17 // You should have received a copy of the GNU Affero General Public License
18 // along with this program. If not, see <http://www.gnu.org/licenses/>.
20 #include <string>
22 #include "nel/misc/common.h"
23 #include "nel/misc/path.h"
25 // contains the service base class
26 #include "nel/net/service.h"
27 #include "nel/net/callback_server.h"
29 #ifdef NL_OS_WINDOWS
30 # ifndef NL_COMP_MINGW
31 # define NOMINMAX
32 # endif
33 # include <windows.h>
34 #endif // NL_OS_WINDOWS
36 using namespace std;
37 using namespace NLMISC;
38 using namespace NLNET;
40 #ifndef CHAT_DIR
41 # define CHAT_DIR ""
42 #endif
44 // THE SERVER
46 // Must be a pointer to control when to start listening socket and when stop it
47 CCallbackServer *Server;
48 vector<TSockId> Clients;
50 // MESSAGES
52 // ***************************************************************************
53 void clientWantsToConnect ( TSockId from, void *arg )
55 // Called when a client wants to connect
56 Clients.push_back (from);
59 // ***************************************************************************
60 void clientWantsToDisconnect ( TSockId from, void *arg )
62 // Called when a client wants to disconnect
63 for (uint i = 0; i < Clients.size(); ++i)
64 if (Clients[i] == from)
66 Clients.erase(Clients.begin()+i);
67 return;
71 // ***************************************************************************
72 void clientSentChat (CMessage &msgin, TSockId from, CCallbackNetBase &netbase)
74 // Called when a client sent a CHAT message
75 string text;
76 msgin.serial(text);
77 CMessage msgout;
78 msgout.setType("CHAT");
79 msgout.serial(text);
80 for (uint i = 0; i < Clients.size(); ++i)
81 Server->send(msgout, Clients[i]);
84 // ***************************************************************************
85 // All messages handled by this server
86 #define NB_CB 1
87 TCallbackItem CallbackArray[NB_CB] =
89 { "CHAT", clientSentChat }
92 // SERVICE
94 // ***************************************************************************
95 class CChatService : public IService
97 public:
99 void init ()
101 // Init the server on port 3333
102 Server = new CCallbackServer();
103 Server->init (3333);
104 Server->setConnectionCallback (clientWantsToConnect, NULL);
105 Server->setDisconnectionCallback (clientWantsToDisconnect, NULL);
106 Server->addCallbackArray (CallbackArray, NB_CB);
109 bool update ()
111 // this function is called every "loop". you return true if you want
112 // to continue or return false if you want to exit the service.
113 // the loop is called evenly (by default, at least one time per second).
115 Server->update();
117 return true;
120 void release ()
122 // Must delete the server here
123 delete Server;
127 // this macro is the "main". the first param is the class name inherited from IService.
128 // the second one is the name of the service used to register and find the service
129 // using the naming service. the third one is the port where the listen socket will
130 // be created. If you put 0, the system automatically finds a port.
131 NLNET_SERVICE_MAIN (CChatService, "CS", "chat_service", 0, EmptyCallbackArray, CHAT_DIR, "");