📄 network.cpp
字号:
/*
This file is part of SWAIN (http://sourceforge.net/projects/swain).
Copyright (C) 2006 Daniel Lindstr鰉 and Daniel Nilsson
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor,
Boston, MA 02110-1301, USA.
*/
#include "stdafx.h"
#include "network.h"
#include "common_client.h"
#include "ClientPacketFactory.h"
#include "PacketHello.h"
#include "PacketBye.h"
#include "PacketSetStatus.h"
//global variables
ConnectionHandler *connectionhandler;
ClientPacketHandler *packethandler;
int server_cid = -1;
//nonglobal
static HANDLE login_semaphore = NULL;
static bool login_success = false;
static ClientPacketFactory *packetfactory;
bool initNetwork(const char *server_address, const char *name, const char *password){
PacketHello *hello = new PacketHello(name, password);
login_semaphore = CreateSemaphore(NULL, 0, 1, NULL);
packetfactory = new ClientPacketFactory();
packethandler = new ClientPacketHandler();
connectionhandler = new ConnectionHandler(packetfactory);
connectionhandler->setPacketHandler(packethandler);
connectionhandler->startSend();
connectionhandler->startReceive();
connectionhandler->startHandle();
if((server_cid = connectionhandler->connectTo(server_address, SERVER_PORT)) == -1){
showMessage(L"Network Error", L"Failed to connect to server.");
return(false);
}
hello->setCId(server_cid);
connectionhandler->sendPacket(hello);
if (!waitForLoginStatus()) {
showMessage(L"Login Failed", L"Bad username or password.");
CloseHandle(login_semaphore);
return false;
}
CloseHandle(login_semaphore);
return(true);
}
bool destroyNetwork(){
changeMyStatus(STATUS_OFFLINE);
connectionhandler->closeConnection(server_cid);
delete connectionhandler;
delete packetfactory;
delete packethandler;
return(true);
}
// This function is called by ClientPacketHandler
void setLoginStatus(bool success) {
login_success = success;
ReleaseSemaphore(login_semaphore, 1, NULL);
}
bool waitForLoginStatus(void) {
if (WaitForSingleObject(login_semaphore, 10000) == WAIT_TIMEOUT) {
return false; // No response in a long time, assume login failure
}
return login_success;
}
void changeMyStatus(STATUS s) {
printf("Setting status to %d\n", s);
PacketSetStatus *pss = new PacketSetStatus(s);
pss->setCId(server_cid);
connectionhandler->sendPacket(pss);
}
void SendBye(int cid){
PacketBye *pb = new PacketBye();
pb->setCId(cid);
connectionhandler->sendPacket(pb);
}
void incomingConnection(int address){
int cid = -1;
sockaddr_in addr;
addr.sin_addr.S_un.S_addr = address;
WCHAR caddr[17];
mbstowcs(caddr, inet_ntoa(addr.sin_addr), 17);
if(!askYesNo(L"Incomming Connection", L"Incoming connection from %s. Do you wish to accept?", caddr)) {
//todo: skicka tillbaka n錿t packet eller n錿t?
}
else{
if((cid = connectionhandler->connectTo(inet_ntoa(addr.sin_addr), CLIENT_PORT)) == -1){
printf("Error connecting to %s.\n", inet_ntoa(addr.sin_addr));
//todo: n錿t mer?
}
else{
PacketReady *pr = new PacketReady();
pr->setCId(cid);
connectionhandler->sendPacket(pr);;
}
}
}
void handleClosedConnection(int cid){
if(cid == server_cid){
showMessage(L"Network Error", L"Lost connection to server.");
//todo: B鰎 inte st鋘ga allt, ifall att en peer to peer connection 鋜 ig錸g.
exit(0);
}
//todo: om peeren d鰎..
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -