2010-06-08 10 views
7

Adlandırılmış bir borular 101 sorun yaşıyorum. C++ yönetilmeyen bir uygulamadan C# yönetilen bir uygulamaya ileten simpleks adında bir pipoyu bağlamak için çok basit bir kurulumum var. Boru bağlanır, ancak arabelleği yıkayıp mesajı ileten sapı kapatmazsam, borudan bir "mesaj" gönderemem. Mesaj engellenmiş gibi. İstemci/sunucunun rollerini tersine çevirmeyi ve herhangi bir şans olmaksızın farklı Bayrak kombinasyonlarıyla çağırmayı denedim. C# yönetilen C++ yönetilmeyen diğer yönlerde iletileri kolayca gönderebilirsiniz. Herhangi bir fikir sahibi olan var mı? Hiçbiriniz C++ tarafından yönetilmeyen C++ 'dan yönetilen mesajlar gönderebilir misiniz? İnterkam edilmiş veya yönetilmeyen boruların çok sayıda örneğini bulabilirim, ancak aralıksız olarak yönetilemez/değil - sadece bunu yapabileceğini iddia ediyor.Adlandırılmış bir boru dizesini umnanaged tarafından yönetilen kod alanına nasıl gönderirsiniz?

Listelerde, açıklık için sarıcı malzemelerin çoğunu ihmal ettim. İlgili olduğuna inandığım anahtar uçlar, boru bağlantısı/oluşturma/okuma ve yazma yöntemleridir. Burada engelleme/iş parçacığı hakkında çok fazla endişelenmeyin.

C# sunucu tarafı

// This runs in its own thread and so it is OK to block 
    private void ConnectToClient() 
    { 
     // This server will listen to the sending client 
     if (m_InPipeStream == null) 
     { 
      m_InPipeStream = 
       new NamedPipeServerStream("TestPipe", PipeDirection.In, 1); 
     } 

     // Wait for client to connect to our server 
     m_InPipeStream.WaitForConnection(); 

     // Verify client is running 
     if (!m_InPipeStream.IsConnected) 
     { 
      return; 
     } 

     // Start listening for messages on the client stream 
     if (m_InPipeStream != null && m_InPipeStream.CanRead) 
     { 
      ReadThread = new Thread(new ParameterizedThreadStart(Read)); 
      ReadThread.Start(m_InPipeStream); 
     } 
    } 


    // This runs in its own thread and so it is OK to block 
    private void Read(object serverObj) 
    { 
     NamedPipeServerStream pipeStream = (NamedPipeServerStream)serverObj; 
     using (StreamReader sr = new StreamReader(pipeStream)) 
     { 
      while (true) 
      { 
       string buffer = "" ; 
       try 
       { 
        // Blocks here until the handle is closed by the client-side!! 
        buffer = sr.ReadLine(); // <<<<<<<<<<<<<< Sticks here 
       } 
       catch 
       { 
        // Read error 
        break; 
       } 

       // Client has disconnected? 
       if (buffer == null || buffer.Length == 0) 
        break; 

       // Fire message received event if message is non-empty 
       if (MessageReceived != null && buffer != "") 
       { 
        MessageReceived(buffer); 
       } 
      } 
     } 
    } 

C++ istemci tarafında

// Static - running in its own thread. 
    DWORD CNamedPipe::ListenForServer(LPVOID arg) 
    { 
     // The calling app (this) is passed as the parameter 
     CNamedPipe* app = (CNamedPipe*)arg; 

     // Out-Pipe: connect as a client to a waiting server 
     app->m_hOutPipeHandle = 
     CreateFile("\\\\.\\pipe\\TestPipe", 
       GENERIC_WRITE, 
       0, 
       NULL, 
       OPEN_EXISTING, 
       FILE_ATTRIBUTE_NORMAL, 
       NULL); 
     // Could not create handle 
     if (app->m_hInPipeHandle == NULL || 
      app->m_hInPipeHandle == INVALID_HANDLE_VALUE) 
     { 
      return 1; 
     } 

     return 0; 
    } 


    // Sends a message to the server 
    BOOL CNamedPipe::SendMessage(CString message) 
    { 
    DWORD dwSent; 

     if (m_hOutPipeHandle == NULL || 
      m_hOutPipeHandle == INVALID_HANDLE_VALUE) 
     { 
      return FALSE; 
     } 
     else 
     { 
      BOOL bOK = 
       WriteFile(m_hOutPipeHandle, 
          message, message.GetLength()+1, &dwSent, NULL); 
      //FlushFileBuffers(m_hOutPipeHandle);    // <<<<<<< Tried this 
      return (!bOK || (message.GetLength()+1) != dwSent) ? FALSE : TRUE; 
     } 
    } 


    // Somewhere in the Windows C++/MFC code... 
    ... 
    // This write is non-blocking. It just passes through having loaded the pipe. 
    m_pNamedPipe->SendMessage("Hi de hi"); 
    ... 

cevap

8

sr.ReadLine(), yeni satır karakter (ler) hattının ucunu bilmek görmek için bekler. Ne yeni çizgi ne de akış sonu aldığından daha fazlasını bekler. Dene:

m_pNamedPipe->SendMessage("Hi de hi\n") 

veya sr.Read() yöntemlerden bazıları.

+0

İşte bu kadar! Her şey şimdi çalışıyor, çok teşekkürler. Yeni hat ihtiyacını tamamen unuttum, çok fazla boru mekaniği üzerine odaklandım (katman-1) Mesaj formatını unuttum (katman-2). – user320240

İlgili konular