- Fix several bugs in select code and remove some dead code
authorCameron Gutman <aicommander@gmail.com>
Sat, 15 Aug 2009 02:09:23 +0000 (02:09 +0000)
committerCameron Gutman <aicommander@gmail.com>
Sat, 15 Aug 2009 02:09:23 +0000 (02:09 +0000)
 - Fix a typo
 - Part 1 of x

svn path=/trunk/; revision=42673

reactos/drivers/network/afd/afd/connect.c
reactos/drivers/network/afd/afd/listen.c
reactos/drivers/network/afd/afd/main.c
reactos/drivers/network/afd/afd/read.c
reactos/drivers/network/afd/afd/select.c
reactos/drivers/network/afd/afd/write.c
reactos/drivers/network/afd/include/afd.h

index d9baa6d..984ed49 100644 (file)
@@ -63,6 +63,9 @@ NTSTATUS MakeSocketIntoConnection( PAFD_FCB FCB ) {
 
    if( Status == STATUS_PENDING ) Status = STATUS_SUCCESS;
 
+   FCB->PollState |= AFD_EVENT_CONNECT | AFD_EVENT_SEND;
+   PollReeval( FCB->DeviceExt, FCB->FileObject );
+
    return Status;
 }
 
@@ -102,18 +105,13 @@ static NTSTATUS NTAPI StreamSocketConnectComplete
        return STATUS_FILE_CLOSED;
     }
 
-    if( NT_SUCCESS(Irp->IoStatus.Status) ) {
-       FCB->PollState |= AFD_EVENT_CONNECT | AFD_EVENT_SEND;
-       AFD_DbgPrint(MID_TRACE,("Going to connected state %d\n", FCB->State));
-       FCB->State = SOCKET_STATE_CONNECTED;
-    } else {
-       FCB->PollState |= AFD_EVENT_CONNECT_FAIL | AFD_EVENT_RECEIVE;
+    if( !NT_SUCCESS(Irp->IoStatus.Status) ) {
+       FCB->PollState |= AFD_EVENT_CONNECT_FAIL;
        AFD_DbgPrint(MID_TRACE,("Going to bound state\n"));
        FCB->State = SOCKET_STATE_BOUND;
+        PollReeval( FCB->DeviceExt, FCB->FileObject );
     }
 
-    PollReeval( FCB->DeviceExt, FCB->FileObject );
-
     /* Succeed pending irps on the FUNCTION_CONNECT list */
     while( !IsListEmpty( &FCB->PendingIrpList[FUNCTION_CONNECT] ) ) {
        NextIrpEntry = RemoveHeadList(&FCB->PendingIrpList[FUNCTION_CONNECT]);
index 8be5247..6aaad3a 100644 (file)
@@ -36,11 +36,6 @@ static NTSTATUS SatisfyAccept( PAFD_DEVICE_EXTENSION DeviceExt,
     else
        Status = MakeSocketIntoConnection( FCB );
 
-    if( NT_SUCCESS(Status) ) {
-       FCB->PollState |= AFD_EVENT_SEND;
-       PollReeval( DeviceExt, NewFileObject );
-    }
-
     return UnlockAndMaybeComplete( FCB, Status, Irp, 0 );
 }
 
@@ -192,9 +187,12 @@ static NTSTATUS NTAPI ListenComplete
     /* Trigger a select return if appropriate */
     if( !IsListEmpty( &FCB->PendingConnections ) ) {
        FCB->PollState |= AFD_EVENT_ACCEPT;
-       PollReeval( FCB->DeviceExt, FCB->FileObject );
+    } else {
+       FCB->PollState &= ~AFD_EVENT_ACCEPT;
     }
 
+    PollReeval( FCB->DeviceExt, FCB->FileObject );
+
     SocketStateUnlock( FCB );
 
     return Status;
@@ -296,7 +294,11 @@ NTSTATUS AfdWaitForListen( PDEVICE_OBJECT DeviceObject, PIRP Irp,
 
        AFD_DbgPrint(MID_TRACE,("Completed a wait for accept\n"));
 
-        FCB->PollState &= ~AFD_EVENT_ACCEPT;
+        if ( IsListEmpty( &FCB->PendingConnections ) )
+             FCB->PollState &= ~AFD_EVENT_ACCEPT;
+        else
+             FCB->PollState |= AFD_EVENT_ACCEPT;
+
         PollReeval( FCB->DeviceExt, FCB->FileObject );
 
        SocketStateUnlock( FCB );
@@ -322,8 +324,6 @@ NTSTATUS AfdAccept( PDEVICE_OBJECT DeviceObject, PIRP Irp,
 
     if( !SocketAcquireStateLock( FCB ) ) return LostSocket( Irp );
 
-    FCB->EventsFired &= ~AFD_EVENT_ACCEPT;
-
     if( FCB->NeedsNewListen ) {
        AFD_DbgPrint(MID_TRACE,("ADDRESSFILE: %x\n", FCB->AddressFile.Handle));
 
@@ -412,8 +412,11 @@ NTSTATUS AfdAccept( PDEVICE_OBJECT DeviceObject, PIRP Irp,
 
            if( IsListEmpty( &FCB->PendingConnections ) ) {
                FCB->PollState &= ~AFD_EVENT_ACCEPT;
-               PollReeval( FCB->DeviceExt, FCB->FileObject );
-           }
+           } else {
+               FCB->PollState |= AFD_EVENT_ACCEPT;
+            }
+
+           PollReeval( FCB->DeviceExt, FCB->FileObject );
 
            SocketStateUnlock( FCB );
            return Status;
index 3ab4983..55d1899 100644 (file)
@@ -300,6 +300,9 @@ AfdDisconnect(PDEVICE_OBJECT DeviceObject, PIRP Irp,
 
     if (ConnInfo) ExFreePool( ConnInfo );
 
+    FCB->PollState |= AFD_EVENT_DISCONNECT;
+    PollReeval( FCB->DeviceExt, FCB->FileObject );
+
     return UnlockAndMaybeComplete( FCB, Status, Irp, 0 );
 }
 
index 1c73a46..20ef822 100644 (file)
@@ -303,9 +303,6 @@ AfdConnectedSocketReadData(PDEVICE_OBJECT DeviceObject, PIRP Irp,
                                                                           Irp, 0 );
     }
 
-    FCB->EventsFired &= ~AFD_EVENT_RECEIVE;
-    PollReeval( FCB->DeviceExt, FCB->FileObject );
-
     if( !(RecvReq = LockRequest( Irp, IrpSp )) )
                return UnlockAndMaybeComplete( FCB, STATUS_NO_MEMORY,
                                                                           Irp, 0 );
@@ -332,7 +329,7 @@ AfdConnectedSocketReadData(PDEVICE_OBJECT DeviceObject, PIRP Irp,
 
     Status = ReceiveActivity( FCB, Irp );
 
-    if( Status == STATUS_PENDING && RecvReq->AfdFlags & AFD_IMMEDIATE ) {
+    if( Status == STATUS_PENDING && (RecvReq->AfdFlags & AFD_IMMEDIATE) ) {
         AFD_DbgPrint(MID_TRACE,("Nonblocking\n"));
         Status = STATUS_CANT_WAIT;
         TotalBytesCopied = 0;
@@ -595,8 +592,6 @@ AfdPacketSocketReadData(PDEVICE_OBJECT DeviceObject, PIRP Irp,
 
     if( !SocketAcquireStateLock( FCB ) ) return LostSocket( Irp );
 
-    FCB->EventsFired &= ~AFD_EVENT_RECEIVE;
-
     /* Check that the socket is bound */
     if( FCB->State != SOCKET_STATE_BOUND )
                return UnlockAndMaybeComplete
@@ -660,10 +655,12 @@ AfdPacketSocketReadData(PDEVICE_OBJECT DeviceObject, PIRP Irp,
     } else if( RecvReq->AfdFlags & AFD_IMMEDIATE ) {
                AFD_DbgPrint(MID_TRACE,("Nonblocking\n"));
                Status = STATUS_CANT_WAIT;
+               FCB->PollState &= ~AFD_EVENT_RECEIVE;
                PollReeval( FCB->DeviceExt, FCB->FileObject );
                UnlockBuffers( RecvReq->BufferArray, RecvReq->BufferCount, TRUE );
                return UnlockAndMaybeComplete( FCB, Status, Irp, 0 );
     } else {
+               FCB->PollState &= ~AFD_EVENT_RECEIVE;
                PollReeval( FCB->DeviceExt, FCB->FileObject );
                return LeaveIrpUntilLater( FCB, Irp, FUNCTION_RECV );
     }
index 010c816..02eb61a 100644 (file)
@@ -205,23 +205,16 @@ AfdSelect( PDEVICE_OBJECT DeviceObject, PIRP Irp,
            FileObject = (PFILE_OBJECT)AFD_HANDLES(PollReq)[i].Handle;
            FCB = FileObject->FsContext;
 
-           if( (FCB->PollState & AFD_EVENT_CLOSE) ||
-               (PollReq->Handles[i].Status & AFD_EVENT_CLOSE) ) {
-               PollReq->Handles[i].Events = 0;
-               PollReq->Handles[i].Status = AFD_EVENT_CLOSE;
-               Signalled++;
-           } else {
-                AFD_DbgPrint(MID_TRACE, ("AFD: Select Events: "));
-                PrintEvents( PollReq->Handles[i].Events );
-                AFD_DbgPrint(MID_TRACE,("\n"));
-
-               PollReq->Handles[i].Status =
+            AFD_DbgPrint(MID_TRACE, ("AFD: Select Events: "));
+            PrintEvents( PollReq->Handles[i].Events );
+            AFD_DbgPrint(MID_TRACE,("\n"));
+
+           PollReq->Handles[i].Status =
                    PollReq->Handles[i].Events & FCB->PollState;
-               if( PollReq->Handles[i].Status ) {
+           if( PollReq->Handles[i].Status ) {
                    AFD_DbgPrint(MID_TRACE,("Signalling %x with %x\n",
                                            FCB, FCB->PollState));
                    Signalled++;
-               }
            }
        }
 
@@ -286,7 +279,6 @@ AfdEventSelect( PDEVICE_OBJECT DeviceObject, PIRP Irp,
                            EventSelectInfo->EventObject,
                            EventSelectInfo->Events));
 
-    FCB->EventSelectTriggers = FCB->EventsFired = 0;
     if( FCB->EventSelect ) ObDereferenceObject( FCB->EventSelect );
     FCB->EventSelect = NULL;
 
@@ -303,8 +295,8 @@ AfdEventSelect( PDEVICE_OBJECT DeviceObject, PIRP Irp,
            FCB->EventSelect = NULL;
        else
            FCB->EventSelectTriggers = EventSelectInfo->Events;
-    } else /* Work done, cancelling select */
-       Status = STATUS_SUCCESS;
+    } else
+       Status = STATUS_INVALID_PARAMETER;
 
     AFD_DbgPrint(MID_TRACE,("Returning %x\n", Status));
 
@@ -370,7 +362,6 @@ VOID PollReeval( PAFD_DEVICE_EXTENSION DeviceExt, PFILE_OBJECT FileObject ) {
     PAFD_FCB FCB;
     KIRQL OldIrql;
     PAFD_POLL_INFO PollReq;
-    PKEVENT EventSelect = NULL;
 
     AFD_DbgPrint(MID_TRACE,("Called: DeviceExt %x FileObject %x\n",
                            DeviceExt, FileObject));
@@ -385,16 +376,6 @@ VOID PollReeval( PAFD_DEVICE_EXTENSION DeviceExt, PFILE_OBJECT FileObject ) {
        return;
     }
 
-    /* Not sure if i can do this at DISPATCH_LEVEL ... try it at passive */
-    AFD_DbgPrint(MID_TRACE,("Current State: %x, Events Fired: %x, "
-                           "Select Triggers %x\n",
-                           FCB->PollState, FCB->EventsFired,
-                           FCB->EventSelectTriggers));
-    if( FCB->PollState & ~FCB->EventsFired & FCB->EventSelectTriggers ) {
-       FCB->EventsFired |= FCB->PollState;
-       EventSelect = FCB->EventSelect;
-    }
-
     /* Now signal normal select irps */
     ThePollEnt = DeviceExt->Polls.Flink;
 
@@ -413,8 +394,10 @@ VOID PollReeval( PAFD_DEVICE_EXTENSION DeviceExt, PFILE_OBJECT FileObject ) {
 
     KeReleaseSpinLock( &DeviceExt->Lock, OldIrql );
 
-    AFD_DbgPrint(MID_TRACE,("Setting event %x\n", EventSelect));
-    if( EventSelect ) KeSetEvent( EventSelect, IO_NETWORK_INCREMENT, FALSE );
+    if( FCB->EventSelect && (FCB->PollState & FCB->EventSelectTriggers) ) {
+        AFD_DbgPrint(MID_TRACE,("Setting event %x\n", FCB->EventSelect));
+        KeSetEvent( FCB->EventSelect, IO_NETWORK_INCREMENT, FALSE );
+    }
 
     AFD_DbgPrint(MID_TRACE,("Leaving\n"));
 }
index f163c36..e7c5c36 100644 (file)
@@ -223,8 +223,6 @@ AfdConnectedSocketWriteData(PDEVICE_OBJECT DeviceObject, PIRP Irp,
 
     if( !SocketAcquireStateLock( FCB ) ) return LostSocket( Irp );
 
-    FCB->EventsFired &= ~AFD_EVENT_SEND;
-
     if( FCB->Flags & AFD_ENDPOINT_CONNECTIONLESS )
     {
         PAFD_SEND_INFO_UDP SendReq;
@@ -393,7 +391,6 @@ AfdPacketSocketWriteData(PDEVICE_OBJECT DeviceObject, PIRP Irp,
 
     if( !SocketAcquireStateLock( FCB ) ) return LostSocket( Irp );
 
-    FCB->EventsFired &= ~AFD_EVENT_SEND;
     FCB->PollState &= ~AFD_EVENT_SEND;
 
     PollReeval( FCB->DeviceExt, FCB->FileObject );
index fd707b9..df1f09d 100644 (file)
@@ -196,7 +196,6 @@ typedef struct _AFD_FCB {
     KEVENT StateLockedEvent;
     PKEVENT EventSelect;
     DWORD EventSelectTriggers;
-    DWORD EventsFired;
     UNICODE_STRING TdiDeviceName;
     PVOID Context;
     DWORD PollState;