#pragma comment(lib , "dll.lib")
#include "Packet.h"
#include "Channel.h"
#include <stdio.h>
///////////////////////////////////////////////////////////////
typedef struct MemberInFo
{
char *Id;
char *Pass;
char *Name;
int Idlen;
int Passlen;
int Namelen;
int C_Port;
int SmsPort;
int FilePort;
LONG C_Ip;
}MemberInFo;
MemberInFo * member = new MemberInFo();
////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////
void Load(); //Log서버 초기화 후 대기
DWORD WINAPI Send(LPVOID temp); //Login 과 Logout에게 DB서버 정보 전송
void LoginReq(Packet * pack, MsgHead msg, SOCKET sock_Log, SOCKET sock_Db);
void LoginPack(SOCKET sock_Db); //로그인 팩
void LoginUnPack(Packet * pack); //로그인 언팩
void LogoutReq(Packet * pack, MsgHead msg, SOCKET sock_Db);
int SearchAck(SOCKET sock_Log); //DB로부터 값을 받아서 리턴
void SetState(); //아이디가 있을경우 State 서버로 회원포트정보 등 전송
void SendState(Packet *Pack); //State 서버 초기화
///////////////////////////////////////////////////////////////////
int main()
{
WSADATA wsaData;
WSAStartup(MAKEWORD(2,2), &wsaData);
AllocConsole();
freopen("conout$","wt",stdout);
while(1)
{
Load();
}
WSACleanup();
delete member;
return 0;
}
void Load()
{
DWORD ThreadID;
SOCKET sock_Log = OpenSocket(LOGSV_IP_ADDR, S_LOG);
SOCKADDR_IN clientaddr={0,};
int len = sizeof(clientaddr);
SOCKET dosock;
while(1)
{
printf("대기중\n");
dosock = accept(sock_Log,(SOCKADDR *)&clientaddr,&len);
CloseHandle(CreateThread(NULL,0,Send,(LPVOID)dosock,0,&ThreadID));
}
closesocket(dosock);
closesocket(sock_Log);
}
DWORD WINAPI Send(LPVOID temp)
{
SOCKET sock_Log = (SOCKET)temp;
MsgHead msg(sock_Log); // Msgid 와 Bodylen을 가져온다. 4 + 4
Packet * pack = new Packet(sock_Log, msg.Getblen()); // Bodylen 만큼 데이터를 가져온다
printf("%d", msg.GetMsgid());
int msgid = msg.GetMsgid();
SOCKET sock_Db = OpenSocket(DBM_IP_ADDR, S_DBM,0); //Db 와의 연결
switch(msgid)
{
case LOGIN: LoginReq(pack, msg, sock_Log, sock_Db); break;
case LOGOUT: LogoutReq(pack, msg, sock_Db); break;
default: break;
}
closesocket(sock_Log);
return 0 ;
}
void LoginReq(Packet * pack, MsgHead msg, SOCKET sock_Log, SOCKET sock_Db)
{
int Ack;
LoginUnPack(pack);
LoginPack(sock_Db);
if((Ack = SearchAck(sock_Db)))
{
send(sock_Log, (char*)&Ack, sizeof(int), 0);
SetState();
closesocket(sock_Log);
}
else
{
send(sock_Log, (char*)&Ack, sizeof(int), 0);
closesocket(sock_Log);
}
delete pack;
closesocket(sock_Db);
}
void LoginUnPack(Packet * pack)
{
pack->UnPack(&member->Idlen, sizeof(member->Idlen));
member->Id = new char[member->Idlen];
pack->UnPack(member->Id, member->Idlen);
pack->UnPack(&member->Passlen, sizeof(member->Passlen));
member->Pass = new char[member->Passlen];
pack->UnPack(member->Pass, member->Passlen);
pack->UnPack(&member->C_Ip, sizeof(member->C_Ip));
pack->UnPack(&member->C_Port, sizeof(member->C_Port));
pack->UnPack(&member->SmsPort, sizeof(member->SmsPort));
pack->UnPack(&member->FilePort, sizeof(member->FilePort));
printf("ID:%s, Pass:%s, IP:%d, C_Port:%d, SPort:%d, FPort:%d", member->Id, member->Pass, member->C_Ip, member->C_Port, member->SmsPort, member->FilePort);
}
void LoginPack(SOCKET sock_Db)
{
MsgHead msg2(LOGIN);
int bodysize = sizeof(member->Idlen) + member->Idlen + sizeof(member->Passlen) + member->Passlen;
msg2.SetBlen(bodysize);
Packet * pack2 = new Packet();
pack2->Pack(&msg2, sizeof(MsgHead));
pack2->Pack(&member->Idlen, sizeof(member->Idlen));
pack2->Pack(member->Id, member->Idlen);
pack2->Pack(&member->Passlen, sizeof(member->Passlen));
pack2->Pack(member->Pass, member->Passlen);
pack2->Send(sock_Db);
delete pack2;
}
void LogoutReq(Packet * pack, MsgHead msg, SOCKET sock_Db)
{
pack->RePack(&msg, sizeof(MsgHead));
pack->Send(sock_Db);
SendState(pack);
delete pack;
closesocket(sock_Db);
}
int SearchAck(SOCKET sock_Log) //db로부터 받은 True False 반환!
{
int re;
recv(sock_Log, (char*)&re, sizeof(int),0);
printf("\n%d", re);
return re;
}
void SetState()
{
MsgHead msg(LOGIN);
int bodysize = sizeof(member->Idlen) + member->Idlen + sizeof(member->C_Ip) + sizeof(member->C_Port) + sizeof(member->SmsPort) + sizeof(member->FilePort);
msg.SetBlen(bodysize);
Packet * pack = new Packet();
pack->Pack(&msg, sizeof(MsgHead));
pack->Pack(&member->Idlen, sizeof(member->Idlen));
pack->Pack(member->Id, member->Idlen);
pack->Pack(&member->C_Ip , sizeof(member->C_Ip));
pack->Pack(&member->C_Port, sizeof(member->C_Port));
pack->Pack(&member->SmsPort, sizeof(member->SmsPort));
pack->Pack(&member->FilePort, sizeof(member->FilePort));
printf("SetState : %d, %d\n",member->C_Ip,member->C_Port);
SendState(pack);
delete pack;
}
void SendState(Packet *Pack)
{
SOCKET sock_State = OpenSocket(STATESV_IP_ADDR, S_STATE,0); //스테이트 서버와 연결
Pack->Send(sock_State);
closesocket(sock_State);
}
'API & MFC > 네트워크프로그래밍' 카테고리의 다른 글
EventSelect란? (0) | 2010.02.05 |
---|---|
ASyncSelect 예제 (1) | 2010.02.04 |
소켓통신의 라이브러리 사용 선택 시 고려사항 (0) | 2010.02.02 |
소켓통신(함수정리) (1) | 2010.01.18 |
OSI 모델 And Ip계층 (0) | 2010.01.18 |