void main(void)
{
  int   Time_Gap=0,Clock_Time,Clock_Time_last,i;
  int   TimeS = 0;
  char  key=0; 
  image area;
  image cur_img,bkg_img,ept_img; 
  image cur_middlewin_img,bkg_middlewin_img,ert_smallwin_img;
  
  int x_lable = 0,y_lable = 0;
  //int x_lable_ = 0,y_lable = 0;
  
  int X0,Y0;
  int x_ofs,y_ofs;
  
  int wind_flag = 0;

  //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  unsigned char *linebuffer;
  int sendbuf[5];
  sockaddr_in    laddr, raddr;
  unsigned       sock, listensock;
  unsigned       error;
  uint_16        rlen;
  int result;
  
  int ms1,kms;
  
  char  receivedata[RECEIVE_BYTES]; 
  //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

  init_licence("T2237AE5748"); 
  // ====================================================================================
  // ====================================================================================
  mem_init();
  // -----------------------------------------
  //开始程序前,拍下空的背景图片,并调节亮度
  Brightness_Adjust();
  //====================================================================================
  OvlClearAll;
  
  print("Press any key to start\n"); 
  print("Press 'q' to quit\n"); 
  print("Press 'c' to clear screen\n");  
  getchar();
  
  //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
   /* server services port */
   laddr.sin_family      = AF_INET;
   laddr.sin_port        = SERVER_PORT;   /* server listen at port SERVER_PORT (2002) */
   laddr.sin_addr.s_addr = INADDR_ANY;    /* every IP addr can connect to the camera */


   /* use TCP/IP protocoll and search for a free port */
   sock = socket_stream();

   if (sock == VCRT_HANDLE_ERROR) 
     {
     print("\nCreate stream socket failed");
     return;
     } 

   /* connection between free port and selected SERVER_PORT. Now SERVER_PORT is free again for new connections. */
   error = bind(sock, &laddr, sizeof(laddr));

   if (error != VCRT_OK) 
     {
     print("\nStream bind failed - 0x%x", error);
     return;
     }

   /* activate sock for TCP/IP connections */
   error = listen(sock, 0);

   if (error != VCRT_OK) 
     {
     print("\nListen failed - 0x%x", error);
     return;
     }

   listensock = sock;

   print("\n\nFast Ethernet port %d\n",laddr.sin_port);
  //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  while(1)
  {
  	
	    
  	  /* Are there connections at any port? The number 0 means: Wait until connection */
      sock = VCRT_selectall(0);

      /* Is there one who wants to connect to port SERVER_PORT */
      if (sock == listensock)
      {
        /* Connection requested; accept it */
        rlen = sizeof(raddr);

        /* accept connection */
        print("\nwait for accept");
        sock = accept(listensock, &raddr, &rlen);
        if (sock == VCRT_HANDLE_ERROR)
        {
          print("\nAccept failed, error 0x%x",VCRT_geterror(listensock));
          continue;
        }
        else
        {
          print("\naccept ready");
        }

        print("\nConnection from %d.%d.%d.%d, port %d",
           (raddr.sin_addr.s_addr >> 24) & 0xFF,
           (raddr.sin_addr.s_addr >> 16) & 0xFF,
           (raddr.sin_addr.s_addr >>  8) & 0xFF,
            raddr.sin_addr.s_addr        & 0xFF,
            raddr.sin_port);
            
      do
	  {
	    if (kbhit())
	    {
	       key = rs232rcv();
	       if(key == 'c')OvlClearAll;
	    }
		
		sendbuf[0] = 0;
		sendbuf[1] = 0;
		sendbuf[2] = 0;
		sendbuf[3] = 0;
		sendbuf[4] = 0;
		
		recv(sock, (char *)receivedata, RECEIVE_BYTES, 0); 
		
	    /* measure time */	
	    /* take next Screen (mode=0) */
	    ScreenBuffer(NextScreen,&area);
	    
	//	ms1=Clock_Time;
	    Clock_Time = getvar(MSEC);
		Time_Gap   = Clock_Time - Clock_Time_last;
	    if(Clock_Time<0)Clock_Time += 1000;
	    Clock_Time_last = Clock_Time;
	/*    
	    if(Clock_Time<ms1)
		{
			kms=ms1/1000;
			if((Clock_Time+kms*1000)>ms1)
				Clock_Time+=kms*1000;
			else 
				Clock_Time+=(kms+1)*1000;
		}  
		
	*/	
	 //   print("Clock_Time = %d\n",Clock_Time);      
		//wind_flag = 0;
	    ImageAssign(&cur_img, (long)Screen[Cur_index]		, BigWinColumns, BigWinRows, ScrGetPitch);
		ImageAssign(&bkg_img, (long)Screen[BackGrand_index]	, BigWinColumns, BigWinRows, ScrGetPitch);
		ImageAssign(&ept_img, (long)EptBckPage          	, BigWinColumns, BigWinRows, ScrGetPitch);	
		
	#ifdef _USE_SMALL_WINDOW_
	    if(wind_flag == 1 )//上次找到了乒乓球
	    {
	    	//预测新的窗口位置
	    	x_ofs = x_lable_his[his_near_pos] - x_lable_his[his_far_pos];
	    	if(x_ofs > dx_win_max)x_ofs = dx_win_max; 
	    	else if( x_ofs < -dx_win_max)x_ofs = - dx_win_max;
	    	y_ofs = y_lable_his[his_near_pos] - y_lable_his[his_far_pos];
	    	if(y_ofs > dx_win_max)y_ofs = dx_win_max; 
	    	else if( y_ofs < -dx_win_max)y_ofs = - dx_win_max;
	    	
	    	X0 = x_lable + x_ofs - MiddleWinColumns/2;
			Y0 = y_lable + y_ofs - MiddleWinRows/2;
			if(X0<0)X0=0;
			else if(X0 + MiddleWinColumns >= BigWinColumns)X0=BigWinColumns-1-MiddleWinColumns;
			if(Y0<0)Y0=0;
			else if(Y0 + MiddleWinRows >= BigWinRows)Y0 = BigWinRows-1-MiddleWinRows;
	    	
	    	//=============================================================== 		
			ImageAssign(&cur_middlewin_img, (cur_img.st + X0 + Y0 * ScrGetPitch), MiddleWinColumns, MiddleWinRows, ScrGetPitch);
			ImageAssign(&bkg_middlewin_img, (bkg_img.st + X0 + Y0 * ScrGetPitch), MiddleWinColumns, MiddleWinRows, ScrGetPitch);				
			ImageAssign(&ert_smallwin_img , (ept_img.st + X0 + Y0 * ScrGetPitch), MiddleWinColumns, MiddleWinRows, ScrGetPitch); 
			if( 1 == Find_pp_pos(cur_middlewin_img,bkg_middlewin_img,ert_smallwin_img,&x_lable,&y_lable,DCUR_TH) )
			{	
					wind_flag = 1;
					x_lable += X0;
					y_lable += Y0;
					upload_lable_his(x_lable,y_lable);
					mark(x_lable,y_lable,"*");
					
					sendbuf[0] = 1;
					sendbuf[1] = x_lable*10;
					sendbuf[2] = y_lable*10;
					sendbuf[3] = Clock_Time;
					sendbuf[4] = TimeS;	 	 
			}
			else wind_flag = 0;
		/*	else 
			{
				x_lable = x_lable_his[his_near_pos];
				y_lable = y_lable_his[his_near_pos];
				wind_flag = GSP(cur_img,&x_lable,&y_lable);
				if(wind_flag)
				{
					mark(x_lable,y_lable,"*");
					upload_lable_his(x_lable,y_lable);
				}
				
				sendbuf[0] = 1;
				sendbuf[1] = x_lable;
				sendbuf[2] = y_lable;
				sendbuf[3] = 2;
				sendbuf[4] = 3;	 				
		//		else 
		//		mark(x_lable,y_lable,"O");
			}*/
				/* calculation time */
		}
		if( wind_flag == 0 )//上次没找到乒乓球,或者这次在小窗口和中等窗口都没找到乒乓球,则在大窗口找
	#endif//_USE_SMALL_WINDOW_
		{
			//-------------------------------------------------------------------
			wind_flag = Find_pp_pos(cur_img,bkg_img,ept_img,&x_lable,&y_lable,dimg_threshold);
	
			//这样赋值以确保x_ofs和y_ofs为0
			x_lable_his[his_near_pos] =  x_lable;
			y_lable_his[his_near_pos] =  y_lable;
			x_lable_his[his_far_pos ] =  x_lable;
			y_lable_his[his_far_pos ] =  y_lable;
		//	mark(x_lable,y_lable,"s");
		}
		
		sendbuf[3] = Time_Gap;
		linebuffer = (unsigned char*)sendbuf;
		result=send(sock, (char *)linebuffer, 20, 0);
        if(result==VCRT_ERROR) 
        {
          print("\nSend VCRT_ERROR=%lx",VCRT_geterror(sock));
        }
		              
	  }while(key != 'q');
	   shutdown(sock, FLAG_CLOSE_TX);
	   print("\nshutdown\n");
  }
  print("\nidle\n");
  shutdown(listensock, FLAG_CLOSE_TX);
  break;
}
Exemplo n.º 2
0
bool CTCPSocket::Send(const void *pData, int nBytes)
{
	if (m_socket == -1 || m_bListening)
		return false;
	
#ifdef WIN32
	SOCKET sock = (SOCKET)m_socket;
	int size = 0;
	unsigned char *ptr = (unsigned char*) pData;
	while (true)
	{
		int s = send(sock, (const char *) ptr, nBytes, 0);
		if (s == SOCKET_ERROR)
		{
			int errnum = WSAGetLastError();
			if (errnum == WSAEWOULDBLOCK)
				continue;
			
			return false;
		}
		
		size += s;
		ptr += s;
		nBytes -= s;
		if (nBytes == 0)
			break;
		
	}

#elif defined(__TI_COMPILER_VERSION__)
	
	char *ptr = (char *) pData;
	while (nBytes>0)
	{
		// as OPT_SEND_NOWAIT is set above, this blocks until data is buffered
		// but does not wait for data to be send and acknowledged.
		// Send() therefore blocks until all data is passed on to the OS.
		int_32 count = send(m_socket, ptr, nBytes, 0);

		if (count==VCRT_ERROR)
		{
			printf("Error in Send(): send failed, error code 0x%x\n", VCRT_geterror(m_socket));
			return false;
		}
		
		ptr += count;
		nBytes -= count;
	}

#else
	int size = 0;
	unsigned char *ptr = (unsigned char *) pData;
	while (true)
	{
		int s = write(m_socket, ptr, nBytes);
		if (s == -1)
		{
			int errnum = errno;
			if (errnum == EAGAIN)
				continue;
			
			return false;
		}
		
		size += s;
		ptr += s;
		nBytes -= s;
		if (nBytes == 0)
			break;
		
	}
#endif
	
	return true;
}
Exemplo n.º 3
0
int CTCPSocket::Recv(void *pData, int nMaxBytes, bool bWait)
{
	if (m_socket == -1 || m_bListening)
		return -1;
	
#ifdef WIN32
	SOCKET sock = (SOCKET) m_socket;

	if (bWait)
	{
		int size = 0;
		unsigned char *ptr = (unsigned char*) pData;
		while (true)
		{
			int s = recv(sock, (char *) ptr, nMaxBytes, 0);
			if (s == SOCKET_ERROR)
			{
				int errnum = WSAGetLastError();
				if (errnum == WSAEWOULDBLOCK)
					continue;
			
				// this is really bad, the connection is probably dead
				return -1;
			}
			
			ptr += s;
			size += s;
			nMaxBytes -= s; 
			
			if (nMaxBytes == 0)
				return size;
				
		}
	}
	else
	{
		int size = recv(sock, (char *) pData, nMaxBytes, 0);
		if (size == SOCKET_ERROR)
		{
			int errnum = WSAGetLastError();
			if (errnum == WSAEWOULDBLOCK)
				return 0;
			
			// this is really bad, the connection is probably dead
		
			return -1;
		}
		else if (size == 0)
		{
			return -1;
		}
		
		return size;
	}

#elif defined(__TI_COMPILER_VERSION__)
	//set OPT_RECV_NOWAIT depending on bWait
	// if bWait==false: behaves like a non-blocking socket on other platforms
	// else: recv() blocks, avoiding polling with busy waiting.
	//       recv() may non the less return early in case of a PUSH flag.
	uint_32 opt_value = bWait? FALSE : TRUE;
	uint_32 error = setsockopt(m_socket, SOL_TCP, OPT_RECEIVE_NOWAIT, &opt_value, sizeof(opt_value));
	if (error != VCRT_OK)
	{
		printf("Error in Recv(), setsockopt(OPT_RECEIVE_NOWAIT) failed with error code 0x%x\n", error);
		return -1;
	}

	if (bWait)
	{
		int size = 0;
		char *ptr = (char *) pData;
		while (nMaxBytes>0)
		{
			int_32 count = recv(m_socket, ptr, nMaxBytes, 0); //is set to blocking above
			
			if (count == VCRT_ERROR)
			{
				printf("Error in Recv(): recv failed, error code 0x%x\n", VCRT_geterror(m_socket));
				// this is really bad, the connection is probably dead
				return -1;
			}
			
			ptr += count;
			size += count;
			nMaxBytes -= count; 
		}
		return size;
	}
	else
	{
		//receive whatever data is available and return immediately
		int_32 count = recv(m_socket, (char*)pData, nMaxBytes, 0); //is set to non-blocking above

		if (count == VCRT_ERROR)
		{
			printf("Error in Recv(): recv failed, error code 0x%x\n", VCRT_geterror(m_socket));
			// this is really bad, the connection is probably dead
			return -1;
		}
		// on vcrt, count==0 means zero bytes received and not connection closed by peer.
		// so in this case 0 is returned and not -1.
		
		return count;
	}

#else
	if (bWait)
	{
		int size = 0;
		unsigned char *ptr = (unsigned char *) pData;
		while (true)
		{
			int s = (int) recv(m_socket, ptr, nMaxBytes, 0);
			if (s == -1)
			{
				int errnum = errno;
				if (errnum == EAGAIN)
					continue;
			
				// this is really bad, the connection is probably dead
		
				return -1;
			}
			
			ptr += s;
			size += s;
			nMaxBytes -= s; 
			
			if (nMaxBytes == 0)
				return size;
				
		}
	}
	else
	{
		int size = (int) recv(m_socket, pData, nMaxBytes, 0);
		if (size == -1)
		{
			int errnum = errno;
			if (errnum == EAGAIN)
				return 0;
			
			// this is really bad, the connection is probably dead
		
			return -1;
		}
		else if (size == 0)
		{
			return -1;
		}
		
		return size;
	}
#endif
	
	return -1;
}
Exemplo n.º 4
0
CTCPSocket* CTCPSocket::Accept(unsigned char *ip)
{
	if (m_socket == -1 || !m_bListening)
		return NULL;
		
#ifdef WIN32
	SOCKET sock = (SOCKET)m_socket;
	struct sockaddr_in cli_addr;
	int clilen = sizeof(cli_addr);

	SOCKET new_socket = accept(sock, (struct sockaddr *) &cli_addr, &clilen);
	if (new_socket == INVALID_SOCKET)
	{
		return NULL;
	}

	// set non blocking
	u_long v = 1;
	if (ioctlsocket(new_socket, FIONBIO, &v) == SOCKET_ERROR)
	{
		printf("setting non-blocking option failed\n");
		Close();
		return false;
	}
	
	// set no delay for sending
	int v2 = 1;
	if (setsockopt(new_socket, IPPROTO_TCP, TCP_NODELAY, (const char*)&v2, sizeof(v2)) == -1)
	{
		printf("newly accepted socket couldn't be set to no delay mode\n");
	}

	if (ip != NULL)
	{
		memcpy(ip, (char *) &cli_addr.sin_addr.s_addr, 4);
	}
	
	CTCPSocket *s = new CTCPSocket();
	s->m_socket = (int) new_socket;

	return s;

#elif defined(__TI_COMPILER_VERSION__)
	uint_32 error;
	
	// acccept is always blocking on vcrt, so we need to test for activity first
	// which is not blocking
	error = VCRT_selectset(&m_socket, 1, (uint_32)-1);
	
	if (error==0) //no new connections, nothing to do
	{
		return NULL;
	}
	if (error==VCRT_SOCKET_ERROR) //an error occured
	{
		printf("Error in accept(): VCRT_selectset failed, error code 0x%x\n", VCRT_geterror(m_socket));
		return NULL;	
	}
	if (error!=m_socket) //should never happen as we check only this socket.
	{
		printf("Error in accept(): VCRT_selectset returned invalid socket");
		return NULL;	
	}
	

	sockaddr_in cli_addr;
	uint_16 clilen = sizeof(cli_addr);
	
	int new_socket = accept(m_socket, &cli_addr, &clilen);
	if (new_socket == VCRT_SOCKET_ERROR)
	{
		error = VCRT_geterror(m_socket);
		if (error == VCRT_OK)
		{
			printf("\nAccept: Connection reset by peer");
		}
		else
		{
			printf("Error, accept() failed with error code 0x%x\n", error);
		}
		return NULL;
	}
	
	
	
	// set non blocking

	// there is no socket-global (non-)blocking flag on VCRT
	// - accept() is always blocking and therefore guarded by VCRT_selectset().
	// - recv() behavior is set during Recv() depending on bWait flag
	// - send(): don't wait for data to be transmitted, return after buffering. this resembles behavior of Send() on other platforms
	uint_32 opt_value;
	
	opt_value = TRUE;
	error = setsockopt(m_socket, SOL_TCP, OPT_SEND_NOWAIT, &opt_value, sizeof(opt_value));
	if (error != VCRT_OK)
	{
		printf("Error, setsockopt(OPT_SEND_NOWAIT) failed with error code 0x%x\n", error);
	}
	
	
	
	// set no delay for sending
	// OPT_NO_NAGLE_ALGORITHM is the VCRT equivalent to TCP_NODELAY
	opt_value = TRUE;
	error = setsockopt(m_socket, SOL_TCP, OPT_NO_NAGLE_ALGORITHM, &opt_value, sizeof(opt_value));
	if (error != VCRT_OK)
	{
		printf("Error, setsockopt(OPT_NO_NAGLE_ALGORITHM) failed with error code 0x%x\n", error);
	}
	
	
	
	if (ip != NULL)
	{
		// vcrt expects inverse order of ip chars
		// than provided by RemoteApplicationHandler.cpp
		cli_addr.sin_addr.s_addr = revert_byte_order(cli_addr.sin_addr.s_addr);
		//never cast ip to an int pointer! this would imply 4byte alignment.
		memcpy( ip, &cli_addr.sin_addr.s_addr, 4);
	}
	
	CTCPSocket *sock = new CTCPSocket();
	sock->m_socket = new_socket;
	
	return sock;

#else
	struct sockaddr_in cli_addr;
	socklen_t clilen = sizeof(cli_addr);
	
	int new_socket = accept(m_socket, (struct sockaddr *) &cli_addr, &clilen);
	if (new_socket < 0)
	{
		return NULL;
	}
	
	// set non blocking
	if (fcntl(new_socket, F_SETFL, O_NONBLOCK) == -1)
	{
		printf("newly accepted socket couldn't be set to non blocking mode\n");
	}
	
	// set no delay for sending
	int v = 1;
	if (setsockopt(new_socket, IPPROTO_TCP, TCP_NODELAY, &v, sizeof(v)) == -1)
	{
		printf("newly accepted socket couldn't be set to no delay mode\n");
	} 
	
	if (ip != NULL)
	{
		bcopy((char *)&cli_addr.sin_addr.s_addr, ip, 4);
	}
	
	CTCPSocket *sock = new CTCPSocket();
	sock->m_socket = new_socket;
	
	return sock;
#endif
}