Пример #1
0
int main( int argc, char const **argv )
{
   NetworkSystem net;

   if (!net.init()) {
      return 1;
   }

   // Do rest of program here
   char const *my_host_name = AllocLocalHostName();
   printf( "Host name: %s\n", my_host_name );

   PrintAddressesForHost( my_host_name, "1234" );

   if (argc > 2) {
      char const *host_name = argv[1];
      char const *msg = argv[2];
      // Start a client
      StartClient( host_name, "1234", msg );
   } else {
      StartHost( my_host_name, "1234" );
   }

   net.deinit();

   printf( "Press any key to continue..." );
   _getch();
   return 0;
}
Пример #2
0
//-------------------------------------------------------------------------------------------------------
int main( int argc, char const **argv )
{
   if (!NetSystemInit()) {
      printf( "Failed to initialize net system.\n" );
      _getch();
      return false;
   }

   // List Addresses
   char const *hostname = AllocLocalHostName();
   ListAddressesForHost( hostname, gHostPort );
   FreeLocalHostName(hostname);

   // Host/Client Logic
   if ((argc <= 1) || (_strcmpi( argv[1], "sock" ) == 0)) {
      printf( "Hosting...\n" );
      NetworkHost( gHostPort ); 
   } else if (argc > 2) {
      char const *addr = argv[1];
      char const *msg = argv[2];
      printf( "Sending message \"%s\" to [%s]\n", msg, addr );
      NetworkClient( addr, gHostPort, msg );
   } else {
      char const *msg = argv[1];
      printf( "Broadcast message \"%s\".\n", msg );
      NetworkBroadcast( msg );
   }

   NetSystemDeinit();

   printf( "Press any key to continue...\n" );
   _getch();
   return 0;
}
std::string GetLocalHostName() {
	std::string hostName = AllocLocalHostName();
	//set default host name
	if (hostName == "") {
		hostName = "localhost";
	}
	return hostName;
}
Пример #4
0
void NetJoinRequestCallback(NetSender* sender, NetMessage& msg) {
	//do nothing
	ConsolePrintString("\nNetJoinRequestCallback => ");

	BinaryBufferParser bp(&msg.messageBuffer[0], msg.curSize);
	std::string otherName = bp.ReadNextString();
	ConsolePrintString(otherName);

	if (NetworkSystem::s_gameSession->CanAddNewConn()) {
		Byte nextConnIndex = NetworkSystem::s_gameSession->GetNextJoinableConnIndex();
		NetworkSystem::s_gameSession->AddConnection(*sender->addr, 0xff, "client_"+ShortToString(nextConnIndex)+"(O.o)((_ | __ | _");

		//sender->conn = NetworkSystem::s_gameSession->FindConnectionInMap(nextConnIndex);
		NetConnection* newClientConnPtr = NetworkSystem::s_gameSession->FindConnectionInMap(0xff);
		if (newClientConnPtr) {
			NetMessage* joinAcceptMsg = new NetMessage(EN_MSG_ID_JOIN_ACCEPT);
			Byte myMaxConns = NetworkSystem::s_gameSession->GetMaxConnections();
			joinAcceptMsg->WRITE_BYTES(myMaxConns);
			joinAcceptMsg->WRITE_BYTES(nextConnIndex);

			if (NetworkSystem::s_gameSession->m_connSelf) {

				Byte myConnIndex = NetworkSystem::s_gameSession->m_connSelf->GetConnIndex();

				joinAcceptMsg->WRITE_BYTES(myConnIndex);
				//joinAcceptMsg->WriteMessageData(&myConnIndex, SIZE_OF(myConnIndex));

				//joinAcceptMsg->WriteMessageData(&std::string(AllocLocalHostName() + "__(O.o)((_|__|_\0"), SIZE_OF(std::string));
				std::string hostNameString = AllocLocalHostName();
				joinAcceptMsg->WRITE_BYTES(hostNameString);

			}//end of if my info exists

			//doing sender->conn causes system to spam new conns

			newClientConnPtr->SendNetMessage(*joinAcceptMsg);

			//newClientConnPtr->SetConnIndex(0xff);

			newClientConnPtr->SetName("client_" + ShortToString(nextConnIndex) + "(O.o)((_ | __ | _");
		}
		
	}
	else {
		NetMessage* joinDenyMsg = new NetMessage(EN_MSG_ID_JOIN_DENY);

		NetworkSystem::s_gameSession->SendNetMessage(*joinDenyMsg);
	}
	

}
Пример #5
0
//-------------------------------------------------------------------------------------------------------
static void NetworkHost( char const *port )
{
   char const *host_name = AllocLocalHostName();
   SOCKET sock = BindAddress( host_name, port, AF_INET, SOCK_DGRAM );
   FreeLocalHostName(host_name);

   if (sock == INVALID_SOCKET) {
      printf( "Failed to create listen socket.\n" );
      return;
   }

   // Don't need to listen for DGRAM sockets

   /*
   // For setting blocking status
   u_long non_blocking = 1;
   ioctlsocket( sock, FIONBIO, &non_blocking )
   */

    printf( "Waiting for messages...\n" );

    sockaddr_storage their_addr;
    char buffer[2048];

    for (;;) {
      int addr_size = sizeof(their_addr);
      int recvd = recvfrom( sock, buffer, 2048, 0, (sockaddr*)&their_addr, &addr_size );

      char from_name[128];
      GetAddressName( from_name, 128, (sockaddr*)&their_addr );

      if (recvd > 0) {
         buffer[recvd] = NULL;
         printf( "Received Message[%s] from %s\n", buffer, from_name );
      } else {
         int error = WSAGetLastError();
         printf( "recvfrom error: %i, %i\n", recvd, error );
      }
    }

    closesocket(sock);
}
Пример #6
0
//-------------------------------------------------------------------------------------------------------
static void NetworkClient( char const *target, char const *port, char const *msg )
{
   char const *host_name = AllocLocalHostName();
   SOCKET sock = BindAddress(host_name, gClientPort, AF_INET, SOCK_DGRAM);
   FreeLocalHostName(host_name);

   if (sock == INVALID_SOCKET) {
      printf( "Could not bind adddress.\n" );
      return;
   }
   
   SpamHelper helper;
   helper.sock = sock;
   helper.msg = msg;

   addrinfo *spam = AllocAddressesForHost( target, port, AF_UNSPEC, SOCK_DGRAM, false );
   ForEachAddress( spam, SpamMessage, &helper ); 
   FreeAddresses( spam );
   
   closesocket( sock );
}
Пример #7
0
std::string NetworkSession::GetAddressStringForHostName(int addrFamily){
	addrinfo hints;
	addrinfo* addr;

	std::string host_name = AllocLocalHostName();

	std::string service = IntToString((int)m_port);

	memset(&hints, 0, sizeof(hints));

	hints.ai_family = addrFamily; //get all addresses AF_INET or AF_INET6
	hints.ai_socktype = SOCK_STREAM; // stream based, determines transport layer TCP
	hints.ai_flags = AI_PASSIVE; // used for binding/listening

	int status = getaddrinfo(host_name.c_str(), service.c_str(), &hints, &addr);
	if (status != 0) {
		ConsolePrintf("Failed to create socket address! %s\n", (char*)gai_strerror(status));
	}

	std::string addressString = "\n===" + host_name + "===\n";

	addrinfo *iter;
	char addr_name[INET6_ADDRSTRLEN];

	for (iter = addr; iter != nullptr; iter = iter->ai_next) {
 		
 		PCSTR addressPCStr = inet_ntop(iter->ai_family, GetInAddress(iter->ai_addr), addr_name, INET6_ADDRSTRLEN);
		char* addressCStr = (char*)addressPCStr;

		addressString += "IP: ";
		addressString += addressCStr;
		addressString += " | PORT: " + service;
		addressString += "\n";
	}

	return addressString;
}
Пример #8
0
NetConnection* NetworkSession::AddConnectionToSelf(const Byte& connIndex, const std::string& connPortService ) {

	static const std::string selfConnName = "self";

	NetAddresses netAddressesToConnect;
	std::string localHostName = AllocLocalHostName();
	AllocAddressesForHost(netAddressesToConnect, localHostName, connPortService );

	std::string addConnDebugToConsole = "Adding Connection to Self";

	AddConnection(netAddressesToConnect[0], connIndex, selfConnName);

	m_netConnNameRegistry[connIndex] = "self";

	addConnDebugToConsole += "\nAdded Connection to Self:" + netAddressesToConnect[0].ToString();

	OUTPUT_STRING_TO_CONSOLE(addConnDebugToConsole, 1000);
	ConsolePrintString(addConnDebugToConsole);

	
	return  m_netConnectionMap[selfConnName];


}