1 // NeL - MMORPG Framework <http://dev.ryzom.com/projects/nel/>
2 // Copyright (C) 2010 Winch Gate Property Limited
4 // This source file has been modified by the following contributors:
5 // Copyright (C) 2014 Jan BOON (Kaetemi) <jan.boon@kaetemi.be>
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/>.
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"
30 # ifndef NL_COMP_MINGW
34 #endif // NL_OS_WINDOWS
37 using namespace NLMISC
;
38 using namespace NLNET
;
46 // Must be a pointer to control when to start listening socket and when stop it
47 CCallbackServer
*Server
;
48 vector
<TSockId
> Clients
;
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
);
71 // ***************************************************************************
72 void clientSentChat (CMessage
&msgin
, TSockId from
, CCallbackNetBase
&netbase
)
74 // Called when a client sent a CHAT message
78 msgout
.setType("CHAT");
80 for (uint i
= 0; i
< Clients
.size(); ++i
)
81 Server
->send(msgout
, Clients
[i
]);
84 // ***************************************************************************
85 // All messages handled by this server
87 TCallbackItem CallbackArray
[NB_CB
] =
89 { "CHAT", clientSentChat
}
94 // ***************************************************************************
95 class CChatService
: public IService
101 // Init the server on port 3333
102 Server
= new CCallbackServer();
104 Server
->setConnectionCallback (clientWantsToConnect
, NULL
);
105 Server
->setDisconnectionCallback (clientWantsToDisconnect
, NULL
);
106 Server
->addCallbackArray (CallbackArray
, NB_CB
);
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).
122 // Must delete the server here
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
, "");