Recent

Author Topic: Platform independent named pipes  (Read 1724 times)

ChristianH

  • New Member
  • *
  • Posts: 46
Platform independent named pipes
« on: April 05, 2020, 12:37:35 pm »
Hi,

is there a platform independent solution for named pipes? The background is that I want to send content to libvlc using pipes. It does work under windows but I have no idea how to create a named pipe under linux.

Cheers
Christian

Cyrax

  • Hero Member
  • *****
  • Posts: 836
Re: Platform independent named pipes
« Reply #1 on: April 05, 2020, 12:43:23 pm »

ChristianH

  • New Member
  • *
  • Posts: 46
Re: Platform independent named pipes
« Reply #2 on: April 05, 2020, 02:11:49 pm »
Thanks for the quick response, I will try it under linux, once I got my vm working.

Maybe someone might find this useful, at least for windows:

Code: [Select]
unit UPipes;

interface

uses
  Windows, Classes, SysUtils;

type
  TPipeServer = class
  private
    FPipeHandle: THandle;
    FPipeName: String;
    FOverlapped: TOverlapped;
  public
    constructor Create(const APipename: string);
    procedure Write(const ABuffer: PByte; const ASize: integer);
    destructor Destroy; override;
  end;

implementation

constructor TPipeServer.Create(const APipename: string);
var
  sa: Security_Attributes;
  sd: Security_Descriptor;
begin
  FPipeName := '\\.\pipe\' + APipeName;

  InitializeSecurityDescriptor(@sd,
    SECURITY_DESCRIPTOR_REVISION);
  SetSecurityDescriptorDacl(@sd, True, nil, False);

  sa.nLength := SizeOf(sa);
  sa.lpSecurityDescriptor := @sd;
  sa.bInheritHandle := False;

  FPipeHandle := CreateNamedPipe(PChar(FPipeName),
    PIPE_ACCESS_OUTBOUND or FILE_FLAG_OVERLAPPED, PIPE_TYPE_BYTE,
    1, 188 * 1024, 188 * 1024, 100, @sa);
  fillchar(FOverlapped, sizeof(FOverlapped),0);
end;

destructor TPipeServer.Destroy;
begin
  FlushFileBuffers(FPipeHandle);
  DisconnectNamedPipe(FPipeHandle);

  if FPipeHandle < INVALID_HANDLE_VALUE then
    CloseHandle(FPipeHandle);
  inherited;
end;

procedure TPipeServer.Write(const ABuffer: PByte; const ASize: integer);
var
  BytesWritten: cardinal;
  Dw: Cardinal;
begin
   if not ConnectNamedPipe(FPipeHandle, @FOverlapped) then
    begin
      Dw := GetLastError;
      if Dw <> ERROR_PIPE_CONNECTED then
      begin
        if Dw = ERROR_NO_DATA then
          DisconnectNamedPipe(FPipeHandle);
       exit;
      end;
    end;

  BytesWritten := 0;
  WriteFile(FPipeHandle, ABuffer^, ASize, BytesWritten, @FOverlapped);
end;


end.

Christian

Thaddy

  • Hero Member
  • *****
  • Posts: 14201
  • Probably until I exterminate Putin.
Re: Platform independent named pipes
« Reply #3 on: April 05, 2020, 03:18:22 pm »
You like hexagonal shaped wheels?
It works, but the horse won't like it.

Named pipes are provided as standard and cross-platform.
« Last Edit: April 05, 2020, 03:21:41 pm by Thaddy »
Specialize a type, not a var.

ChristianH

  • New Member
  • *
  • Posts: 46
Re: Platform independent named pipes
« Reply #4 on: April 05, 2020, 08:50:41 pm »
You like hexagonal shaped wheels?
It works, but the horse won't like it.

Named pipes are provided as standard and cross-platform.

You speak in miracles, to be honest I have no idea what you mean. I'm open for any suggestion. The reason for using pipes in this certain case is that I want to provide ffmpeg and libvlc my generated mpeg2-ts stream without providing this stream via rtp, tcp or any other kind of protocol.

Christian

winni

  • Hero Member
  • *****
  • Posts: 3197
Re: Platform independent named pipes
« Reply #5 on: April 05, 2020, 09:32:52 pm »
Hi!

Dont let you confuse by Thaddys jokes.

Use named pipes - they are cross platform.
And should work on Win, Lin & Mac.

Winni

jwdietrich

  • Hero Member
  • *****
  • Posts: 1232
    • formatio reticularis
Re: Platform independent named pipes
« Reply #6 on: April 05, 2020, 09:47:42 pm »
Hi!

Dont let you confuse by Thaddys jokes.

Use named pipes - they are cross platform.
And should work on Win, Lin & Mac.

Winni

Given that it works on Windows it is puzzling that it is defined in the BaseUnix unit.
function GetRandomNumber: integer; // xkcd.com
begin
  GetRandomNumber := 4; // chosen by fair dice roll. Guaranteed to be random.
end;

http://www.formatio-reticularis.de

Lazarus 2.2.6 | FPC 3.2.2 | PPC, Intel, ARM | macOS, Windows, Linux

 

TinyPortal © 2005-2018