コード例 #1
0
void closeConnection()
{
	if (cSocket.Close() != 0)
	{
		CCLog("ODSocket :closeConnection Close Error,errCode = %d!",cSocket.GetError());
	}
	if (cSocket.Clean() != 0)
	{
		CCLog("ODSocket :closeConnection Clean Error,errCode = %d!",cSocket.GetError());
	}
	//
	g_NetworkStat = NetworkStat_NotConnect;
	CCLog(" -----Network closed-----");
	//
	g_NetworkStat = NetworkStat_NotConnect;


	MessageAutoLock lock;
	RequestMessage* temp;
	int count = 0;
	while(!requestMsgQueue.empty())
	{
		count ++;
		temp = requestMsgQueue.front();
		requestMsgQueue.pop();
		delete temp;
	}
	CCLog("%s --> throwed %d request packet(s)",__FUNCTION__,count);


	// 这也不需要,因为ODSocket::Clean()有调用这个函数
//#ifdef WIN32
//	WSACleanup();
//#endif
}
コード例 #2
0
bool connectToServer()
{
	if (g_NetworkStat != NetworkStat_NotConnect)
		return true;

	if (cSocket.Init() == -1)
	{
		CCLog("\n==============================================================");
		CCLog("ODSocket: connectToServer Init error,errorCode = %d!", cSocket.GetError());
		CCLog("==============================================================\n");
		// 通知链接失败
		GlobalNetworkMsgDispacher::getMsgDispacher()->addPakage(new GlobalNetPackage(Type_Conncet_Err,NULL,NULL));
		return false;
	}
	if (cSocket.Create(AF_INET,SOCK_STREAM,0) == false)
	{
		CCLog("\n==============================================================");
		CCLog("ODSocket: connectToServer Create error,errorCode = %d!",cSocket.GetError());
		CCLog("==============================================================\n");

		// 通知链接失败
		GlobalNetworkMsgDispacher::getMsgDispacher()->addPakage(new GlobalNetPackage(Type_Conncet_Err,NULL,NULL));
		return false;
	}
	if (cSocket.Connect(NetWorkThreadProc::getProc()->getCurrentIp(),NetWorkThreadProc::getProc()->getCurrentPort()) == false)
	{
		CCLog("\n==========================================================================");
		CCLog("ODSocket: connectToServer[%s:%d] Connect error,errorCode = %d!",NetWorkThreadProc::getProc()->getCurrentIp(),NetWorkThreadProc::getProc()->getCurrentPort(),cSocket.GetError());
		CCLog("============================================================================\n");

		closeConnection();

		// 通知链失败
		GlobalNetworkMsgDispacher::getMsgDispacher()->addPakage(new GlobalNetPackage(Type_Conncet_Err,NULL,NULL));

		return false;
	}
	CCLog("\n\n");
	CCLog("\n==============================================================");
	CCLog("ODSocket : connectToServer %s-%d Success!",NetWorkThreadProc::getProc()->getCurrentIp(),NetWorkThreadProc::getProc()->getCurrentPort());
	CCLog("==============================================================\n");
	CCLog("\n\n");
	//cSocket.setT

	//设置状态
	g_NetworkStat = NetworkStat_Connected;

	// 设置超时
	{
		cSocket.SetRecvTimeOut(RECV_TIME_OUT);
		cSocket.SetSendTimeOut(SEND_TIME_OUT);
	}

	// 这个在init时已经调用了,这里不需要了,而且WSAStartup应该是winSocket第一个调用的函数
	// 所以写在这里也是不对的
//#ifdef WIN32
//	WORD wVersionRequested;
//	WSADATA wsaData;
//	int err;
//
//	/* Use the MAKEWORD(lowbyte, highbyte) macro declared in Windef.h */
//	wVersionRequested = MAKEWORD(2, 2);
//
//	err = WSAStartup(wVersionRequested, &wsaData);
//	if (err != 0) {
//		/* Tell the user that we could not find a usable */
//		/* Winsock DLL.                                  */
//		CCLog("WSAStartup failed");
//
//		closeConnection();
//
//		return false;
//	}
//#endif

	// 通知链接成功
	GlobalNetworkMsgDispacher::getMsgDispacher()->addPakage(new GlobalNetPackage(Type_Conncet_Suc,NULL,NULL));

	return true;
}