Page 1 of 2 12 LastLast
Results 1 to 10 of 13

Thread: Howto write a server that runs on linux with freepascal

  1. #1

    Howto write a server that runs on linux with freepascal

    FreePascal
    Linux
    Server

    Who knows a bare bones example on how to write a server that runs on linux with freepascal?
    http://3das.noeska.com - create adventure games without programming

  2. #2

    Howto write a server that runs on linux with freepascal

    There is an example that ships with freepascal called daemon.pp, it gives you the basics.
    <A HREF="http://www.myhpf.co.uk/banner.asp?friend=139328">
    <br /><IMG SRC="http://www.myhpf.co.uk/banners/60x468.gif" BORDER="0">
    <br /></A>

  3. #3

    Howto write a server that runs on linux with freepascal

    It is not in my examples folder. Where can i download daemon.pp ?
    http://3das.noeska.com - create adventure games without programming

  4. #4

    Howto write a server that runs on linux with freepascal

    It's in the source packet
    Peregrinus, expectavi pedes meos in cymbalis
    Nullus norvegicorum sole urinat

  5. #5

    Howto write a server that runs on linux with freepascal

    hi !
    don't know if it's efficient, but look at this

    [pascal]
    unit Daemon;

    {************************************************* ************************************************}
    { }
    { Borland Community Chatbot }
    { Copyright (c) 2002 Dave Nottage }
    { }
    { This program is free software; you can redistribute it and/or modify it under the terms of the }
    { GNU General Public License as published by the Free Software Foundation; either version 2 of }
    { the License, or (at your option) any later version. }
    { }
    { This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; }
    { without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. }
    { See the GNU General Public License for more details. }
    { }
    { You should have received a copy of the GNU General Public License along with this program; }
    { if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, }
    { MA 02111-1307 USA }
    { }
    { Original author: Dave Nottage }
    { Email: davidn@smartchat.net.au }
    { Snail mail: Borland Community Chatbot, 1 Victoria St, MILE END, SA, 5031, Australia }
    { }
    {************************************************* ************************************************}

    {************************************************* *********}
    { Borland Chat Daemonizer }
    {************************************************* *********}

    interface

    type
    TDaemonApplication = class(TObject)
    private
    FTerminated: Boolean;
    FPID: pid_t;
    FStdOutput: string;
    FErrOutput: string;
    FDetach: Boolean;
    procedure InstallSignalHandlers;
    procedure PerformDaemonMagic;
    protected
    function Daemonize: Boolean; virtual;
    procedure DoSigHup; virtual;
    procedure DoSigTerm; virtual;
    procedure DoSigQuit; virtual;
    public
    constructor Create;
    procedure Run;
    property Detach: Boolean read FDetach write FDetach; // determines whether to fork or not.
    property ErrorOutput: string read FErrOutput write FErrOutput; // change to setter method so it is dynamic
    property PID: pid_t read FPID;
    property StandardOutput: string read FStdOutput write FStdOutput;
    property Terminated: boolean read FTerminated;
    end;

    var
    Application: TDaemonApplication = nil;

    implementation

    uses
    Libc;

    { handle SIGHUP & SIGTERM }
    procedure DoSig(sig: longint); cdecl;
    begin
    case sig of
    SIGHUP:
    // Dispatch SIGHUP
    Application.DoSigHup;
    SIGTERM:
    // Dispatch SIGTERM
    Application.DoSigTerm;
    SIGQUIT:
    // Dispatch SIGQUIT
    Application.DoSigQuit;
    end;
    end;

    constructor TDaemonApplication.Create;
    begin
    inherited Create;
    FStdOutput := '/dev/null'; // change to a constant (non-localised)
    FErrOutput := '/dev/null';
    FTerminated := False;
    FDetach := True;
    end;

    procedure TDaemonApplication.PerformDaemonMagic;
    begin
    setsid()
    // http://www.erlenstar.demon.co.uk/unix/faq_2.html#SEC16
    Close(Input);
    AssignFile(Output, FStdOutput);
    Rewrite(Output);
    AssignFile(ErrOutput, FErrOutput);
    Rewrite(ErrOutput);
    end;

    function TDaemonApplication.Daemonize: Boolean;
    begin
    { daemonize }
    if FDetach then
    begin
    FPID := fork();
    case FPID of
    0:
    begin
    PerformDaemonMagic;
    __chdir('/');
    end;
    -1:
    // Forking error
    else
    // Halt;
    end;
    end
    else
    Result := True;
    end;

    procedure TDaemonApplication.InstallSignalHandlers;
    var
    SignalAction: TSigAction;
    SignalSet: TSigSet;
    begin
    { block all signals except -HUP & -TERM }

    sigfillset(SignalSet);

    sigdelset(SignalSet, SIGHUP);
    sigdelset(SignalSet, SIGTERM);

    pthread_sigmask(SIG_BLOCK, @SignalSet, nil);

    { setup the signal handlers }
    FillChar(SignalAction, SizeOf(SignalAction), 0);
    SignalAction.__sigaction_handler := @DoSig;
    sigaction(SIGTERM, @SignalAction, nil);

    FillChar(SignalAction, SizeOf(SignalAction), 0);
    SignalAction.__sigaction_handler := @DoSig;
    sigaction(SIGHUP, @SignalAction, nil);

    end;

    procedure TDaemonApplication.Run;
    begin
    if Daemonize then
    begin
    InstallSignalHandlers;
    while not FTerminated do
    begin
    // Here we will allow whatever hooks into the daemon application, to
    // do their thing.
    // RunDataModules;
    end;
    end;
    end;

    procedure TDaemonApplication.DoSigTerm;
    begin
    FTerminated := True;
    end;

    procedure TDaemonApplication.DoSigHup;
    begin

    end;

    procedure TDaemonApplication.DoSigQuit;
    begin

    end;

    initialization
    Application := TDaemonApplication.Create;

    finalization
    Application.Free;

    end.
    [/pascal]

  6. #6

    Re: Howto write a server that runs on linux with freepascal

    Sorry for digging up this old post of mine, but i am still strugling with daemon.pp and how it is supposed to work. It should also work under windows? But compiling it with fpc under windows and linux it is not giving me an working example. It think it should write some info to an log file? But where is that log file?
    http://3das.noeska.com - create adventure games without programming

  7. #7

    Re: Howto write a server that runs on linux with freepascal

    Hi noeska...

    If you're using Lazarus theres a package you can install called lazdaemon. You'll find it in the available packages list in the "Configure packages" dialog. This'll give you a nice template that you can use when creating a new project.

  8. #8

    Re: Howto write a server that runs on linux with freepascal

    Synapse works with Linux too. I made wrap up class to Next3D game engine, at least michalis said it compiled under Linux.
    http://www.pascalgamedevelopment.com...p?topic=6218.0
    There is demo too of TCP and UDP server, sending and receiving different packets.

    I know its possible to directly use Synapse but i'm not so keen on doing the complicated multi-threading stuff again every time for network projects

    It doesn't rely on any visual component so it should work even without Lazarus.

  9. #9

    Re: Howto write a server that runs on linux with freepascal

    I dont use lazarus. I only use fpc.

    As for the real webserver part i use indy. That part is already working but when starting the server then it blocks the console.

    So i thought i need a daemon so i can start the server in the background. Any other alternatives or good documentation on the fpc daemon.pp example?

    (btw this is a port from delphi (win32) to fpc (linux). Al that fails that i can start the server in the background so the console remains useable.
    http://3das.noeska.com - create adventure games without programming

  10. #10

    Re: Howto write a server that runs on linux with freepascal

    Noeska, your looking to Fork your execution, not Daemon execution. Daemon implies running as part of the Kernel or Kernel Group, while Forking simply means to execute in parallel with the current operations.

    Look at launching a forked process (I have code some place if I can ever find it LOL), but until then read http://www.freepascal.org/docs-html/...inux/fork.html

    - Jeremy

    PS: Everyone is correct that Synapse has better cross platform than Indy. Though, if my reading has not deceived me, that may change soon. Also look at switching from Threading to Fiber execution as this will lower your overhead in both Linux and Windows. It will also get you out of having to worry about thread runaway and loading.

Page 1 of 2 12 LastLast

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •