#include "stdafx.h"
#include <iostream>
#include <winsock.h>
#include <string.h>
#include <windows.h>
#include "common.h"
#include "md5.h"
#include "packet_push.h"
#include "packet_pop.h"
#include "msg_handler.h"
#pragma comment(lib,"ws2_32.lib")
using namespace std;
char g_write_buff[MAX_PACKET_SIZE] = {0};
char g_read_buff[MAX_PACKET_SIZE] = {0};
int g_write_len = 0;
int g_read_offset = 0;
static MyHandler s_handler;
SOCKET my_sockfd;
void EncodePacket(......)
{
......
}
void ShowUI()
{
cout << "\t**********************************************************************************************************"<<endl;
cout << "\t* 测试 *"<<endl;
cout << "\t**********************************************************************************************************"<<endl;
cout << "\t*************************************模拟GM发送登录信息测试*****************************************"<<endl;
cout << "\t*************************************模拟GM查询服务器表测试*****************************************"<<endl;
cout << "\t***************************************************q/Q***************************************************"<<endl;
cout << "\t**********************************************************************************************************"<<endl;
cout << "\t*请输入选择测试内容的序号"<<endl;
}
int GMLoginTest() // 模拟GM发送登录信息测试
{
CPacketPush p_push;
struct net_packet *client_packet = (struct net_packet * )g_write_buff;
p_push.Init(client_packet ->pb,MAX_PACKET_BODY_SIZE);
p_push.PushStr("GM01");
MD5 md5("1qaz2wsx");
p_push.PushStr(md5.md5().c_str());
client_packet->type = C2S_USER_LOGIN_GM;
client_packet->pblen = p_push.GetLength();
int size = sizeof(struct net_packet)+client_packet->pblen;
EncodePacket((char*)client_packet,size);
int ret = send(my_sockfd,(const char*)client_packet ,size,0);
if(ret == SOCKET_ERROR)
{
cout<<"send failed"<<endl;
}
memset(g_write_buff,0,sizeof(g_write_buff));
return 0;
}
void ParsePacket(struct net_packet *client_packet)
{
if(NULL == client_packet)
return;
int type = client_packet->type;
int pblen = client_packet->pblen;
return;
}
DWORD WINAPI SendThread(LPVOID lpParameter)
{
char *input = new char[32];
while(1)
{
ShowUI();
cin>>input;
if('q' == *input || 'Q' == *input)
break
else if('1' == *input)
(void)GMLoginTest();
else if('2' == *input)
(void)GMQueryServIdListTest();
else
{
cout<<"请重新输入"<<endl;
continue;
}
}
delete []input;
return 0;
}
int _tmain(int argc,_TCHAR* argv[])
{
static bool bReadComplete = true;
static int leftBytes = 0;
MSG_HANDLER handler;
struct net_packet *client_packet = NULL;
WSADATA Ws;
struct sockaddr_in ServerAddr;
int ret;
s_handler.RegHandler(X2C_USER_LOGIN,X2CHandle::HandleX2CLoginResult);
s_handler.RegHandler(X2C_GM_QUERY_SERV_ID_LIST,X2CHandle::HandleX2CQueryServIdList);
if(WSAStartup(MAKEWORD(2,2),&Ws)!=0)
{
cout<<"Init fail"<<endl;
return -1;
}
my_sockfd = socket(AF_INET,SOCK_STREAM,IPPROTO_TCP);
if(my_sockfd == INVALID_SOCKET)
{
cout <<"creat fail "<<endl;
return -2;
}
ServerAddr.sin_family = AF_INET;
ServerAddr.sin_port = htons(SERVER_LISTEN_PORT);
ServerAddr.sin_addr.s_addr = inet_addr(SERV_IP_ADDR);
memset(ServerAddr.sin_zero,0,8);
ret = connet(my_sockfd,(struct sockaddr*)&ServerAddr,sizeof(ServerAddr));
if(ret == SOCK_ERROR)
{
cout <<"connect fail "<<endl;
return -3;
}
else
cout <<"connect to server OK "<<endl;
HANDLE h1 = ::CreatThread(NULL,0,SendThread,NULL,0,NULL);
//send & receive data
int recv_type;
int recv_len;
while(1)
{
int n = recv(my_sockfd,&g_read_buff[g_read_offset],MAX_PACKET_SIZE - g_read_offset,0);
cout<<"n = "<<n<<endl;
if(n == SOCKET_ERROR)
{
cout <<"receive data fail "<<endl;
break;
}
if(bReadComplete)
{
client_packet = (struct sockaddr*)g_read_buff;
recv_type = client_packet ->type;
recv_len = client_packet ->pblen;
if(n<recv_len + MAX_HEAD_SIZE )
{
bReadComplete = false;
leftBytes = recv_len +MAX_HEAD_SIZE -n;
g_read_offset+=n;
continue;
}
}
else
{
if(n<leftBytes)
{
leftBytes-=n;
g_read_offset+=n;
continue;
}
else
{
bReadComplete = true;
leftBytes = 0;
}
}
handler = s_handler.GetHandler(recv_type);
if(NULL!=handler)
ret = handler(client_packet);
memset(g_read_buff,0,sizeof(g_read_buff));
g_read_offset = 0;
}
::CloseHandle(h1);
return 0;
}