/* -TAG:LICENSE-
   
   This file is under the DigitalSpark Freeware Class License, see the
   file LICENSE for complete licensing information.
                                                         
                                                         -TAG:LICENSE- */
#include <Threading.h>
#include "tricky.h"   

extern	int	ClientFilter( char *data );
extern	int	ServerFilter( char *data );
extern	size_t	Write( int slot, void *data, size_t size );

/* Bridges between the client and server sockets */

void *ServerBridge( void )
{
char	sData[1024];
char	Buffer[10];
int	cDis=0,sDis=0;
int	sPos=0;

while ( 1 )
   {
   cDis = ClientConnected;
   sDis = ServerConnected;

   if ( ServerConnected == 1 )
      {
      if ( read(ServerSock,&Buffer,1) != 1 )
         ServerConnected = 0;
      sData[sPos] = Buffer[0];
      sPos++;
      if ( Buffer[0] == 0x0a )
         {
         sData[sPos] = 0;
         if ( ServerFilter(sData) == 0 )
            {
            if ( Authorized == 1 )
               {
               //Error(0,sData);
               cBusBroadcast(sData,0);
               }
            }
         sPos=0;
         }
      }
   if ( ServerConnected == 0 )
      usleep(EnforcedWait*10);

   if ( sDis > ServerConnected )
      {
      ServerAuth = 0;
      Error(0,"Server Disconnected");
      }

   }

return 0;
}
