/* -TAG:LICENSE-

   This file is under the DigitalSpark Freeware Class License, see the
   file LICENSE for complete licensing information.

                                                         -TAG:LICENSE- */

#define IN_CB
#include <Threading.h>
#include <Strings.h>
#include "tricky.h"

/* Removes a client from the bus

   Returns the return of the close() on the socket, or -1 if it couldn't
   find the socket
*/

int	cBusRemove( int Sock )
{
int	i=0, ret=0;

while ( i <= MAX_CLIENTS )
   {
   if ( Client[i].State != CS_FREE )
      {
      if ( Client[i].Sock == Sock )
         {
         pthread_mutex_lock(&Client[i].Mutex);
         if ( Client[i].State != CS_FREE )
            {
            shutdown(Sock,0);
            ret = close(Sock);
            Client[i].Sock = -1;
            Client[i].State = CS_FREE;
            //free(Client[i].Info);
            pthread_mutex_unlock(&Client[i].Mutex);
            cBus_Connected--;
            Error(0,"Client(%i) disconnected",i);
            return ret;
            }
         return 0;
         }
      }
   i++;
   }

return -1;
}
