Skip to content

Commit

Permalink
Adding threadsafty for reading and writing
Browse files Browse the repository at this point in the history
  • Loading branch information
Frederic Kehrein committed Feb 18, 2020
1 parent 9a00366 commit db8cc86
Show file tree
Hide file tree
Showing 3 changed files with 308 additions and 113 deletions.
5 changes: 2 additions & 3 deletions example.lpi
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
<?xml version="1.0" encoding="UTF-8"?>
<CONFIG>
<ProjectOptions>
<Version Value="11"/>
<Version Value="12"/>
<PathDelim Value="\"/>
<General>
<Flags>
<MainUnitHasCreateFormStatements Value="False"/>
<MainUnitHasTitleStatement Value="False"/>
<MainUnitHasScaledStatement Value="False"/>
<CompatibilityMode Value="True"/>
</Flags>
<SessionStorage Value="InProjectDir"/>
<MainUnit Value="0"/>
<Title Value="example"/>
<UseAppBundle Value="False"/>
<ResourceType Value="res"/>
Expand All @@ -24,7 +24,6 @@
</PublishOptions>
<RunParams>
<FormatVersion Value="2"/>
<Modes Count="0"/>
</RunParams>
<Units Count="3">
<Unit0>
Expand Down
17 changes: 7 additions & 10 deletions example.lpr
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

{$mode objfpc}{$H+}

uses {$IFDEF UNIX} {$IFDEF UseCThreads}
cthreads, {$ENDIF} {$ENDIF}
uses {$IFDEF UNIX}
cthreads, {$ENDIF}
Classes,
SysUtils,
WebSocket,
Expand Down Expand Up @@ -40,8 +40,7 @@ TSocketHandler = class(TThreadedWebsocketHandler)
var
str: string;
begin
WriteLn('Connected to ', NetAddrToStr(
ACommunication.SocketStream.RemoteAddress.sin_addr));
WriteLn('Connected to ', ACommunication.SocketStream.RemoteAddress.Address);
ACommunication.OnRecieveMessage := @MessageRecieved;
ACommunication.OnClose := @ConnectionClosed;
while ACommunication.Open do
Expand All @@ -55,8 +54,8 @@ TSocketHandler = class(TThreadedWebsocketHandler)
finally
Free;
end;
WriteLn('Message sent to ',
NetAddrToStr(ACommunication.SocketStream.RemoteAddress.sin_addr), ': ', str);
WriteLn('Message to ', ACommunication.SocketStream.RemoteAddress.Address,
': ', str);
end;
socket.Stop(True);
end;
Expand All @@ -66,8 +65,7 @@ TSocketHandler = class(TThreadedWebsocketHandler)
Comm: TWebsocketCommunincator;
begin
Comm := TWebsocketCommunincator(Sender);
WriteLn('Connection to ', NetAddrToStr(Comm.SocketStream.RemoteAddress.sin_addr),
' closed');
WriteLn('Connection to ', Comm.SocketStream.RemoteAddress.Address, ' closed');
end;

procedure TSocketHandler.MessageRecieved(Sender: TObject);
Expand All @@ -83,8 +81,7 @@ TSocketHandler = class(TThreadedWebsocketHandler)
for m in Messages do
if m is TWebsocketStringMessage then
begin
WriteLn('Message from ',
NetAddrToStr(Comm.SocketStream.RemoteAddress.sin_addr),
WriteLn('Message from ', Comm.SocketStream.RemoteAddress.Address,
': ', TWebsocketStringMessage(m).Data);
end;
finally
Expand Down
Loading

0 comments on commit db8cc86

Please sign in to comment.