I understand

But non-threaded is not an option.. our application is a high performance
video management system, it must be multi-threaded, in fact we have a very
highly threaded architecture, and it would create huge bottlenecks making
it single threaded (for network message processing). This specific part of
our application, is the part that sends video from server to clients. I
assign the socket to a message processing thread that manages sending and
receiving data for this section. I don't necessarily have to send data from
different threads, data can be sent from a single thread (but not main
thread) that is assigned to the socket.

But.. I found a solution to fix my initial problem (sending data from two
different threads).

Can you please validate if this would cause any issues?

I have a custom client class (for TWSocketServer) that is derived
from TWSocketClient that is instantiated by the server for every client
socket that connects to it.
I overwrote the Send function with the following code that checks for Ssl,
if Ssl is disabled then it would call inherited.. but it it is enabled,
instead of calling  TryToSend  directly from the calling thread (that is
causing the issue), it would only add the data to the send buffer and then
post FD_WRITE message, and the thread assigned to the client socket would
pick it up later and send the buffered data from its context.

I didn't change any ICS code, but only my derived class... do you think
that by always adding to the send buffer (PutDataInSendBuffer) and posting
FD_WRITE instead of calling TryToSend could cause any issues? Otherwise,
this fixes the main issue of socket stopping writting data... I will still
investigate on disconnections...


  // When working with SSL, we can't send data from different threads,
  // apparently OpenSSL does not like it. It could be an issue with ICS as
well.
  // To overcome the problem, when sending data with SSL enabled, instead of
  // directly sending the data from the calling thread, we just put the data
  // to the send buffer and post a message so it will be sent later from the
  // thread that is processing the socket messages
  if (not SslEnable) or
     ((FState <> wsConnected) and (FState <> wsSocksConnected)) then
  begin
    inherited;
    Exit;
  end;

  // Add to send buffer
  if Len <= 0 then
    Exit(0)
  else
  begin
    Result := Len;
    PutDataInSendBuffer(Data, Len);
  end;

  // Post message for sending data
  PostMessage(Handle,
              FMsg_WM_ASYNCSELECT,
              WParam(FHSocket),            { V8.08 }
              IcsMakeLong(FD_WRITE, 0));




Thanks
Eric


On Wed, Oct 3, 2018 at 3:10 PM Angus Robertson - Magenta Systems Ltd <
an...@magsys.co.uk> wrote:

> > It happens if I'm sending from a thread or even only from the
> > main thread (from a timer), please check this demo app:
>
> Sorry, won't be looking for more bugs in ICS until the V8.57 release is
> done, it's weeks later already.
>
> I can only suggest trying to avoid threads, I don't use them and don't
> have any problems in my commercial applications.
>
> Angus
>
> --
> To unsubscribe or change your settings for TWSocket mailing list
> please goto http://lists.elists.org/cgi-bin/mailman/listinfo/twsocket
> Visit our website at http://www.overbyte.be
>
-- 
To unsubscribe or change your settings for TWSocket mailing list
please goto http://lists.elists.org/cgi-bin/mailman/listinfo/twsocket
Visit our website at http://www.overbyte.be

Reply via email to