⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 main.cc

📁 initial working phase of the design of said editor, featuring multicasting, advanced linux keyboard
💻 CC
字号:
#include "program.h"
#include "thread.h"

program puck;

int init_mcast_socket();

#include <netinet/ip.h>			// iphdr
#include <netinet/udp.h>		// udphdr
int done = 0;
char inbuffer[ MMSIZE ];
//u_int input;
//bool pending = false;

#include <netinet/in.h>			// sockaddr_in
#include <strings.h>			// bzero
#include <string.h>			// strlen, memcpy
#include <stdlib.h>			// strtol
#include <unistd.h>			// read
#include <stdio.h> // printf, stdin

int ms; // multicast socket
queue* q;
struct sockaddr_in to;
u_int id;

void io( int signal )
{
   fd_set inputs;
   FD_ZERO( &inputs );
   FD_SET( fileno( stdin ), &inputs );
   FD_SET( ms, &inputs );

   int count = 0;
   struct timeval time_zero = { 0, 0 };
   count = select( FD_SETSIZE, &inputs, NULL, NULL, &time_zero );
   while( count != 0 )
   {
      int rc;
      if( FD_ISSET( fileno( stdin ), &inputs ) )
      {
	 char inbuf;
	 for(;;)
	 {
	    rc = read( fileno( stdin ), &inbuf, 1 );

//	    lick = inbuf; // overloaded
	    byte* state = new byte[ 1 + sizeof( timeval ) ];
	    if( rc <= 0 ) break;

	    *state = inbuf;

	    if( inbuf == 1 ) // esc
	    {
	       if( done == 1 ) // stuck in a loop somewhere
	       {
		  puck.kbd_reset();
		  printf( "* loop broken *\n" );
		  fflush( stdout );
		  exit( 0 );
	       }
	       done = 1;
	    }
	    struct timezone tz;
	    gettimeofday( ( timeval* )( state + 1 ), &tz );

	    puck.que -> strokes++;
//	    printf( "> %d\r\n", puck.que -> strokes );
	    spawn( &vector::key, state );

	    break;
	 }
      }
      if( FD_ISSET( ms, &inputs ) )
      {
	 int was = q -> locked++; // semaphore on the multicast channel
	 u_int length = sizeof( struct sockaddr_in );
	 struct sockaddr_in sock;
	 bzero( &sock, sizeof( sock ) );

	 int rc;

	 rc = recvfrom( ms, &inbuffer, MMSIZE, 0,
	    ( struct sockaddr* )&sock, &length ); // recieve this datagram

//   printf( "f<%d>", rc );
//   fflush( stdout );
	 inbuffer[ 19 ] = '\0';
	 int sender = strtol( inbuffer + 11, NULL, 0x10 );

	 // ignore what is coming while we have the conch
	 if( was > 0 || rc < 20 + 16 || ( sender == id && q -> clients > 1 ) )
	 {
	    FD_ZERO( &inputs );
	    FD_SET( fileno( stdin ), &inputs );
	    count = select( FD_SETSIZE, &inputs, NULL, NULL, &time_zero );
	    q -> locked--;
	    continue;
	 }
	 else if( inbuffer[ 0 ] == '*' )
	 {
	    inbuffer[ 10 ] = '\0';
	    length = strtol( inbuffer + 2, NULL, 0x10 );

	 int strings = *( int* )( inbuffer + 20 );
	 char** str = new char*[ strings ];
	 int index = 32;
	 int strng = 0;
	 for(; strng < strings; strng++ )
	 {
	    int len = *( int* )( inbuffer + index );
	    index += 4;
	    str[ strng ] = new char[ len ];
	    memcpy( str[ strng ], inbuffer + index, len );
	    index += len;
	 }

//   printf( "s" );
//   fflush( stdout );

	    q -> signal( inbuffer + index, length, sender
	    , *( timeval* )( inbuffer + 24 ) );

//   printf( "+" );
//   fflush( stdout );

	 }
	 else if( inbuffer[ 0 ] == ':' )
	 {
	 }
	 q -> locked--;
      }
      FD_ZERO( &inputs );
      FD_SET( fileno( stdin ), &inputs );
      FD_SET( ms, &inputs );

      count = select( FD_SETSIZE, &inputs, NULL, NULL, &time_zero );
  }
   FD_ZERO( &inputs );
}

void segv( int signal )
{
   puck.kbd_reset();
   printf( "* segmentation violation handled *\n" );
   fflush( stdout );
   _exit( 1 );
}

void term( int signal )
{
   puck.kbd_reset();
   printf( "* terminated *\n" );
   fflush( stdout );
   _exit( 1 );
   //   exit( 1 );
}

#include <fcntl.h>			// fcntl
#include <unistd.h>			// getpid
#include <signal.h>			// signal, SIGSEGV, SIGTERM

volatile thread* top_thread = ( thread* )NULL;

int main( int argc, char** argv )
{
   signal( SIGSEGV, segv );
   signal( SIGTERM, term );
   signal( SIGABRT, term );

   if( argc < 2 )
   {
      printf( "usage: %s <id#>\r\n", argv[ 0 ] );
      exit( 1 );
   }
   id = strtol( argv[ 1 ], NULL, 10 );

   signal( SIGIO, io );

   fcntl( fileno( stdin ), F_SETFL,
      fcntl( fileno( stdin ), F_GETFL, 0 ) | O_ASYNC | O_NONBLOCK );
   fcntl( fileno( stdin ), F_SETOWN, getpid() );

   init_mcast_socket();
   puck.que = q = new queue( &puck.pad, ms, to, id );

   puck.pad.clear();
#if 0
   puck.pad.debug();
#endif // debug

//   while( !done );
   while( !done ) //puck.exit == -1 )
   {
      if( top_thread == ( thread* )NULL )
         continue;

//      printf( "<" );
//      fflush( stdout );

      thread* here = ( thread* )top_thread;

      while( here == BUSY_THREAD )

         here = ( thread* )top_thread;

      top_thread = here -> next;

//      printf( "x" );
//      fflush( stdout );

//      printf( "*" );
//      fflush( stdout );

      puck.call( here -> addr, here -> state );

//      delete here; // this postpones failure a bit
//      printf( "*\r\n" );
//      fflush( stdout );

//      printf( ">" );
//      fflush( stdout );
   }
   delete q;

   return 0;
//   return puck.exit;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -