Update lua versions
[ryzomcore.git] / nel / samples / net / chat / client.cpp
blob4408e5bbc30bb48679c1a6c302bdf1d41b0997ef
1 // NeL - MMORPG Framework <http://dev.ryzom.com/projects/nel/>
2 // Copyright (C) 2010 Winch Gate Property Limited
3 //
4 // This program is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU Affero General Public License as
6 // published by the Free Software Foundation, either version 3 of the
7 // License, or (at your option) any later version.
8 //
9 // This program is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU Affero General Public License for more details.
14 // You should have received a copy of the GNU Affero General Public License
15 // along with this program. If not, see <http://www.gnu.org/licenses/>.
19 * Login system example, client.
21 * This client connects to a front-end server using login system.
23 * Before running this client, the front end service sample must run,
24 * and also the NeL naming_service, time_service, login_service, welcome_service.
29 // Includes
32 #include <string>
34 #include "nel/misc/types_nl.h"
35 #include "nel/misc/debug.h"
36 #include "nel/misc/config_file.h"
37 #include "nel/misc/bit_mem_stream.h"
38 #include "nel/misc/path.h"
40 #include "nel/net/callback_client.h"
42 #ifdef NL_OS_WINDOWS
43 #include <conio.h>
44 #else
45 #include "kbhit.h"
46 #endif
48 using namespace std;
49 using namespace NLMISC;
50 using namespace NLNET;
52 // Really simple chat
53 // Do not display what you are typing
55 #ifdef NL_OS_WINDOWS
56 #define KEY_ESC 27
57 #define KEY_ENTER 13
58 #else
59 #define KEY_ESC 27
60 #define KEY_ENTER 10
61 #endif
63 #ifndef CHAT_DIR
64 # define CHAT_DIR "."
65 #endif
67 string CurrentEditString;
69 // ***************************************************************************
70 void serverSentChat (CMessage &msgin, TSockId from, CCallbackNetBase &netbase)
72 // Called when the server sent a CHAT message
73 string text;
74 msgin.serial(text);
75 printf("%s\n", text.c_str());
78 // ***************************************************************************
79 // All messages handled by this server
80 #define NB_CB 1
81 TCallbackItem CallbackArray[NB_CB] =
83 { "CHAT", serverSentChat }
87 * main
89 int main (int argc, char **argv)
91 NLMISC::CApplicationContext applicationContext;
93 CCallbackClient *Client;
95 NLMISC::CPath::addSearchPath(CHAT_DIR);
97 // Read the host where to connect in the client.cfg file
98 CConfigFile ConfigFile;
99 ConfigFile.load (NLMISC::CPath::lookup("client.cfg"));
100 string LSHost(ConfigFile.getVar("LSHost").asString());
102 // Init and Connect the client to the server located on port 3333
103 Client = new CCallbackClient();
104 Client->addCallbackArray (CallbackArray, NB_CB);
106 printf("Please wait connecting...\n");
109 CInetAddress addr(LSHost+":3333");
110 Client->connect(addr);
112 catch(const ESocket &e)
114 printf("%s\n", e.what());
115 return 0;
118 if (!Client->connected())
120 printf("Connection Error\n");
121 return 0;
123 printf("Connected.\n");
125 #ifdef NL_OS_UNIX
126 init_keyboard();
127 #endif
128 // The main loop
131 int c;
132 if (kbhit())
134 c = getch();
135 if (c == KEY_ESC)
137 break; // FINSIH
139 else if (c == KEY_ENTER)
141 CMessage msg;
142 msg.setType("CHAT");
143 msg.serial (CurrentEditString);
144 Client->send(msg);
145 CurrentEditString = "";
147 else
149 CurrentEditString += c;
152 Client->update();
154 while (Client->connected());
156 #ifdef NL_OS_UNIX
157 close_keyboard();
158 #endif
159 // Finishing
160 delete Client;