> <--- PSEUDO CODE -------->
> 
> bool WaitOnWrite = false;
> int res;
> 
> // it associates FD_WRITE event with SockEvent object
> 
>   WSAEventSelect(sock,SockEvent,FD_WRITE);
> 
> for (;;)
> {
> 
>   if (WaitOnWrite) 
>     WaitForMultipleObjects(SockEvent)
>   else                
>     WaitForMultipleObjects(UserEvent);
> 
> 
>   res = SSL_write(sock,userBuf,len);
>   res = SSL_get_error(sock,res);
> 
>   switch(res)
>   { 
>     case SSL_ERROR_NONE:
>       WaitOnWrite = false;
>       break;
> 
>     case SSL_ERROR_WANT_WRITE:
>       WaitOnWrite = false;
>       break;
>   }
> }
> 
> <--- PSEUDO CODE END -------->

This won't work for a variety of reasons. One is that an SSL_write may fail 
because of a negotiation in progress and being able to *read* data from the 
socket may allow the write to progress.

I would suggest some changes to this loop that should solve your problems:

1) Protect all SSL_read/SSL_write functions with a mutex (per-connection if you 
want). This will prevent you from calling SSL_read an SSL_write at the same 
time for the same connection. It will also allow you to synchronize your 
handling of cases where you need to wait.

2) Use only a single event. That way, you can never get the 'WaitOnWrite' flag 
wrong since you don't need it. If an OpenSSL operation fails because of 
WANT_READ or WANT_WRITE, block the event while you still hold the mutex, 
release the mutex, then block on the event.

Now you just need to make sure that if anything changes, that event will be 
unblocked. If a read or write becomes possible, the system will unblock the 
event for you. If you change anything, you need to unblock the event, so:

4) If an OpenSSL function succeeds, unblock the event while you still hold the 
mutex. That way, if an SSL_read consumes the data that was needed to let an 
SSL_write succeed, you will retry the SSL_write automatically.

Again, the short version is this: Anytime anything changes, retry all 
operations that were waiting for something.

DS


______________________________________________________________________
OpenSSL Project                                 http://www.openssl.org
User Support Mailing List                    [email protected]
Automated List Manager                           [EMAIL PROTECTED]

Reply via email to