コード例 #1
0
int main(int argc, char *argv[])
{
	/*
	  REMOTESOCKET setup
	 */

	CUDPSocket remoteSocket;
	remoteSocket.Initialise();
	remoteSocket.MakeNonBlocking();
	//The socket must be bound so that we know the port number
	remoteSocket.Bind(REMOTEPORT);
	//Define where we are sending the data
	remoteSocket.SetDestinationAddress("127.0.0.1", LOCALPORT);

	printf("Receiving data FROM: [%u]\n\n", LOCALPORT, REMOTEPORT);

	// Clear the buffer memory
	byte receivingBuffer[sizeof(MyPacket_t)];
	memset(receivingBuffer, 0x0, sizeof(MyPacket_t));

	byte acknowledgementBuffer[sizeof(MyPacket_t)];
	memset(acknowledgementBuffer, 0x0, sizeof(MyPacket_t));

	// Initialise the packet
	MyPacket_t receivePacket = MyPacket_t();

	/* infinite loop */
	while(true)
	{
		// RECEIVE

		/* receive packet in to the buffer */
		int receiveResult = remoteSocket.Receive(receivingBuffer);

		// if n>0 we have got some data
		if(receiveResult > 0)
		{
			// got some data
			memcpy(&receivePacket, receivingBuffer, sizeof(MyPacket_t));
			printf("Received from IP: [%s] PORT: [%u]\n",
				inet_ntoa(remoteSocket.GetDestinationAddress().sin_addr),
				ntohs(remoteSocket.GetDestinationAddress().sin_port));

			int sendResult = remoteSocket.Send(receivingBuffer);
			// data has been sent
			if(sendResult > 0)
			{
				printf("Sent an acknowldment for ID %d\n", receivePacket.ID);
			}
		}

		usleep(1000000);
	}

	return 0;
}
コード例 #2
0
ファイル: network.cpp プロジェクト: JerryZhou/Stratagus
/**
**  Called if message for the network is ready.
**  (by WaitEventsOneFrame)
*/
void NetworkEvent()
{
	if (!IsNetworkGame()) {
		NetworkInSync = true;
		return;
	}
	// Read the packet.
	unsigned char buf[1024];
	CHost host;
	int len = NetworkFildes.Recv(&buf, sizeof(buf), &host);
	if (len < 0) {
		DebugPrint("Server/Client gone?\n");
		// just hope for an automatic recover right now..
		NetworkInSync = false;
		return;
	}

	// Setup messages
	if (NetConnectRunning) {
		if (NetworkParseSetupEvent(buf, len, host)) {
			return;
		}
	}
	const unsigned char msgtype = buf[0];
	if (msgtype == MessageInit_FromClient || msgtype == MessageInit_FromServer) {
		return;
	}
	NetworkParseInGameEvent(buf, len, host);
}
コード例 #3
0
ファイル: network.cpp プロジェクト: JerryZhou/Stratagus
/**
**  Cleanup network.
*/
void ExitNetwork1()
{
	if (!IsNetworkGame()) { // No network running
		return;
	}

#ifdef DEBUG
	printStatistic(NetworkFildes.getStatistic());
	NetworkFildes.clearStatistic();
	NetworkStat.print();
#endif

	NetworkFildes.Close();
	NetExit(); // machine dependent setup

	NetworkInSync = true;
	NetPlayers = 0;
	HostsCount = 0;
}
コード例 #4
0
ファイル: network.cpp プロジェクト: JerryZhou/Stratagus
/**
**  Send message to all clients.
**
**  @param packet       Packet to send.
**  @param numcommands  Number of commands.
*/
static void NetworkBroadcast(const CNetworkPacket &packet, int numcommands, int player = 255)
{
	const unsigned int size = packet.Size(numcommands);
	unsigned char *buf = new unsigned char[size];
	packet.Serialize(buf, numcommands);

	// Send to all clients.
	if (NetConnectType == 1) { // server
		for (int i = 0; i < HostsCount; ++i) {
			const CHost host(Hosts[i].Host, Hosts[i].Port);
			if (Hosts[i].PlyNr == player) {
				continue;
			}
			NetworkFildes.Send(host, buf, size);
		}
	} else { // client		
		const CHost host(Hosts[HostsCount - 1].Host, Hosts[HostsCount - 1].Port);
		NetworkFildes.Send(host, buf, size);
	}
	delete[] buf;
}
コード例 #5
0
ファイル: network.cpp プロジェクト: JerryZhou/Stratagus
/**
**  Initialize network port.
*/
void InitNetwork1()
{
	CNetworkParameter::Instance.FixValues();

	NetInit(); // machine dependent setup

	// Our communication port
	const int port = CNetworkParameter::Instance.localPort;
	const char *NetworkAddr = NULL; // FIXME : bad use
	const CHost host(NetworkAddr, port);
	NetworkFildes.Open(host);
	if (NetworkFildes.IsValid() == false) {
		fprintf(stderr, "NETWORK: No free port %d available, aborting\n", port);
		NetExit(); // machine dependent network exit
		return;
	}
#ifdef DEBUG
	const std::string hostStr = host.toString();
	DebugPrint("My host:port %s\n" _C_ hostStr.c_str());
#endif

#if 0 // FIXME: need a working interface check
	unsigned long ips[10];
	int networkNumInterfaces = NetSocketAddr(NetworkFildes, ips, 10);
	if (networkNumInterfaces) {
		DebugPrint("Num IP: %d\n" _C_ networkNumInterfaces);
		for (int i = 0; i < networkNumInterfaces; ++i) {
			DebugPrint("IP: %d.%d.%d.%d\n" _C_ NIPQUAD(ntohl(ips[i])));
		}
	} else {
		fprintf(stderr, "NETWORK: Not connected to any external IPV4-network, aborting\n");
		ExitNetwork1();
		return;
	}
#endif
}
コード例 #6
0
int main(int argc, char *argv[])
{
    int IDtoAcknowledge = -1;
    int receivedID = -1;
    /*
      LOCALSOCKET setup
     */

    CUDPSocket localSocket;
    localSocket.Initialise();
    localSocket.MakeNonBlocking();
    //The socket must be bound so that we know the port number
    localSocket.Bind(LOCALPORT);
    //Define where we are sending the data
    localSocket.SetDestinationAddress("127.0.0.1", REMOTEPORT);

    printf("Sending data FROM: [%u] TO: [%u]\n\n", LOCALPORT, REMOTEPORT);

    // Clear the buffer memory
    byte localBuffer[sizeof(MyPacket_t)];
    memset(localBuffer, NULL, sizeof(MyPacket_t));

    byte receivingBuffer[sizeof(MyPacket_t)];
    memset(receivingBuffer, NULL, sizeof(MyPacket_t));

    // Initialise the packet
    MyPacket_t sendPacket;
    sendPacket.Acceleration = 1.0f;
    MyPacket_t receivePacket;

    /* infinite loop */
    while(true)
    {
        if(IDtoAcknowledge == receivedID)
        {
            // Copy the data into the buffer and send it
            memcpy(localBuffer, &sendPacket, sizeof(MyPacket_t));
            int sendResult = localSocket.Send(localBuffer);
            // data has been sent
            if(sendResult > 0)
            {
                printf("Sent to IP: [%s] PORT: [%u]\n",
                       inet_ntoa(localSocket.GetDestinationAddress().sin_addr),
                       ntohs(localSocket.GetDestinationAddress().sin_port));
                sendPacket.Inspect();

                IDtoAcknowledge = sendPacket.ID;
                receivedID = -1;

                sendPacket.ID++;
                sendPacket.Velocity += sendPacket.Acceleration;
            }
        }

        /* receive packet in to the buffer */
        int receiveResult = localSocket.Receive(receivingBuffer);
        // if n>0 we have got some data
        if(receiveResult > 0)
        {
            memcpy(&receivePacket, receivingBuffer, sizeof(MyPacket_t));
            printf("Received an acknowldment for ID %d\n", receivePacket.ID);
            receivedID = receivePacket.ID;
        }

        usleep(1000000);
    }

    return 0;
}