Ejemplo n.º 1
0
void LocalPC::tcpServerReceiveData(void *tcp, char *buffer, int size)
{
    DEBUG_OUTPUT("[LocalPC]receive:\n%s\n", buffer_format(buffer, size));
    HeartbeatProtocol protocol;
    Heartbeat *hb = protocol.find(buffer, size);
    if(hb!=NULL)
    {
        delete hb;

        char *p = NULL;
        int size = 0;
        bool slave = isSlave();
        double timePoint = getSetupTime();
        Heartbeat *t = protocol.makeHeartbeat(slave, timePoint);
        if(NULL!=t)
        {
            if(t->makeBuffer(&p, size))
            {
                TcpClient *client = (TcpClient *)tcp;
                client->send(p, size);
                delete p;
            }
        }
    }
}
Ejemplo n.º 2
0
void tcpUpdate(float dt)
{
	accumulated_time += dt;
	if (accumulated_time >= time_to_send_heart_beat)
	{
		//发送心跳包
		uint32_t empty = 4;
		tcp_client.send((unsigned char*)&empty, 4);

		accumulated_time -= time_to_send_heart_beat;
	}

	if (stage == to_receive_size)
	{
		if (tcp_client.receive((unsigned char*)&data_size, 4) == 0)
		{
			return;
		}
		data_size -= 4; //后面数据的具体长度是要减去头的4的
		receive_data = new unsigned char[data_size];
		stage = to_receive_data;
	}

	if (stage == to_receive_data)
	{
		if (tcp_client.receive(receive_data, data_size) == 0)
		{
			return;
		}

		boids::BoidsMessageHeader __msg;
		if (__msg.ParseFromArray(receive_data, data_size))
		{
			switch (__msg.type())
			{
				HANDLE_MSG(boids::AUTO_MATCH_RESPONSE, boids::MatchResponse, autoMatchResponse);
			default:
				cocos2d::log("[ERROR] unrecognized msg type: %d", __msg.type());
				break;
			}
		}
		else
		{
			cocos2d::log("[ERROR] receive a packet but not a BoidsMessageHeader. data_len: %d", data_size);
		}

		delete receive_data;
		stage = to_receive_size;
	}
}
Ejemplo n.º 3
0
void SendMsg(boids::MessageType msg_type, P& proto)
{
	boids::BoidsMessageHeader proto_with_header;
	proto_with_header.set_type(msg_type);
	proto.SerializeToString(proto_with_header.mutable_data());

	int size = proto_with_header.ByteSize();
	size += 4;

	unsigned char* send_buf = new unsigned char[size];
	*((uint32_t*)send_buf) = size;

	proto_with_header.SerializeWithCachedSizesToArray((google_lalune::protobuf::uint8*)(send_buf + 4));
	tcp_client.send(send_buf, size);
}
Ejemplo n.º 4
0
int main(int argc, char** argv){
  if (argc < 2) {
    printf ("ERROR: Chamada incorreta.\nEscreva '<nome do programa> <endereço IP> <porta>'.\n");
    return 0;
  }

  receivingBuffer = sendingBuffer = new char[512];
  matrix clientMatrix;

  TcpClient client;
  client.connect (argv[1], atoi(argv[2]));

  while (client.receive (receivingBuffer, textLength)) {
    cout << receivingBuffer;
    if (receivingBuffer[textLength-1]  == ':'){
      cin >> sendingBuffer;
      client.send (sendingBuffer);
    }
  }
Ejemplo n.º 5
0
int _tmain(int argc, _TCHAR* argv[])
{
    TcpClient client;

    TcpSocketCallback_t callback;
    callback.fn_recv   = recvData;
    callback.fn_notify = netNotify;
    callback.usrData   = NULL;
    client.setCallback(callback);

    InetAddress serverAddr("127.0.0.1", 58888);
    client.connect(serverAddr);

    connEvent = CreateEvent(NULL, FALSE, FALSE, NULL);
    while (!connectServer) {
        WaitForSingleObject(connEvent, INFINITE);
        if (reconnect) {
            client.connect(serverAddr);
        }
    }

    int i = 60;
    while (i > 0) { 
        i--;
        Sleep(1000);
        string msg = "hi, I am client";
        client.send(msg.c_str(), msg.size());
    }

    client.disconnect();
    if (connEvent) {
        CloseHandle(connEvent);
        connEvent = NULL;
    }
    return 0;
}