| 1 | unit UTCPServer;
|
|---|
| 2 |
|
|---|
| 3 | {$mode Delphi}{$H+}
|
|---|
| 4 |
|
|---|
| 5 | interface
|
|---|
| 6 |
|
|---|
| 7 | uses
|
|---|
| 8 | Classes, SysUtils,
|
|---|
| 9 | {$IFDEF WINDOWS}
|
|---|
| 10 | WinSock,
|
|---|
| 11 | {$ELSE}
|
|---|
| 12 | baseunix, sockets,
|
|---|
| 13 | //LibC,
|
|---|
| 14 | {$ENDIF}
|
|---|
| 15 | BlckSock, UPool, UResetableThread;
|
|---|
| 16 |
|
|---|
| 17 | type
|
|---|
| 18 | TTCPServer = class;
|
|---|
| 19 |
|
|---|
| 20 | { TTCPClientThread }
|
|---|
| 21 |
|
|---|
| 22 | TTCPClientThread = class(TResetableThread)
|
|---|
| 23 | Parent: TTCPServer;
|
|---|
| 24 | Socket: TTCPBlockSocket;
|
|---|
| 25 | procedure Execute;
|
|---|
| 26 | constructor Create;
|
|---|
| 27 | destructor Destroy; override;
|
|---|
| 28 | end;
|
|---|
| 29 |
|
|---|
| 30 | { TClientThreadedPool }
|
|---|
| 31 |
|
|---|
| 32 | TClientThreadedPool = class(TThreadPool)
|
|---|
| 33 | protected
|
|---|
| 34 | function NewItemObject: TObject; override;
|
|---|
| 35 | private
|
|---|
| 36 | FActive: Boolean;
|
|---|
| 37 | procedure SetActive(const AValue: Boolean);
|
|---|
| 38 | public
|
|---|
| 39 | property Active: Boolean read FActive write SetActive;
|
|---|
| 40 | end;
|
|---|
| 41 |
|
|---|
| 42 | { TAcceptThread }
|
|---|
| 43 |
|
|---|
| 44 | TAcceptThread = class(TThread)
|
|---|
| 45 | Parent: TTCPServer;
|
|---|
| 46 | procedure Execute; override;
|
|---|
| 47 | end;
|
|---|
| 48 |
|
|---|
| 49 | { TTCPServer }
|
|---|
| 50 |
|
|---|
| 51 | TTCPServer = class
|
|---|
| 52 | private
|
|---|
| 53 | FOnClientConnect: TNotifyEvent;
|
|---|
| 54 | Socket: TTCPBlockSocket;
|
|---|
| 55 | FActive: Boolean;
|
|---|
| 56 | AcceptThread: TAcceptThread;
|
|---|
| 57 | procedure SetActive(const AValue: Boolean);
|
|---|
| 58 | public
|
|---|
| 59 | ThreadPool: TClientThreadedPool;
|
|---|
| 60 | Address: string;
|
|---|
| 61 | Port: Word;
|
|---|
| 62 | constructor Create;
|
|---|
| 63 | destructor Destroy; override;
|
|---|
| 64 | property Active: Boolean read FActive write SetActive;
|
|---|
| 65 | property OnClientConnect: TNotifyEvent read FOnClientConnect
|
|---|
| 66 | write FOnClientConnect;
|
|---|
| 67 | end;
|
|---|
| 68 |
|
|---|
| 69 | implementation
|
|---|
| 70 |
|
|---|
| 71 | { TTCPServer }
|
|---|
| 72 |
|
|---|
| 73 | procedure TTCPServer.SetActive(const AValue: Boolean);
|
|---|
| 74 | begin
|
|---|
| 75 | if AValue and not FActive then begin
|
|---|
| 76 | with Socket do begin
|
|---|
| 77 | ThreadPool.Active := True;
|
|---|
| 78 | CreateSocket;
|
|---|
| 79 | SetLinger(True, 10);
|
|---|
| 80 | WriteLn(Address + ':' + IntToStr(Port));
|
|---|
| 81 | Bind(Address, IntToStr(Port));
|
|---|
| 82 | WriteLn(LastError);
|
|---|
| 83 | if LastError <> 0 then raise Exception.Create('Socket bind error');
|
|---|
| 84 | Listen;
|
|---|
| 85 | if LastError <> 0 then raise Exception.Create('Socket listen error');
|
|---|
| 86 | AcceptThread := TAcceptThread.Create(True);
|
|---|
| 87 | AcceptThread.Parent := Self;
|
|---|
| 88 | AcceptThread.FreeOnTerminate := False;
|
|---|
| 89 | AcceptThread.Resume;
|
|---|
| 90 | end;
|
|---|
| 91 | end else
|
|---|
| 92 | if not AValue and FActive then begin
|
|---|
| 93 | with Socket do begin
|
|---|
| 94 | AcceptThread.Terminate;
|
|---|
| 95 | AcceptThread.WaitFor;
|
|---|
| 96 | AcceptThread.Destroy;
|
|---|
| 97 | ThreadPool.Active := False;
|
|---|
| 98 | CloseSocket;
|
|---|
| 99 | end;
|
|---|
| 100 | end;
|
|---|
| 101 | FActive := AValue;
|
|---|
| 102 | end;
|
|---|
| 103 |
|
|---|
| 104 | constructor TTCPServer.Create;
|
|---|
| 105 | begin
|
|---|
| 106 | ThreadPool := TClientThreadedPool.Create;
|
|---|
| 107 | ThreadPool.TotalCount := 10;
|
|---|
| 108 | ThreadPool.Active := True;
|
|---|
| 109 |
|
|---|
| 110 | Socket := TTCPBlockSocket.Create;
|
|---|
| 111 | Address := '0.0.0.0';
|
|---|
| 112 | Port := 80;
|
|---|
| 113 | end;
|
|---|
| 114 |
|
|---|
| 115 | destructor TTCPServer.Destroy;
|
|---|
| 116 | begin
|
|---|
| 117 | ThreadPool.Destroy;
|
|---|
| 118 | Active := False;
|
|---|
| 119 | Socket.Destroy;
|
|---|
| 120 | inherited Destroy;
|
|---|
| 121 | end;
|
|---|
| 122 |
|
|---|
| 123 | { TAcceptThread }
|
|---|
| 124 |
|
|---|
| 125 | procedure TAcceptThread.Execute;
|
|---|
| 126 | var
|
|---|
| 127 | NewSocket: TSocket;
|
|---|
| 128 | NewObject: TTCPClientThread;
|
|---|
| 129 | begin
|
|---|
| 130 | repeat
|
|---|
| 131 | if Parent.Socket.CanRead(1000) then begin
|
|---|
| 132 | NewSocket := Parent.Socket.Accept;
|
|---|
| 133 | if Parent.Socket.LastError = 0 then begin
|
|---|
| 134 | NewObject := TTCPClientThread(Parent.ThreadPool.Acquire);
|
|---|
| 135 | NewObject.Parent := Parent;
|
|---|
| 136 | NewObject.Socket.Socket := NewSocket;
|
|---|
| 137 | NewObject.Start;
|
|---|
| 138 | end;
|
|---|
| 139 | end;
|
|---|
| 140 | until Terminated;
|
|---|
| 141 | end;
|
|---|
| 142 |
|
|---|
| 143 | { TTCPClientThread }
|
|---|
| 144 |
|
|---|
| 145 | procedure TTCPClientThread.Execute;
|
|---|
| 146 | begin
|
|---|
| 147 | if Assigned(Parent.FOnClientConnect) then
|
|---|
| 148 | Parent.FOnClientConnect(Self);
|
|---|
| 149 |
|
|---|
| 150 | Parent.ThreadPool.Release(Self);
|
|---|
| 151 | end;
|
|---|
| 152 |
|
|---|
| 153 | constructor TTCPClientThread.Create;
|
|---|
| 154 | begin
|
|---|
| 155 | inherited;
|
|---|
| 156 | Method := Execute;
|
|---|
| 157 | Socket := TTCPBlockSocket.Create;
|
|---|
| 158 | end;
|
|---|
| 159 |
|
|---|
| 160 | destructor TTCPClientThread.Destroy;
|
|---|
| 161 | begin
|
|---|
| 162 | Socket.Destroy;
|
|---|
| 163 | inherited;
|
|---|
| 164 | end;
|
|---|
| 165 |
|
|---|
| 166 | { TClientThreadedPool }
|
|---|
| 167 |
|
|---|
| 168 | function TClientThreadedPool.NewItemObject: TObject;
|
|---|
| 169 | begin
|
|---|
| 170 | Result := TTCPClientThread.Create;
|
|---|
| 171 | TResetableThread(Result).OnException := ThreadException;
|
|---|
| 172 | end;
|
|---|
| 173 |
|
|---|
| 174 | procedure TClientThreadedPool.SetActive(const AValue: Boolean);
|
|---|
| 175 | begin
|
|---|
| 176 | FActive := AValue;
|
|---|
| 177 | end;
|
|---|
| 178 |
|
|---|
| 179 | end.
|
|---|
| 180 |
|
|---|