1 Star 1 Fork 1

笨笨的鸡蛋/tt-code-fragment

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
test_client.cpp 4.02 KB
一键复制 编辑 原始数据 按行查看 历史
笨笨的鸡蛋 提交于 2015-07-24 07:13 . add some ocde with smart pointer
/*================================================================
* Copyright (C) 2014 All rights reserved.
*
* 文件名称:test_client.cpp
* 创 建 者:Zhang Yuanhao
* 邮 箱:bluefoxah@gmail.com
* 创建日期:2014年12月30日
* 描 述:
*
================================================================*/
#include <vector>
#include <iostream>
#include "ClientConn.h"
#include "netlib.h"
#include "TokenValidator.h"
#include "Thread.h"
#include "IM.BaseDefine.pb.h"
#include "IM.Buddy.pb.h"
#include "playsound.h"
#include "Common.h"
#include "Client.h"
using namespace std;
#define MAX_LINE_LEN 1024
static string g_login_domain = "http://127.0.0.1:8080";
static string g_cmd_string[10];
static int g_cmd_num;
static CClient* g_pClient = NULL;
static void split_cmd(const char* buf)
{
int len = strlen(buf);
string element;
g_cmd_num = 0;
for (int i = 0; i < len; i++) {
if (buf[i] == ' ' || buf[i] == '\t') {
if (!element.empty()) {
g_cmd_string[g_cmd_num++] = element;
element.clear();
}
} else {
element += buf[i];
}
}
// put the last one
if (!element.empty()) {
g_cmd_string[g_cmd_num++] = element;
}
}
static void print_help()
{
printf("Usage:\n");
printf("login user_name user_pass\n");
/*
printf("connect serv_ip serv_port user_name user_pass\n");
printf("getuserinfo\n");
printf("send toId msg\n");
printf("unreadcnt\n");
*/
printf("close\n");
printf("quit\n");
}
static void doLogin(const string& strName, const string& strPass)
{
try{
g_pClient = new CClient(strName, strPass, g_login_domain);
}catch(...){
printf("get error while alloc memory\n");
PROMPTION;
return;
}
g_pClient->connect();
}
static void do_register(const string& name, const string& passwd)
{
try{
g_pClient = new CClient(name, passwd, g_login_domain);
g_pClient->connect();
}catch(const std::exception& exp){
cout << "Exception encountered: " << exp.what() << endl;
}
}
static void exec_cmd()
{
if (g_cmd_num == 0) {
return;
}
if(g_cmd_string[0] == "login"){
if(g_cmd_num == 3){
doLogin(g_cmd_string[1], g_cmd_string[2]);
}else{
print_help();
}
}else if(g_cmd_string[0] == "reg"){
if(g_cmd_num == 3){
do_register(g_cmd_string[1], g_cmd_string[2]);
}else{
print_help();
}
}else if (strcmp(g_cmd_string[0].c_str(), "close") == 0) {
g_pClient->close();
}else if (strcmp(g_cmd_string[0].c_str(), "quit") == 0) {
exit(0);
}else if(strcmp(g_cmd_string[0].c_str(), "list") == 0) {
printf("+---------------------+\n");
printf("| 用户名 |\n");
printf("+---------------------+\n");
CMapNick2User_t mapUser = g_pClient->getNick2UserMap();
auto it = mapUser.begin();
for(;it!=mapUser.end();++it){
uint32_t nLen = 21 - it->first.length();
printf("|");
for(uint32_t i=0; i<nLen/2; ++it) {
printf(" ");
}
printf("%s", it->first.c_str());
for(uint32_t i=0; i<nLen/2; ++it) {
printf(" ");
}
printf("|\n");
printf("+---------------------+\n");
}
}
else {
print_help();
}
}
class CCmdThread : public CThread
{
public:
void OnThreadRun()
{
string line;
cout << PROMPT << flush;
while (true)
{
getline(cin, line);
if (line.size() == 0) {
cout << PROMPT << flush; //add here to make the shell quicker
continue;
}
split_cmd(line.c_str());
exec_cmd();
cout << PROMPT << flush;
}
}
private:
char m_buf[MAX_LINE_LEN];
};
int main(int argc, char* argv[])
{
// play("message.wav");
printf("pid is %d\n", getpid());
CCmdThread cmdThread;
cmdThread.StartThread();
signal(SIGPIPE, SIG_IGN);
int ret = netlib_init();
if (ret == NETLIB_ERROR)
return ret;
netlib_eventloop();
return 0;
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
C++
1
https://gitee.com/benben-de-eggs/tt-code-fragment.git
git@gitee.com:benben-de-eggs/tt-code-fragment.git
benben-de-eggs
tt-code-fragment
tt-code-fragment
master

搜索帮助