代码拉取完成,页面将自动刷新
/*================================================================
* 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;
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。