Esempio n. 1
0
void CRemote::Run()
{
	TCPClient* client = listener->Accept(NULL);
	while (*run)
	{
		if (client != NULL)
		{
			client->Send(head,head.GetLength());
			CString myServer;
			for (int i = 0; i < document->getServerCount(); i++)
			{
				myServer.Format(server,document->getServer(i)->getExecutable());
				client->Send(myServer, myServer.GetLength());
			}
			client->Send(tail,tail.GetLength());
			delete client;
			client = NULL;
		}
		Sleep(1000); // timeout for reconnects
		client = listener->Accept(NULL);
	}
}
void ReadCB(const NetPacket& packet, const unsigned char* buf, void* userdata)
{
    char senddata[256] = {0};
    TCPClient* client = (TCPClient*)userdata;
    sprintf(senddata, "****recv server data(%p,%d)", client, packet.datalen);
    fprintf(stdout, "%s\n", senddata);
    NetPacket tmppack = packet;
    tmppack.datalen = (std::min)(strlen(senddata), sizeof(senddata) - 1);
    std::string retstr = PacketData(tmppack, (const unsigned char*)senddata);
    if (client->Send(&retstr[0], retstr.length()) <= 0) {
        fprintf(stdout, "(%p)send error.%s\n", client, client->GetLastErrMsg());
    }
    fprintf(stdout, "call time %d\n", ++call_time);
    //if (call_time > 50) {
    //    is_exist = true;
    //}
}
void ReConnectCB(NET_EVENT_TYPE eventtype, void* userdata)
{
    TCPClient* client = (TCPClient*)userdata;
    if (NET_EVENT_TYPE_RECONNECT == eventtype) {
        fprintf(stdout, "succeed reconnect.\n");
		char senddata[256];
        memset(senddata, 0, sizeof(senddata));
        sprintf(senddata, "client(%p) call %d", client, ++call_time);
        NetPacket packet;
        packet.header = 0x01;
        packet.tail = 0x02;
        packet.datalen = (std::min)(strlen(senddata), sizeof(senddata) - 1);
        std::string str = PacketData(packet, (const unsigned char*)senddata);
        if (client->Send(&str[0], str.length()) <= 0) {
            fprintf(stdout, "(%p)send error.%s\n", client, client->GetLastErrMsg());
        } else {
            fprintf(stdout, "send succeed:%s\n", senddata);
        }
    } else {
        fprintf(stdout, "server disconnect.\n");
    }
}