Re: [twsocket] Found a bug and made a fix in function UrlDecode

2010-08-09 Thread Bjørnar Nielsen
Arno,

 ... try the following code and let me know how it works for you, 

The code works for me. But should not  the first overload directive be inside 
the conditional define?

Regards Bjørnar
--
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

Re: [twsocket] Found a bug and made a fix in function UrlDecode

2010-08-09 Thread Arno Garrels
Bjørnar Nielsen wrote:
 Arno,
 
 ... try the following code and let me know how it works for you,
 
 The code works for me. But should not  the first overload directive
 be inside the conditional define? 

Yes, it's better inside, corrected. Just updated the svn repository.

-- 
Arno Garrels

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

[twsocket] Detecting POP3 protocol state and disconnecting

2010-08-09 Thread Zvone
A few POP3 related questions...

1. How am I supposed to detect when POP3 component has disconnected or
what state is it in?

OnSessionClosed appears to trigger BEFORE it disconnects but after
QUIT is sent (not logical for me, should be the last one to trigger)
and what seems to trigger at the disconnection time is OnRequestDone
when TPop3Request is pop3Quit which appears to be the last one to
happen. But if I immediately during this event start new batch of POP3
commands (by setting new server, new port etc.) it may sometimes
trigger pop3 already connected and sometimes won't. So obviously it
is still connected.

Also, is it good to fill pop3 component with new server name while in
event handler or how to implement a small time-delay (to fill it right
after the event is handled)?

Next I tried OnStateChange event. It is supposed to update
SyncPop3Cli1-State or perhaps ProtocolState right? Well... it does
trigger but SyncPop3Cli1-State always returns pop3Ready (defined as
0). Same goes for SyncPop3Cli1-ProtocolState - whenever I read it it
always says pop3Disconnected. So why these are not updated or how to
read them?

Finally, I tried SyncPop3Cli1-Connected - which always returns true.

I could start 2-3 pop3 components for 2-3 servers rather than waiting
for first one to finish, but that is rather dumb idea if protocol
state can be detected.


2. What good is TimeOut property for? It is obviously not for timeout
like for example waiting for server response because you have to use
TTimer for that. I also tried to see if it will be used for DNS lookup
or something like that but it doesn't seem to have any effect...
--
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


Re: [twsocket] DNS Problem

2010-08-09 Thread Eric Fleming Bonilha

Arno Garrels wrote:


So multiple calls to DnsLookup from the same thread context are
serialized.


In other words, DnsLookup always returns immediately, however
Win32 API WSAAsyncGetHostByName serializes multiple requests from
the same thread context internally. 
This may trigger OnDnsLookupDone delayed but does not block a

thread.

A workaround was to write your own name resolution that uses 
multiple threads.


Thank you Arno

Now I understand the structure better

But, in any case, the thread should not block in my software right?

Eric 
--

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


Re: [twsocket] Detecting POP3 protocol state and disconnecting

2010-08-09 Thread Arno Garrels
Zvone wrote:
 A few POP3 related questions...
 
 1. How am I supposed to detect when POP3 component has disconnected or
 what state is it in?

OnSessionClosed is the event to initiate a reconnect delayed (see below).
 
 OnSessionClosed appears to trigger BEFORE it disconnects but after
 QUIT is sent (not logical for me, should be the last one to trigger)
 and what seems to trigger at the disconnection time is OnRequestDone
 when TPop3Request is pop3Quit which appears to be the last one to
 happen. But if I immediately during this event start new batch of POP3
 commands (by setting new server, new port etc.) it may sometimes
 trigger pop3 already connected and sometimes won't. So obviously it
 is still connected.

Yes, one cannot rely on these events always trigger in the same order.
Also it might happen that you get a response to the QUIT request but the
server doesn't close the connection. In this case after some timeout
exceeded you have to close the connection yourself. In latest ICS V7
there's an basic timeout feature implemented in TWSocket that you could
use for this purpose, this feature has to be explicitly enabled by
setting conditional define EXPERIMENTAL_TIMEOUT in OverbyteIcsDefs.inc.
When built with EXPERIMENTAL_TIMEOUT TPop3Cli.CtrlSocket will get 
three new properties and one new event: 

property TimeoutSampleInterval: LongWord
property ConnectTimeout: LongWord
property IdleTimeout: LongWord
property OnTimeout: TTimeoutEvent   
 
 Also, is it good to fill pop3 component with new server name while in
 event handler or how to implement a small time-delay (to fill it right
 after the event is handled)?

The correct way to reconnect (delayed) is: 
In the OnSessionClosed event handler post a custom Windows message 
to some Window or to the form handle and from the message handler 
populate properties and start the next session. 

[schnip]
 
 I could start 2-3 pop3 components for 2-3 servers rather than waiting
 for first one to finish, but that is rather dumb idea if protocol
 state can be detected.

No problem to have multiple instances, you can use the same event
handlers for all instances, just use the Sender argument and cast it 
to, for instance, TPop3Cli.
 
 
 2. What good is TimeOut property for? It is obviously not for timeout
 like for example waiting for server response because you have to use
 TTimer for that. I also tried to see if it will be used for DNS lookup
 or something like that but it doesn't seem to have any effect...

This timeout only works (if at all) with the synchronous methods or 
TSyncPop3Cli.

-- 
Arno Garrels
--
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


Re: [twsocket] DNS Problem

2010-08-09 Thread Arno Garrels
Eric Fleming Bonilha wrote:
 Arno Garrels wrote:
 
 So multiple calls to DnsLookup from the same thread context are
 serialized.
 
 In other words, DnsLookup always returns immediately, however
 Win32 API WSAAsyncGetHostByName serializes multiple requests from
 the same thread context internally.
 This may trigger OnDnsLookupDone delayed but does not block a
 thread.
 
 A workaround was to write your own name resolution that uses
 multiple threads.
 
 Thank you Arno
 
 Now I understand the structure better
 
 But, in any case, the thread should not block in my software right?

Correct, the calling thread won't be blocked.

-- 
Arno Garrels
--
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


Re: [twsocket] DNS Problem

2010-08-09 Thread Eric Fleming Bonilha


Correct, the calling thread won't be blocked.



Arno,

I´m having a problem with a customer, and I sucessfully reproduced the same 
problem here. I really believe that it is related to DNS Lookup. Here is 
what is happening:


- I have a communication thread that spawns all TCP client sockets, so, all 
communication messages are carried by this thread, not by the main thread
- I can have several instaces of TCP clients spawned by the communication 
thread


The test that I did:

- I have some TCP sockets connected to fixed IP addresses and receiving 
realtime video data (MPEG-4 video stream)
- When I try to connect to a DNS address from dnsalias.com, all the data 
that was receiving from all TCP sockets stops for 2 seconds.


I believe that this time is the DNS lookup because if I try to ping the 
address it takes about 2 seconds to resolve the address


You told me that the thread would not blobk, but it stops receiving all TCP 
data during the DNS lookup?


Any idea on why does it happens?

If I make my own DNS lookup using threads, would it solve?

Thanks!

Eric


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


Re: [twsocket] DNS Problem

2010-08-09 Thread Eric Fleming Bonilha


Correct, the calling thread won't be blocked.



Arno,

I have checked the ICS code, and I found this:

OverbyteIcsWSocket.pas on line 7290 (TCustomWSocket.Connect)

   if not FAddrResolved then begin
   { The next line will trigger an exception in case of failure }
   sin.sin_addr.s_addr := 
WSocket_Synchronized_ResolveHost(AnsiString(FAddrStr)).s_addr;

   FAddrResolved := TRUE;
   end;

So, when I first call connect, it will try to resolve the host by using 
WSocket_Synchronized_ResolveHost


This is a sync routine, it just quits when it resolves, and in that case, 
the thread will be blocked until the address is resolved.


That is the problem that I´m having, because on my customer site it takes 2 
seconds to resolve the ip address, and during this time, all TCP 
communications are dead, because the .Connect method is called from the 
communication thread context


Am I using an old version of ICS? I´m using version 7 with D2010.

If this is the right way of ICS working, then I think I have to make a DNS 
lookup thread


Thanks
Eric 


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


Re: [twsocket] DNS Problem

2010-08-09 Thread Arno Garrels
Eric,

 Correct, the calling thread won't be blocked.
 
 
 Arno,
 
 I have checked the ICS code, and I found this:
 
 OverbyteIcsWSocket.pas on line 7290 (TCustomWSocket.Connect)
 
 if not FAddrResolved then begin
 { The next line will trigger an exception in case of
 failure } sin.sin_addr.s_addr :=
 WSocket_Synchronized_ResolveHost(AnsiString(FAddrStr)).s_addr;
 FAddrResolved := TRUE;
 end;
 
 So, when I first call connect, it will try to resolve the host by
 using WSocket_Synchronized_ResolveHost

Sorry I misunderstood. I thought you used method DnsLookup.
So in this case it is simple to fix, call method DnsLookup and connect
with a dotted IP address from the OnDnsLookupDone event handler.

-- 
Arno Garrels 


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


Re: [twsocket] DNS Problem

2010-08-09 Thread Eric Fleming Bonilha

Sorry I misunderstood. I thought you used method DnsLookup.
So in this case it is simple to fix, call method DnsLookup and connect
with a dotted IP address from the OnDnsLookupDone event handler.



Hi Arno

Thanks for the clarification

So, if I use the method DnsLookup all the requests will be serialized 
correct?


If I try to connect to several different DNS that takes several seconds to 
resolve (I tested in my customer site and it actually takes 5 to 6 seconds 
to resolve), it means that if it is serialized, the last request will take 
very long to respond


Instead, if I spaw a thread (and release after use) for each lookup and use 
the routine WSocketResolveHost from the new thread context, I can response 
many addresses all at once, correct?


Is it safe to use WSocketResolveHost routine? I can translate the result to 
dotted IP address easily


Thanks
Eric 


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


Re: [twsocket] DNS Problem

2010-08-09 Thread Arno Garrels
Eric,

 Sorry I misunderstood. I thought you used method DnsLookup.
 So in this case it is simple to fix, call method DnsLookup and
 connect with a dotted IP address from the OnDnsLookupDone event
 handler. 
 
 
 Hi Arno
 
 Thanks for the clarification
 
 So, if I use the method DnsLookup all the requests will be serialized
 correct?

Correct, and it won't block the calling thread. 

 
 If I try to connect to several different DNS that takes several
 seconds to resolve (I tested in my customer site and it actually
 takes 5 to 6 seconds to resolve), it means that if it is serialized,
 the last request will take very long to respond

That's true as well.
 
 Instead, if I spaw a thread (and release after use) for each lookup
 and use the routine WSocketResolveHost from the new thread context, I
 can response many addresses all at once, correct?

In parallel yes, the more CPUs or cores the better.
 
 Is it safe to use WSocketResolveHost routine? I can translate the
 result to dotted IP address easily

Yes, it is safe, however I would consider to use WSocket_gethostbyname 
as well (more work).

-- 
Arno Garrels


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


Re: [twsocket] DNS Problem

2010-08-09 Thread Eric Fleming Bonilha

Yes, it is safe, however I would consider to use WSocket_gethostbyname
as well (more work).



What is the differenc? I See that wsocket_gethostbyname returns more 
information, but is there a reason to use it instead of the other method?


Thanks

Eric 


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


Re: [twsocket] DNS Problem

2010-08-09 Thread Arno Garrels
Eric Fleming Bonilha wrote:
 Yes, it is safe, however I would consider to use
 WSocket_gethostbyname as well (more work).
 
 
 What is the differenc? I See that wsocket_gethostbyname returns more
 information, but is there a reason to use it instead of the other
 method? 

WSocket_Synchronized_ResolveHost includes checks like
WSocketIsDottedIP() etc. which doesn't require to run
in a worker thread. I would start a worker thread only
in case wsocket_gethostbyname must be called.   

If you want to optimize this a bit more you could use
a thread pool, since creating threads takes time as well.
Perhaps something like TIcsAsyncDnsLookup that I wrote for
async name resolution with IPv6 recently, it has a thread
pool, however uses the GetAddrInfo() API which was easy to change: 
http://svn.overbyte.be:8443/svn/ics/branches/icsipv6/Delphi/Vc32/OverbyteIcsWSocket.pas
User and password both ics.

-- 
Arno Garrels
--
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