Skip to content

Commit

Permalink
last preparation step to WebSockets async server
Browse files Browse the repository at this point in the history
  • Loading branch information
Arnaud Bouchez committed Feb 24, 2021
1 parent 0e84a61 commit e9e1cd9
Show file tree
Hide file tree
Showing 6 changed files with 79 additions and 114 deletions.
5 changes: 2 additions & 3 deletions src/net/mormot.net.client.pas
Original file line number Diff line number Diff line change
Expand Up @@ -2389,8 +2389,7 @@ function SendEmail(const Server: TSMTPConnection; const From, CsvDest, Subject,
result := SendEmail(
Server.Host, From, CsvDest, Subject, Text, Headers,
Server.User, Server.Pass, Server.Port, TextCharSet,
(Server.Port = '465') or
(Server.Port = '587'));
aTLS or (Server.Port = '465') or (Server.Port = '587'));
end;

{$I-}
Expand Down Expand Up @@ -2430,7 +2429,7 @@ function SendEmail(const Server, From, CsvDest, Subject, Text, Headers, User,
P := pointer(CsvDest);
if P = nil then
exit;
TCP := Open(Server, Port, aTLS);
TCP := SocketOpen(Server, Port, aTLS);
if TCP <> nil then
try
TCP.CreateSockIn; // we use SockIn and SockOut here
Expand Down
40 changes: 20 additions & 20 deletions src/net/mormot.net.relay.pas
Original file line number Diff line number Diff line change
Expand Up @@ -405,14 +405,31 @@ procedure SetRestFrame(out frame: TWebSocketFrame; status: integer;

{ TRelayServerProtocol }

constructor TRelayServerProtocol.Create(aOwner: TPublicRelay;
const aServerKey: RawUtf8);
begin
fOwner := aOwner;
inherited Create('synopserelay', '');
if aServerKey <> '' then
SetEncryptKey({aServer=}true, aServerKey);
end;

function TRelayServerProtocol.Clone(
const aClientUri: RawUtf8): TWebSocketProtocol;
begin
result := TRelayServerProtocol.Create(fOwner, '');
if fEncryption <> nil then
TRelayServerProtocol(result).fEncryption := fEncryption.Clone;
end;

procedure TRelayServerProtocol.ProcessIncomingFrame(Sender: TWebSocketProcess;
var Frame: TWebSocketFrame; const info: RawUtf8);
var
log: TSynLog;
connection: THttpServerConnectionID;
sent: boolean;
rest: TRelayFrameRestPayload;
client: TWebSocketServerResp;
client: TWebSocketProcessServer;
i: PtrInt;
p: ^THttpServerRequest;
begin
Expand Down Expand Up @@ -491,9 +508,9 @@ procedure TRelayServerProtocol.ProcessIncomingFrame(Sender: TWebSocketProcess;
else
begin
// redirect the frame to the final client
sent := client.WebSocketProcess.SendFrame(Frame);
sent := client.SendFrame(Frame);
log.Log(LOG_DEBUGERROR[not sent], 'ProcessIncomingFrame % #% % %',
[ToText(Frame.opcode)^, connection, client.WebSocketProcess.RemoteIP,
[ToText(Frame.opcode)^, connection, client.RemoteIP,
KBNoSpace(length(Frame.payload))], self);
end;
end;
Expand All @@ -507,23 +524,6 @@ procedure TRelayServerProtocol.ProcessIncomingFrame(Sender: TWebSocketProcess;
end;
end;

constructor TRelayServerProtocol.Create(aOwner: TPublicRelay;
const aServerKey: RawUtf8);
begin
fOwner := aOwner;
inherited Create('synopserelay', '');
if aServerKey <> '' then
SetEncryptKey({aServer=}true, aServerKey);
end;

function TRelayServerProtocol.Clone(
const aClientUri: RawUtf8): TWebSocketProtocol;
begin
result := TRelayServerProtocol.Create(fOwner, '');
if fEncryption <> nil then
TRelayServerProtocol(result).fEncryption := fEncryption.Clone;
end;


{ TSynopseServerProtocol }

Expand Down
4 changes: 3 additions & 1 deletion src/net/mormot.net.server.pas
Original file line number Diff line number Diff line change
Expand Up @@ -1441,7 +1441,7 @@ destructor THttpServer.Destroy;
endtix: Int64;
i: PtrInt;
resp: THttpServerResp;
callback: TNetSocket;
callback: TNetSocket; // touch-and-go to the server to release main Accept()
begin
Terminate; // set Terminated := true for THttpServerResp.Execute
if fThreadPool <> nil then
Expand Down Expand Up @@ -1940,6 +1940,8 @@ constructor THttpServerSocket.Create(aServer: THttpServer);
fCompress := aServer.fCompress;
fCompressAcceptEncoding := aServer.fCompressAcceptEncoding;
fSocketLayer := aServer.Sock.SocketLayer;
TLS.Enabled := aServer.Sock.TLS.Enabled; // not implemented yet
OnLog := aServer.Sock.OnLog;
end;
end;

Expand Down
14 changes: 8 additions & 6 deletions src/net/mormot.net.sock.pas
Original file line number Diff line number Diff line change
Expand Up @@ -523,6 +523,7 @@ TCrtSocket = class
/// connect to aServer:aPort
// - optionaly via TLS (using the SChannel API on Windows, or by including
// mormot.lib.openssl11 unit to your project) - with custom input options
// - see also SocketOpen() for a wrapper catching any connection exception
constructor Open(const aServer, aPort: RawUtf8; aLayer: TNetLayer = nlTCP;
aTimeOut: cardinal = 10000; aTLS: boolean = false; aTLSContext: PNetTLSContext = nil);
/// bind to an address
Expand Down Expand Up @@ -803,10 +804,10 @@ TUri = record
'80', '443');


/// create a TCrtSocket, returning nil on error
// (useful to easily catch socket error exception ENetSock)
function Open(const aServer, aPort: RawUtf8;
aTLS: boolean = false): TCrtSocket;
/// create a TCrtSocket instance, returning nil on error
// - useful to easily catch any exception, and provide a custom TNetTLSContext
function SocketOpen(const aServer, aPort: RawUtf8;
aTLS: boolean = false; aTLSContext: PNetTLSContext = nil): TCrtSocket;


implementation
Expand Down Expand Up @@ -2613,10 +2614,11 @@ function TUri.Root: RawUtf8;
Root := copy(address, 1, i - 1);
end;

function Open(const aServer, aPort: RawUtf8; aTLS: boolean): TCrtSocket;
function SocketOpen(const aServer, aPort: RawUtf8; aTLS: boolean;
aTLSContext: PNetTLSContext): TCrtSocket;
begin
try
result := TCrtSocket.Open(aServer, aPort, nlTCP, 10000, aTLS);
result := TCrtSocket.Open(aServer, aPort, nlTCP, 10000, aTLS, aTLSContext);
except
result := nil;
end;
Expand Down
12 changes: 6 additions & 6 deletions src/net/mormot.net.ws.core.pas
Original file line number Diff line number Diff line change
Expand Up @@ -103,10 +103,6 @@ TWebSocketFrame = record
FRAME_LEN_2BYTES = 126;
FRAME_LEN_8BYTES = 127;

const
/// tht HTTP status code returned when a WebSockets
HTTP_WEBSOCKETCLOSED = 0;


/// used to return the text corresponding to a specified WebSockets frame type
function ToText(opcode: TWebSocketFrameOpCode): PShortString; overload;
Expand Down Expand Up @@ -663,6 +659,7 @@ TWebSocketProcess = class(TSynPersistent)
/// the associated 'Remote-IP' HTTP header value
// - returns '' if Protocol=nil or Protocol.RemoteLocalhost=true
function RemoteIP: RawUtf8;
{$ifdef HASINLINE}inline;{$endif}
/// direct access to the low-level incoming frame stack
property Incoming: TWebSocketFrameList
read fIncoming;
Expand Down Expand Up @@ -781,6 +778,7 @@ function ToText(st: TWebSocketProcessState): PShortString;

procedure ComputeChallenge(const Base64: RawByteString; out Digest: TSha1Digest);
const
// see https://tools.ietf.org/html/rfc6455
SALT: string[36] = '258EAFA5-E914-47DA-95CA-C5AB0DC85B11';
var
SHA: TSha1;
Expand Down Expand Up @@ -2161,7 +2159,8 @@ function TWebSocketProcess.NotifyCallback(aRequest: THttpServerRequestAbstract;
HiResDelay(start);
if fState in [wpsDestroy, wpsClose] then
begin
result := HTTP_WEBSOCKETCLOSED;
WebSocketLog.Add.Log(sllError,
'NotifyCallback on closed connection', self);
exit;
end;
if fIncoming.AnswerToIgnore = 0 then
Expand Down Expand Up @@ -2200,7 +2199,8 @@ function TWebSocketProcess.NotifyCallback(aRequest: THttpServerRequestAbstract;
while not fIncoming.Pop(fProtocol, head, answer) do
if fState in [wpsDestroy, wpsClose] then
begin
result := HTTP_WEBSOCKETCLOSED;
WebSocketLog.Add.Log(sllError,
'NotifyCallback on closed connection', self);
exit;
end
else if GetTickCount64 > max then
Expand Down
Loading

0 comments on commit e9e1cd9

Please sign in to comment.