Ejemplo n.º 1
0
    void Run() {
        do {
            // wait for a connection
            NPT_Socket* client = NULL;
            NPT_LOG_FINE_1("waiting for connection on port %d...", m_Port);
            NPT_Result result = m_Socket.WaitForNewClient(client, NPT_TIMEOUT_INFINITE);
            if (NPT_FAILED(result) || client == NULL) return;
                    
            NPT_SocketInfo client_info;
            client->GetInfo(client_info);
            NPT_LOG_FINE_2("client connected (%s -> %s)",
                client_info.local_address.ToString().GetChars(),
                client_info.remote_address.ToString().GetChars());

            // get the output stream
            NPT_OutputStreamReference output;
            client->GetOutputStream(output);

            // generate policy based on our current IP
            NPT_String policy = "<cross-domain-policy>";
            policy += "<allow-access-from domain=\""+client_info.local_address.GetIpAddress().ToString()+"\" to-ports=\""+m_AuthorizedPorts+"\"/>";
            policy += "<allow-access-from domain=\""+client_info.remote_address.GetIpAddress().ToString()+"\" to-ports=\""+m_AuthorizedPorts+"\"/>";
            policy += "</cross-domain-policy>";

            NPT_MemoryStream* mem_input = new NPT_MemoryStream();
            mem_input->Write(policy.GetChars(), policy.GetLength());
            NPT_InputStreamReference input(mem_input);
            
            NPT_StreamToStreamCopy(*input, *output);
            
            
            delete client;
        } while (!m_Aborted);
    }
Ejemplo n.º 2
0
/*----------------------------------------------------------------------
|       TcpServerLoop
+---------------------------------------------------------------------*/
static void
TcpServerLoop(int port)
{
    NPT_TcpServerSocket listener;

    NPT_Result result = listener.Bind(NPT_SocketAddress(NPT_IpAddress::Any, port)); 
    if (NPT_FAILED(result)) {
        NPT_Debug("ERROR: Bind() failed (%d)\n", result);
    }
		
    NPT_Socket* client;

    for (;;) {
        NPT_Debug("waiting for client on port %d\n", port);
        NPT_Result result = listener.WaitForNewClient(client);
        NPT_SocketInfo socket_info;
        client->GetInfo(socket_info);
        NPT_Debug("client connected from %s:%d\n",
            socket_info.remote_address.GetIpAddress().ToString().GetChars(),
            socket_info.remote_address.GetPort());
        NPT_InputStreamReference input;
        client->GetInputStream(input);
        NPT_OutputStreamReference output;
        client->GetOutputStream(output);
        do {
            char buffer[1024];
            NPT_Size bytes_read;
            result = input->Read(buffer, sizeof(buffer), &bytes_read);
            if (NPT_SUCCEEDED(result)) {
                NPT_Debug("read %ld bytes\n", bytes_read);
                output->Write(buffer, bytes_read);
            }
        } while (NPT_SUCCEEDED(result));
        delete client;
    }
}