Self-made instant chat backend-write a configuration, read the configuration, instant chat backend
I am ashamed to say that I have never studied well since I have been working for more than half a year. Half a year ago, I thought about how to create a game chat module? Although I have some ideas, I just want to think about it. Now, we can use our spare time to implement it.
Affected by work, I also want to define all the define and referenced header files in one file:
1 /// 2 /// @file define.h 3 /// @author marrs(chenchengxi993@gmail.com) 4 /// @date 2017-07-13 21:57:13 5 /// 6 7 #ifndef __DEFINE_H__ 8 #define __DEFINE_H__ 9 10 #include <json/json.h>11 #include <iostream>12 #include <fstream>13 #include <sstream>14 15 namespace marrs{16 17 using namespace std;18 19 #define BASE_CONF_PATH "../conf/modular.conf"20 21 #define BASE_CONF_KEY_IP "Ip"22 23 typedef short Int_16;24 typedef int Int_32;25 typedef long Int_64;26 typedef unsigned short UInt_16;27 typedef unsigned int UInt_32;28 typedef unsigned long UInt_64;29 typedef char Char;30 typedef string String;31 typedef void Void;32 33 typedef short* pInt_16;34 typedef int* pInt_32;35 typedef long* pInt_64;36 typedef unsigned short* pUInt_16;37 typedef unsigned int* pUInt_32;38 typedef unsigned long* pUInt_64;39 typedef char* pChar; 40 typedef void* pVoid;41 42 }43 44 #endif
The first few lines compile. vimrc in the home directory. Each time a new type of file is generated, the content will be written into the file by default.
1 autocmd BufNewFile *.[ch],*.hpp,*.cpp,*.cc exec ":call Addreadme()" 2 3 function Addreadme() 4 call setline(1, " ///") 5 call append(1, " /// @file " .expand("%:t")) 6 call append(2, " /// @author marrs(chenchengxi993@gmail.com)") 7 call append(3, " /// @date ".strftime("%Y-%m-%d %H:%M:%S")) 8 call append(4, " ///") 9 call append(5, " ")10 call append(6, "#include <iostream>")11 call append(7, " ")12 call append(8, "namespace marrs{")13 call append(9, " ")14 call append(10, "using std::cout;")15 call append(11, "using std::endl;")16 call append(12, " ")17 call append(13, " ")18 call append(14, " ")19 call append(15, "}")20 endf
Next, I am used to writing configuration and reading configuration methods first.
Write a socket configuration for the ftp and Mini Search Engine you wrote before. Personal preferences are in json format, as follows:
{ "Ip" : "192.168.188.128", "Port" : 2000,}
Well, there is a little bit of content. Let's do this first. We need to add more content later.
The advantage of configuring write is that if some things that may change are written to death, the program must be re-compiled. With the configuration file, you only need to restart it. You don't even need to restart it. Just tell the program to reload the file. Next, let's write a simple method for reading configuration.
1 /// 2 /// @file conf.h 3 /// @author marrs(chenchengxi993@gmail.com) 4 /// @date 2017-07-13 21:49:14 5 /// 6 7 #ifndef __CONF_H__ 8 #define __CONF_H__ 9 10 #include "define.h"11 12 namespace marrs{13 14 class BaseConf15 {16 public:17 BaseConf();18 ~BaseConf();19 Void LoadConf();20 21 public:22 Int_32 GetVal(String str_key, Int_32 & rInt32_value);23 UInt_32 GetVal(String str_key, UInt_32 & rUInt32_value);24 String GetVal(String str_key, String & str_value);25 26 private:27 Json::Value jsnData;28 Json::FastWriter jsnWriter;29 Json::Reader jsnReader;30 };31 32 }33 34 #endif
1 /// 2 /// @file conf.cc 3 /// @author marrs(chenchengxi993@gmail.com) 4 /// @date 2017-07-13 22:32:32 5 /// 6 7 #include "conf.h" 8 9 namespace marrs{10 11 BaseConf::BaseConf()12 {13 LoadConf(); 14 }15 16 BaseConf::~BaseConf()17 {18 //do nothing19 }20 21 Void BaseConf::LoadConf()22 {23 ifstream ifs_conf;24 ifs_conf.open(BASE_CONF_PATH);25 if (!ifs_conf.good())26 {27 //todo log and exit28 return;29 }30 31 if(!jsnReader.parse(ifs_conf, jsnData))32 {33 //todo log and exit34 return;35 }36 }37 38 Int_32 BaseConf::GetVal(String str_key, Int_32 & rInt32_value)39 {40 if(jsnData.isMember(str_key))41 {42 rInt32_value = jsnData[str_key].asInt();43 }44 return rInt32_value;45 }46 47 UInt_32 BaseConf::GetVal(String str_key, UInt_32 & rUInt32_value)48 {49 if(jsnData.isMember(str_key))50 {51 rUInt32_value = jsnData[str_key].asUInt();52 }53 return rUInt32_value;54 }55 56 57 String BaseConf::GetVal(String str_key, String & str_value)58 {59 if(jsnData.isMember(str_key))60 {61 str_value = jsnData[str_key].asString();62 }63 return str_value;64 }65 66 }//end namespace
Next, we will test whether the data can be read successfully.
1 /// 2 /// @file main.cc 3 /// @author marrs(chenchengxi993@gmail.com) 4 /// @date 2017-07-13 23:11:35 5 /// 6 7 #include "base/conf.h" 8 9 using namespace marrs;10 11 int main()12 {13 BaseConf conf;14 String str_ip;15 conf.GetVal(BASE_CONF_KEY_IP, str_ip);16 cout << str_ip << endl;17 return 0;18 }19
Running result:
[ccx@ubuntu ~/chat/src]$>./main.exe 192.168.188.128
As expected.
Let's get there today. You can write a little bit in a day and always write it.
Https://github.com/ccx19930930/my_Instant_chat