summaryrefslogtreecommitdiff
path: root/packages/fcl-net/src/fpsock.pp
blob: 00e03f988ac139dfdf57d81b90016fb7f27caebf (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
{

    Socket communication components
    Copyright (c) 2003 by
      Areca Systems GmbH / Sebastian Guenther, sg@freepascal.org

    See the file COPYING.FPC, included in this distribution,
    for details about the copyright.

    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.
}

{$mode objfpc}
{$H+}

unit fpSock;

interface

uses Errors, SysUtils, Sockets, Classes, fpAsync, Resolve;

type

  ESocketError = class(Exception)
  end;

  TSocketComponent = class(TComponent)
  private
    FEventLoop: TEventLoop;
  public
    property EventLoop: TEventLoop read FEventLoop write FEventLoop;
  end;

  TSocketStream = class(THandleStream)
  private
    FOnDisconnect: TNotifyEvent;
    function GetLocalAddress: TSockAddr;
    function GetPeerAddress: TSockAddr;
  protected
    procedure Disconnected; virtual;
  public
    destructor Destroy; override;
    function Read(var Buffer; Count: LongInt): LongInt; override;
    function Write(const Buffer; Count: LongInt): LongInt; override;

    property LocalAddress: TSockAddr read GetLocalAddress;
    property PeerAddress: TSockAddr read GetPeerAddress;
    property OnDisconnect: TNotifyEvent read FOnDisconnect write FOnDisconnect;
  end;


  // Connection-based sockets

  TConnectionBasedSocket = class(TSocketComponent)
  protected
    FStream: TSocketStream;
    FActive: Boolean;
    procedure SetActive(Value: Boolean); virtual; abstract;
    property Active: Boolean read FActive write SetActive;
    property Stream: TSocketStream read FStream;
  public
    destructor Destroy; override;
  end;


  TConnectionState = (
    connDisconnected,
    connResolving,
    connConnecting,
    connConnected);

  TClientConnectionSocket = class;

  TConnectionStateChangeEvent = procedure(Sender: TClientConnectionSocket;
    OldState, NewState: TConnectionState) of object;

  TClientConnectionSocket = class(TConnectionBasedSocket)
  private
    FOnStateChange: TConnectionStateChangeEvent;
    FRetries: Integer;
    FRetryDelay: Integer;       // Delay between retries in ms
    RetryCounter: Integer;
    RetryTimerNotifyHandle: Pointer;
    CanWriteNotifyHandle: Pointer;
    procedure RetryTimerNotify(Sender: TObject);
    procedure SocketCanWrite(Sender: TObject);
  protected
    FConnectionState: TConnectionState;

    procedure CreateSocket; virtual; abstract;
    procedure DoResolve; virtual;
    procedure DoConnect; virtual;
    function GetPeerName: String; virtual; abstract;

    procedure SetActive(Value: Boolean); override;
    procedure SetConnectionState(NewState: TConnectionState);

    property ConnectionState: TConnectionState read FConnectionState;
    property Retries: Integer read FRetries write FRetries default 0;
    property RetryDelay: Integer read FRetryDelay write FRetryDelay default 500;
    property OnConnectionStateChange: TConnectionStateChangeEvent
      read FOnStateChange write FOnStateChange;
  public
    constructor Create(AOwner: TComponent); override;
    destructor Destroy; override;
  end;


  TQueryConnectEvent = procedure(Sender: TConnectionBasedSocket; Socket: Integer;
    var DoConnect: Boolean) of object;
  TConnectEvent = procedure(Sender: TConnectionBasedSocket;
    Stream: TSocketStream) of object;

  TSocketConnectionServer = class(TConnectionBasedSocket)
  private
    FOnQueryConnect: TQueryConnectEvent;
    FOnConnect: TConnectEvent;
  protected
    DataAvailableNotifyHandle: Pointer;
    procedure ListenerDataAvailable(Sender: TObject);
    function DoQueryConnect(ASocket: Integer): Boolean;
    procedure DoConnect(AStream: TSocketStream); virtual;
    property OnQueryConnect: TQueryConnectEvent read FOnQueryConnect
      write FOnQueryConnect;
    property OnConnect: TConnectEvent read FOnConnect write FOnConnect;
  end;


  // TCP/IP components

  TCustomTCPClient = class(TClientConnectionSocket)
  private
    FHost: String;
    FPort: Word;
    HostAddr: THostAddr;
    procedure SetHost(const Value: String);
    procedure SetPort(Value: Word);
  protected
    procedure CreateSocket; override;
    procedure DoResolve; override;
    procedure DoConnect; override;
    function GetPeerName: String; override;

    property Host: String read FHost write SetHost;
    property Port: Word read FPort write SetPort;
  public
    destructor Destroy; override;
  end;

  TTCPClient = class(TCustomTCPClient)
  public
    property ConnectionState;
    property Stream;
  published
    property Active;
    property Host;
    property Port;
    property Retries;
    property RetryDelay;
    property OnConnectionStateChange;
  end;

  TCustomTCPServer = class;

  TCustomTCPServer = class(TSocketConnectionServer)
  private
    FPort: Word;
    procedure SetActive(Value: Boolean); override;
  protected
    //!!!: Interface/bindings list?
    property Port: Word read FPort write FPort;
  public
    destructor Destroy; override;
  end;

  TTCPServer = class(TCustomTCPServer)
  public
    property Stream;
  published
    property Active;
    property Port;
    property OnQueryConnect;
    property OnConnect;
  end;


implementation

uses
  baseunix,Unix;

resourcestring
  SSocketNoEventLoopAssigned = 'No event loop assigned';
  SSocketCreationError = 'Could not create socket: %s';
  SHostNotFound = 'Host "%s" not found';
  SSocketConnectFailed = 'Could not connect to %s: %s';
  SSocketBindingError = 'Could not bind socket to port %d: %s';
  SSocketAcceptError = 'Connection accept failed: %s';
  SSocketIsActive = 'Cannot change parameters while active';

Const
  Sys_EAGAIN = ESYSEAGAIN;
  Sys_EINPROGRESS = ESYSEINPROGRESS;


// TSocketStream

destructor TSocketStream.Destroy;
begin
  CloseSocket(Handle);
  inherited Destroy;
end;

function TSocketStream.Read(var Buffer; Count: LongInt): LongInt;
begin
  Result := fprecv(Handle, @Buffer, Count, MSG_NOSIGNAL);
  if Result = -1 then
  begin
    Result := 0;
    if SocketError <> Sys_EAGAIN then
      Disconnected;
  end;
end;

function TSocketStream.Write(const Buffer; Count: LongInt): LongInt;
begin
  Result := FPsend(Handle, @Buffer, Count, MSG_NOSIGNAL);
  if Result = -1 then
  begin
    Result := 0;
    if SocketError <> Sys_EAGAIN then
      Disconnected;
  end;
end;

procedure TSocketStream.Disconnected;
begin
  if Assigned(OnDisconnect) then
    OnDisconnect(Self);
end;

function TSocketStream.GetLocalAddress: TSockAddr;
var
  len: LongInt;
begin
  len := SizeOf(TSockAddr);
  if fpGetSockName(Handle, @Result, @len) <> 0 then
    FillChar(Result, SizeOf(Result), 0);
end;

function TSocketStream.GetPeerAddress: TSockAddr;
var
  len: LongInt;
begin
  len := SizeOf(TSockAddr);
  if FpGetPeerName(Handle, @Result, @len) <> 0 then
    FillChar(Result, SizeOf(Result), 0);
end;


// TConnectionBasedSocket

destructor TConnectionBasedSocket.Destroy;
begin
  FreeAndNil(FStream);
  inherited Destroy;
end;


// TClientConnectionSocket

constructor TClientConnectionSocket.Create(AOwner: TComponent);
begin
  inherited Create(AOwner);
  FRetryDelay := 500;
end;

destructor TClientConnectionSocket.Destroy;
begin
  if Assigned(RetryTimerNotifyHandle) then
    EventLoop.RemoveTimerNotify(RetryTimerNotifyHandle);
  inherited Destroy;
end;

procedure TClientConnectionSocket.DoResolve;
begin
  // By default, no resolving is done, so continue directly with connecting
  DoConnect;
end;

procedure TClientConnectionSocket.DoConnect;
begin
  SetConnectionState(connConnecting);

  try
    if not Assigned(EventLoop) then
      raise ESocketError.Create(SSocketNoEventLoopAssigned);
    CanWriteNotifyHandle := EventLoop.SetCanWriteNotify(Stream.Handle,
      @SocketCanWrite, nil);
  except
    SetConnectionState(connDisconnected);
    raise;
  end;
end;

procedure TClientConnectionSocket.SetActive(Value: Boolean);
begin
  if Value <> Active then
  begin
    if Value then
    begin
      // Activate the connection
      FActive := True;
      RetryCounter := 0;

      CreateSocket;
      DoResolve;
    end else
    begin
      // Close the connection
      FActive := False;
      try
        FreeAndNil(FStream);
        if Assigned(CanWriteNotifyHandle) then
        begin
          EventLoop.ClearCanWriteNotify(CanWriteNotifyHandle);
          CanWriteNotifyHandle := nil;
        end;
        if Assigned(RetryTimerNotifyHandle) then
        begin
          EventLoop.RemoveTimerNotify(RetryTimerNotifyHandle);
          RetryTimerNotifyHandle := nil;
        end;
      finally
        SetConnectionState(connDisconnected);
      end;
    end;
  end;
end;

procedure TClientConnectionSocket.SetConnectionState(NewState:
  TConnectionState);
var
  OldState: TConnectionState;
begin
  if NewState <> ConnectionState then
  begin
    OldState := ConnectionState;
    FConnectionState := NewState;
    if Assigned(OnConnectionStateChange) then
      OnConnectionStateChange(Self, OldState, NewState);
  end;
end;


procedure TClientConnectionSocket.RetryTimerNotify(Sender: TObject);
begin
  RetryTimerNotifyHandle := nil;
  Active := True;
end;

procedure TClientConnectionSocket.SocketCanWrite(Sender: TObject);
var
  Error: Integer;
  ErrorLen, GetResult: LongInt;
begin
  if ConnectionState = connConnecting then
  begin
    EventLoop.ClearCanWriteNotify(CanWriteNotifyHandle);
    CanWriteNotifyHandle := nil;

    ErrorLen := SizeOf(Error);
    GetResult := Sockets.fpGetSockOpt(Stream.Handle, SOL_SOCKET, SO_ERROR,
      @Error, @ErrorLen);
    if GetResult <> 0 then
      raise ESocketError.CreateFmt(SSocketConnectFailed,
        [GetPeerName, StrError(GetResult)]);
    if Error <> 0 then
      if (RetryCounter >= Retries) and (Retries >= 0) then
        raise ESocketError.CreateFmt(SSocketConnectFailed,
          [GetPeerName, StrError(Error)])
      else begin
        Active := False;
        RetryTimerNotifyHandle := EventLoop.AddTimerNotify(RetryDelay, False,
          @RetryTimerNotify, Self);
        Inc(RetryCounter);
      end
    else
    begin
      RetryCounter := 0;
      SetConnectionState(connConnected);
    end;
  end;
end;


// TSocketConnectionServer

procedure TSocketConnectionServer.ListenerDataAvailable(Sender: TObject);
var
  ClientSocket: Integer;
  Addr: TInetSockAddr;
  AddrSize: Integer;
begin
  AddrSize := SizeOf(Addr);
  ClientSocket := FpAccept(Stream.Handle, @Addr, @AddrSize);
  if ClientSocket = -1 then
    raise ESocketError.CreateFmt(SSocketAcceptError, [StrError(SocketError)]);

  if DoQueryConnect(ClientSocket) then
    DoConnect(TSocketStream.Create(ClientSocket));
end;

function TSocketConnectionServer.DoQueryConnect(ASocket: Integer): Boolean;
begin
  Result := True;
  if Assigned(OnQueryConnect) then
    OnQueryConnect(Self, ASocket, Result);
end;

procedure TSocketConnectionServer.DoConnect(AStream: TSocketStream);
begin
  if Assigned(OnConnect) then
    OnConnect(Self, AStream);
end;


// TCustomTCPClient

type
  TClientSocketStream = class(TSocketStream)
  protected
    Client: TCustomTCPClient;
    procedure Disconnected; override;
  end;

procedure TClientSocketStream.Disconnected;
begin
  inherited Disconnected;
  Client.Active := False;
end;


destructor TCustomTCPClient.Destroy;
begin
  if Assigned(CanWriteNotifyHandle) then
  begin
    EventLoop.ClearCanWriteNotify(CanWriteNotifyHandle);
    // Set to nil to be sure that descendant classes don't do something stupid
    CanWriteNotifyHandle := nil;
  end;
  inherited Destroy;
end;

procedure TCustomTCPClient.SetHost(const Value: String);
begin
  if Value <> Host then
  begin
    if Active then
      raise ESocketError.Create(SSocketIsActive);
    FHost := Value;
  end;
end;

procedure TCustomTCPClient.SetPort(Value: Word);
begin
  if Value <> Port then
  begin
    if Active then
      raise ESocketError.Create(SSocketIsActive);
    FPort := Value;
  end;
end;

procedure TCustomTCPClient.DoResolve;
var
  HostResolver: THostResolver;
begin
  HostAddr := StrToNetAddr(Host);
  if HostAddr.s_bytes[4] = 0 then
  begin
    HostResolver := THostResolver.Create(nil);
    try
      SetConnectionState(connResolving);
      if not HostResolver.NameLookup(FHost) then
        raise ESocketError.CreateFmt(SHostNotFound, [Host]);
      HostAddr := HostResolver.HostAddress;
    finally
      HostResolver.Free;
    end;
  end;
  DoConnect;
end;

procedure TCustomTCPClient.CreateSocket;
var
  Socket: Integer;
begin

  Socket := Sockets.FPSocket(AF_INET, SOCK_STREAM, 0);
  if Socket = -1 then
    raise ESocketError.CreateFmt(SSocketCreationError,
      [StrError(SocketError)]);
  FStream := TClientSocketStream.Create(Socket);
  TClientSocketStream(FStream).Client := Self;
end;

procedure TCustomTCPClient.DoConnect;
var
  SockAddr: TInetSockAddr;
begin
  inherited DoConnect;
  SockAddr.sin_Family := AF_INET;
  SockAddr.sin_Port := ShortHostToNet(Port);
  SockAddr.sin_Addr.s_addr := Cardinal(HostAddr);
  if Sockets.FpConnect(Stream.Handle, @SockAddr, SizeOf(SockAddr))<>0 Then
    if (SocketError <> sys_EINPROGRESS) and (SocketError <> 0) then
      raise ESocketError.CreateFmt(SSocketConnectFailed,
        [GetPeerName, StrError(SocketError)]);
end;

function TCustomTCPClient.GetPeerName: String;
begin
  Result := Format('%s:%d', [Host, Port]);
end;


// TCustomTCPServer

destructor TCustomTCPServer.Destroy;
begin
  if Assigned(DataAvailableNotifyHandle) then
  begin
    EventLoop.ClearDataAvailableNotify(DataAvailableNotifyHandle);
    // Set to nil to be sure that descendant classes don't do something stupid
    DataAvailableNotifyHandle := nil;
  end;
  inherited Destroy;
end;

procedure TCustomTCPServer.SetActive(Value: Boolean);
var
  Socket, TrueValue: Integer;
  Addr: TInetSockAddr;
begin
  if Active <> Value then
  begin
    FActive := False;
    if Value then
    begin
      Socket := Sockets.fpSocket(AF_INET, SOCK_STREAM, 0);
      if Socket = -1 then
        raise ESocketError.CreateFmt(SSocketCreationError,
          [StrError(SocketError)]);
      TrueValue := 1;
      Sockets.fpSetSockOpt(Socket, SOL_SOCKET, SO_REUSEADDR,
        @TrueValue, SizeOf(TrueValue));
      FStream := TSocketStream.Create(Socket);
      Addr.sin_Family := AF_INET;
      Addr.sin_Port := ShortHostToNet(Port);
      Addr.sin_Addr.s_addr := 0;
      if  fpBind(Socket, @Addr, SizeOf(Addr))<>0 then
        raise ESocketError.CreateFmt(SSocketBindingError,
          [Port, StrError(SocketError)]);
      fpListen(Socket, 5);
      if not Assigned(EventLoop) then
        raise ESocketError.Create(SSocketNoEventLoopAssigned);
      DataAvailableNotifyHandle := EventLoop.SetDataAvailableNotify(Socket,
        @ListenerDataAvailable, nil);
      FActive := True;
    end else
    begin
      FreeAndNil(FStream);
      EventLoop.ClearDataAvailableNotify(DataAvailableNotifyHandle);
      DataAvailableNotifyHandle := nil;
    end;
  end;
end;

end.