| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185 |
- /*
- * This file is part of the Airplay SDK Code Samples.
- *
- * Copyright (C) 2001-2011 Ideaworks3D Ltd.
- * All Rights Reserved.
- *
- * This source code is intended only as a supplement to Ideaworks Labs
- * Development Tools and/or on-line documentation.
- *
- * THIS CODE AND INFORMATION ARE PROVIDED "AS IS" WITHOUT WARRANTY OF ANY
- * KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
- * IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
- * PARTICULAR PURPOSE.
- */
- /**
- * @page ExampleS3EHelloWorld S3E Hello World Example
- *
- * The following example, in typical Hello World style, displays the phrase
- * "Hello, World!" on screen.
- *
- * The functions required to achieve this are:
- * <ul>
- * <li>s3eDebugPrint()
- * </ul>
- *
- * All examples will follow this basic pattern; a brief description of what
- * the example does will be given followed by a list of all the important
- * functions and, perhaps, classes.
- *
- * Should the example be more complex, a more detailed explanation of what the
- * example does and how it does it will be added. Note that most examples
- * use an example framework to remove boilerplate code and allow the projects
- * to be made up of a single source file for easy viewing. This framework can
- * be found in the examples/s3e/ExamplesMain directory.
- *
- * @include s3eHelloWorld.cpp
- */
- #include "s3e.h"
- // ------- RAKNET INCLUDES ------------
- #include "RakPeerInterface.h"
- #include "PacketLogger.h"
- #include "Rand.h"
- #include "Kbhit.h"
- #include <stdio.h>
- #include "RakSleep.h"
- #include "MessageIdentifiers.h"
- #include "BitStream.h"
- #include "GetTime.h"
- using namespace RakNet;
- // main entry point for the application
- int main()
- {
- // ------- RAKNET CODE ------------
- RakPeerInterface *rakPeer1, *rakPeer2;
- rakPeer1=RakPeerInterface::GetInstance();
- rakPeer2=RakPeerInterface::GetInstance();
- rakPeer1->SetMaximumIncomingConnections(8);
- rakPeer2->SetMaximumIncomingConnections(8);
- bool gotConnectionRequestAccepted[2];
- bool gotNewIncomingConnection[2];
- Packet *packet;
- SocketDescriptor sd1(60000,0);
- SocketDescriptor sd2(2000,0);
- unsigned short numSystems[2];
- // Wait for a quit request from the host OS
- while (!s3eDeviceCheckQuitRequest())
- {
- if (s3eTimerGetMs() % 3000 < 1000)
- s3eSurfaceClear(255, 0, 0);
- else if (s3eTimerGetMs() % 3000 < 2000)
- s3eSurfaceClear(0, 255, 0);
- else
- s3eSurfaceClear(0, 0, 255);
- // ------- RAKNET CODE ------------
- gotConnectionRequestAccepted[0]=false;
- gotConnectionRequestAccepted[1]=false;
- gotNewIncomingConnection[0]=false;
- gotNewIncomingConnection[1]=false;
- numSystems[0]=0;
- numSystems[1]=0;
- rakPeer1->Startup(1,&sd1, 1);
- rakPeer2->Startup(1,&sd2, 1);
- RakSleep(100);
- rakPeer1->Connect("127.0.0.1", 2000, 0, 0);
- rakPeer2->Connect("127.0.0.1", 60000, 0, 0);
- RakSleep(100);
- for (packet=rakPeer1->Receive(); packet; rakPeer1->DeallocatePacket(packet), packet=rakPeer1->Receive())
- {
- if (packet->data[0]==ID_NEW_INCOMING_CONNECTION)
- gotNewIncomingConnection[0]=true;
- else if (packet->data[0]==ID_CONNECTION_REQUEST_ACCEPTED)
- gotConnectionRequestAccepted[0]=true;
- else if (packet->data[0]==ID_CONNECTION_ATTEMPT_FAILED)
- printf("Error on rakPeer1, got ID_CONNECTION_ATTEMPT_FAILED\n");
- }
- for (packet=rakPeer2->Receive(); packet; rakPeer2->DeallocatePacket(packet), packet=rakPeer2->Receive())
- {
- if (packet->data[0]==ID_NEW_INCOMING_CONNECTION)
- gotNewIncomingConnection[1]=true;
- else if (packet->data[0]==ID_CONNECTION_REQUEST_ACCEPTED)
- gotConnectionRequestAccepted[1]=true;
- else if (packet->data[0]==ID_CONNECTION_ATTEMPT_FAILED)
- printf("Error on rakPeer2, got ID_CONNECTION_ATTEMPT_FAILED\n");
- }
- rakPeer1->GetConnectionList(0,&numSystems[0]);
- rakPeer2->GetConnectionList(0,&numSystems[1]);
- if (gotConnectionRequestAccepted[0]==true && gotConnectionRequestAccepted[1]==true)
- {
- printf("Test passed\n");
- }
- else if (numSystems[0]!=1 || numSystems[1]!=1)
- {
- printf("Test failed, system 1 has %i connections and system 2 has %i connections.\n", numSystems[0], numSystems[1]);
- }
- else if (gotConnectionRequestAccepted[0]==false && gotConnectionRequestAccepted[1]==false)
- {
- printf("Test failed, ID_CONNECTION_REQUEST_ACCEPTED is false for both instances\n");
- }
- else if (gotNewIncomingConnection[0]==true && gotNewIncomingConnection[1]==true)
- {
- printf("Test failed, ID_NEW_INCOMING_CONNECTION is true for both instances\n");
- }
- else if (gotConnectionRequestAccepted[0]==false && gotConnectionRequestAccepted[1]==false)
- {
- printf("Test failed, ID_NEW_INCOMING_CONNECTION is false for both instances\n");
- }
- else if (gotConnectionRequestAccepted[0]==true && gotNewIncomingConnection[1]==false)
- {
- printf("Test failed, ID_CONNECTION_REQUEST_ACCEPTED for first instance, but not ID_NEW_INCOMING_CONNECTION for second\n");
- }
- else if (gotConnectionRequestAccepted[1]==true && gotNewIncomingConnection[0]==false)
- {
- printf("Test failed, ID_CONNECTION_REQUEST_ACCEPTED for second instance, but not ID_NEW_INCOMING_CONNECTION for first\n");
- }
- else if ((int)gotConnectionRequestAccepted[0]+
- (int)gotConnectionRequestAccepted[1]!=1)
- {
- printf("Test failed, does not have exactly one instance of ID_CONNECTION_REQUEST_ACCEPTED\n");
- }
- else if ((int)gotNewIncomingConnection[0]+
- (int)gotNewIncomingConnection[1]!=1)
- {
- printf("Test failed, does not have exactly one instance of ID_NEW_INCOMING_CONNECTION\n");
- }
- else if ((int)gotConnectionRequestAccepted[0]+
- (int)gotConnectionRequestAccepted[1]+
- (int)gotNewIncomingConnection[0]+
- (int)gotNewIncomingConnection[1]!=2)
- {
- printf("Test failed, does not have exactly one instance of ID_CONNECTION_REQUEST_ACCEPTED and one instance of ID_NEW_INCOMING_CONNECTION\n");
- }
- else
- printf("Test passed\n");
- rakPeer1->Shutdown(0);
- rakPeer2->Shutdown(0);
- // flip the surface buffer
- s3eSurfaceShow();
- // sleep for 0ms to allow the OS to process events etc
- s3eDeviceYield(0);
- //Update keyboard and touchscreen event queues
- s3eKeyboardUpdate();
- s3ePointerUpdate();
- //Quit if there was any activity since the last time the s3e*Update() functions
- //were called
- if (s3eKeyboardAnyKey())
- break;
- if (s3ePointerGetState(S3E_POINTER_BUTTON_SELECT) & S3E_POINTER_STATE_PRESSED)
- break;
- }
- return 0;
- }
|