#include "Chat.h"
const pair<int,int> Chat::ChatOutPos = pair<int,int>(2,6);
const pair<int,int> Chat::SendOutPos = pair<int,int>(2,25);
const pair<int,int> Chat::MemOutPos = pair<int,int>(68,15);
list<string> Chat::ChatRecords;
SocketList* Chat::Room = NULL;
Chat::Chat():MaxName(16),RecSocket(NULL)
{
StartSocketLib;
srand(GetTickCount());
}
Chat::~Chat(){CloseSocketLib;}
void Chat::SetOutPos(pair<int,int> pos)
{
HANDLE hStd = GetStdHandle(STD_OUTPUT_HANDLE);
COORD setPS;
setPS.X=pos.first; //设置光标位置
setPS.Y=pos.second;
SetConsoleCursorPosition(hStd,setPS);
}
void Chat::CinString(void *Receive)
{
SetOutPos(Chat::SendOutPos);
string *Result = static_cast<string*>(Receive);
*Result = "";
cin >> *Result;
}
int Chat::Menu()
{
cout << "1:创建房间" << endl;
cout << "2:进入房间" << endl;
cout << "请选择你要进行的操作:";
while(true)
{
int select = 0;
cin >> select;
if(select == 1)
{
CreateRoom();
break;
}
else if(select == 2)
{
EnterRoom();
break;
}
else
{
cout << "输入有误!请重新输入:";
}
}
return 0;
}
string Chat::SetName()
{
int num = rand() % MaxName;
string name = AllName[num];
AllName.erase(find(AllName.begin(),AllName.end(),name));
return name;
}
void Chat::CreateRoom()
{
isServer = true;
Room = new SocketList(true);
cout << "创建房间成功" << endl;
ActivateAllName();
DeleteName(myName = SetName());
NameInRoom.push_back(myName);
Fresh();
}
void Chat::ActivateAllName()
{
const int MAXNUM = 16;
const string nameArr[MAXNUM] = {"迅捷斥候","光辉女郎","末日使者","雪人骑士","刀锋意志","玛西亚之力","琴瑟仙女","寒冰射手","暗影之拳","邪恶小法师","武器大师","殇之木乃伊","卡牌大师","无极剑圣","流浪法师","不祥之刃"};
for(int i = 0; i != MAXNUM; i++)
AllName.push_back(nameArr[i]);
}
void Chat::DeleteName(string name)
{
vector<string>::iterator itr = find(AllName.begin(),AllName.end(),name);
if(itr != AllName.end())
AllName.erase(itr);
--MaxName;
}
void Chat::EnterRoom()
{
isServer = false;
cout << "请输入进入的房间IP地址:";
string RoomIp;
cin >> RoomIp;
Room = new SocketList(false,RoomIp);
cout << "进入房间成功" << endl;
Fresh();
}
void Chat::clearText(pair<int,int> pos)
{
int line = 0,length = 0;
if(pos == SendOutPos)
{
line = 5;
length = 64;
}
else if(pos == ChatOutPos)
{
line = 15;
length = 64;
}
else if(pos == MemOutPos)
{
line = 16;
length = 15;
}
for (int i = 0; i != line; ++i)
{
SetOutPos(pos);
for(int j = 0; j != length; ++j)
cout << " ";
++pos.second;
}
}
void Chat::Fresh()
{
ThreadLib::ThreadID CinThread;
string *str = new string;
system("cls");
DrawBackground();
ShowName();
while(true)
{
CinThread = ThreadLib::Create( CinString, (void*)str );
while(true)
{
detectingMsg();
if (*str != "")
{
*str = myName + "(" + getTime() + ")\n│" + *str;
if(isServer)
{
RecStrList.push_back(*str);
freshChatRecord();
}
Room->Send(*str);
clearText(SendOutPos);
*str = "";
break;
}
Sleep(1000);
}
}
delete str;
}
void Chat::SetMutLinePos(pair<int,int> pos,int i)
{
pair<int,int> newPos = pos;
newPos.second += i;
SetOutPos(newPos);
}
vector<string> Chat::SeparateMsg(string str)
{
vector<string> retStrVec;
int pos;
while((pos = str.find("#end#")) != string::npos)
{
retStrVec.push_back(str.substr(0,pos));
str.erase(0,pos+5);
}
if(str != "")
retStrVec.push_back(str);
return retStrVec;
}
bool Chat::detectingInstruct(string str)
{
if(str == "#Connected#")
{
if(isServer && RecSocket != NULL)
{
if(AllName.size() == 0)
{
RecSocket->SendData("#NoID#");
}
string newName = SetName();
NameInRoom.push_back(newName);
ShowName();
Room->Send("#ClearNameInRoom##end#");
for(vector<string>::iterator itr = NameInRoom.begin();
itr != NameInRoom.end();itr++)
Room->Send("#ShowName#" + *itr + "#end#");
RecSocket->SendData("#GetName#" + newName + "#end#");
}
return true;
}
else if(str.substr(0,9) == "#GetName#")
{
myName = str.substr(9,str.length());
return true;
}
else if(str.substr(0,10) == "#ShowName#")
{
NameInRoom.push_back(str.substr(10,str.length()));
return true;
}
else if(str == "#CheckOnline#")
{
Room->Send("#DisableName#" + myName + "#end#");
return true;
}
else if(str.substr(0,13) == "#DisableName#" && isServer)
{
DeleteName(str.substr(13,str.length()));
return true;
}
else if(str == "#NoID#")
{
MessageBox(NULL,L"人数已满16人上限",L"提示",0);
exit(0);
return true;
}
else if(str == "#ClearNameInRoom#")
{
NameInRoom.clear();
return true;
}
return false;
}
void Chat::ShowName()
{
int i = 0;
clearText(MemOutPos);
for(vector<string>::iterator itr = NameInRoom.begin();
itr != NameInRoom.end(); itr++,i++)
{
SetMutLinePos(MemOutPos,i);
cout << *itr;
}
SetOutPos(SendOutPos);
}
void Chat::freshChatRecord()
{
bool freshAllRecord = false;
const int RecordMax = 10;
bool instruct = false;
for(list<string>::iterator itr = RecStrList.begin();itr != RecStrList.end();itr++)
{
vector<string> msgVector = SeparateMsg(*itr);
for(vector<string>::iterator it = msgVector.begin();
it != msgVector.end();it++)
{
if( !(instruct = detectingInstruct(*it)))
ChatRecords.push_back(*itr);
}
if(!instruct)
{
if(ChatRecords.size() >= RecordMax-1)
{
ChatRecords.pop_front();
clearText(ChatOutPos);
int i=1;
for(list<string>::iterator itr = ChatRecords.begin();itr != ChatRecords.end();itr++,i++)
{
SetMutLinePos(ChatOutPos,2*i-2);
cout << *itr;
}
}
else
{
SetMutLinePos(ChatOutPos,2*ChatRecords.size()-2);
cout << *itr;
}
}
else
{
ShowName();
}
}
SetOutPos(SendOutPos);
}
void Chat::detectingMsg()
{
RecStrList = Room->Listening(&RecSocket);
if(RecStrList.size() != 0)
{
freshChatRecord();
}
}
string Chat::getTime()
{
time_t t = time(0);
char tmp[64];
strftime( tmp, sizeof(tmp), "%X",localtime(&t));
return string(tmp);
}
void Chat::DrawBackground()
{
cout<< "┌────────────────────────────────┬─────┬─┬─┬─┐" << endl
<< "│C++技术交流群 ByDuo(东莞理工学院 2014级软件工程 赖静朝) │ 举报 ▼│─│□│×│" << endl
<< "├───┐ ├─────┴─┴─┴─┤" << endl
<< "│ │ │招广告摊位 │" << endl
<< "│ 聊天 │ 公告 相册 文件 直播 卍设置 │请联系:ljcduo@163.com│" << endl
<< "│ └────────────────────────────┼───────────┤" << endl
<< "│ │ 公告 │" << endl
<< "│ │走别人的路, │" << endl
<< "│ │让别人无路可走。 │" << endl
<< "│ │ │" << endl
<< "│ │ │" << endl
<< "│ │ │" << endl
<< "│ │ │" << endl
<< "│ ├───────────┤" << endl
<< "│ │群成员 │" << endl
<< "│
没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论
收起资源包目录
Chat.rar (79个子文件)
Chat
Chat.sdf 30.71MB
Release
Chat.lastbuildstate 89B
Chat.pdb 731KB
CL.write.1.tlog 1010B
CL.read.1.tlog 45KB
Chat.exe.intermediate.manifest 381B
mt.read.1.tlog 656B
Chat.exe 44KB
mt.command.1.tlog 470B
cl.command.1.tlog 1KB
main.obj 1.01MB
Chat.log 3KB
Chat.write.1.tlog 0B
link.write.1.tlog 694B
ThreadLibFunctions.obj 820KB
link.command.1.tlog 2KB
link.read.1.tlog 3KB
mt.write.1.tlog 296B
vc100.pdb 380KB
ThreadLibFunctions.h 6KB
Chat.cpp 10KB
SocketList.cpp 3KB
Chat.sln 874B
SocketList.h 850B
Platform.h 759B
ThreadLibFunctions.cpp 1KB
Chat.vcxproj.filters 2KB
main.cpp 138B
ThreadLibMutex.h 3KB
Chat.vcxproj.user 143B
Chat.h 2KB
ThreadLib.h 458B
ipch
chat-5b2f69c5
chat-30c77d2c.ipch 58.5MB
Debug
link.5580-cvtres.read.1.tlog 2B
vc100.idb 867KB
Chat.lastbuildstate 87B
Chat.pdb 1.89MB
CL.write.1.tlog 4KB
CL.read.1.tlog 112KB
Chat.exe.intermediate.manifest 381B
mt.read.1.tlog 318B
rc.write.1.tlog 298B
TCPSocket.obj 138KB
Chat.exe.embed.manifest 406B
rc.read.1.tlog 290B
Chat.exe 206KB
Chat.vcxprojResolveAssemblyReference.cache 707B
Chat.Build.CppClean.log 2KB
mt.command.1.tlog 372B
cl.command.1.tlog 3KB
link-cvtres.read.1.tlog 2B
Chat_manifest.rc 198B
link.5580.read.1.tlog 2B
main.obj 59KB
Chat.log 3KB
Chat.write.1.tlog 0B
link.write.1.tlog 1KB
ThreadLibFunctions.obj 176KB
Chat.exe.embed.manifest.res 472B
link.6696-cvtres.read.1.tlog 2B
link-cvtres.write.1.tlog 2B
link.6696-cvtres.write.1.tlog 2B
link.command.1.tlog 2KB
rc.command.1.tlog 510B
link.read.1.tlog 4KB
link.5580.write.1.tlog 2B
Chat.ilk 1.42MB
mt.write.1.tlog 318B
link.6696.write.1.tlog 2B
link.6696.read.1.tlog 2B
link.5580-cvtres.write.1.tlog 2B
SocketList.obj 484KB
Chat.obj 797KB
vc100.pdb 412KB
TCPSocket.h 814B
ThreadException.h 2KB
Chat.vcxproj 5KB
Chat.suo 33KB
TCPSocket.cpp 2KB
共 79 条
- 1
资源评论
梦幻DUO
- 粉丝: 504
- 资源: 6
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功