Cancel read on socket

Paul Whitfield paulw at omnitronics.com.au
Wed May 28 07:08:57 UTC 2008


Sergei Organov wrote:
> Leon Pollak <leonp at plris.com> writes:
> 
>> On Monday, 26 בMay 2008, Sergei Organov wrote:
>>> shutdown()
>> I studied the internet again.
>> And again I am afraid that shutdown will not help - please, correct me if I am 
>> wrong:
>> 1. shutdown seems not to cancel currently in progress "read" operation (which 
>> blocked the task now).
> 
> Well, I'm not sure about UDP sockets, but for TCP sockets it should cancel
> read operation the same way as if remote end had closed connection, I
> believe. Though the socket won't be useful for reading after that anyway.
> 
> Alternate approach to your problem could be to configure receive timeout
> for the socket, like this:
> 
>   setsockopt(s, SOL_SOCKET, SO_RCVTIMEO, &tv, len);
> 

If you have don't want/need a timeout you could modify your
code to make use of a SO_RCVWAKEUP callback.

You could create a callback that sends an event to the waiting task.
To cancel send a different event to the waiting task


Paul ...

It goes something like this (error handling removed):


/* To install the callback */
struct sockwakeup wakeup;

wakeup.sw_pfn = callback;
wakeup.sw_arg = (caddr_t ) fd;

setsockopt(fd, SOL_SOCKET, SO_RCVWAKEUP, &wakeup, sizeof(wakeup));



/* The callback */
void callback(struct socket *so, caddr_t arg)
{
    rtems_event_send( task, EVENT_UDP_RX );
}



/* The Cancel function */
void cancel_udp_wait(void )
{
    rtems_event_send(task, EVENT_UDP_CANCEL);
}


/* The Application Code */

    rtems_event_receive(  EVENT_UDP_RX | EVENT_UDP_CANCEL ,     	
		RTEMS_EVENT_ANY | RTEMS_WAIT,
		RTEMS_NO_TIMEOUT,
		&event);

    if ( event & EVENT_UDP_RX )
    {
        // Read and Process
    }
    if ( event & EVENT_UDP_CANCEL )
    {
        // Do Cancel processing
    }



More information about the users mailing list