Ejemplo n.º 1
0
int CConnect::Write(const char* data, int len)
{  
	ssize_t size = write(pSocket_->Get_Fd(), data, len);
	if(size < 0)
	{  
		if(errno == EAGAIN || errno == EWOULDBLOCK || errno == EINTR)
		{
			size = 0;
			goto done;
		}
		else
			goto err;
	}
	else if(size < len) // add to out buff
	{  
		goto done;
	}
	return size;

err:
	Handle_Error();
	return size;
done:
	buffmanage_->IncreWriteBuff(data+size,static_cast<uint16_t>(len));
	SetWriting();
	return size;
	
}
Ejemplo n.º 2
0
int CConnect::Writev()
{  
	std::vector<DataBuff*>& vWrite = buffmanage_->GetWriteList();
	const int iov_count = vWrite.size();
	if(iov_count == 0)
	{
		if(IsWriting())
			DisWriting();
		return 0;
	}

	struct iovec iow[iov_count];
	for (int i = 0; i < iov_count; ++i)
	{
		iow[i].iov_base = vWrite[i]->data + vWrite[i]->cur;
		iow[i].iov_len  = vWrite[i]->len - vWrite[i]->cur;
	}

	ssize_t  cur_iov = 0;
	ssize_t nwritenum = 0;
	int ntotalwrite = buffmanage_->GetWritable();
	
	while(ntotalwrite > nwritenum)
	{  		
		ssize_t n = writev(pSocket_->Get_Fd(), &iow[cur_iov], iov_count - cur_iov);
		if(n > 0)
		{  
			nwritenum += n;
			while(static_cast<unsigned int>(n) >= iow[cur_iov].iov_len)
				n -= iow[cur_iov++].iov_len;
			iow[cur_iov].iov_base = (char*)iow[cur_iov].iov_base+n;
			iow[cur_iov].iov_len  -= n;
		}
		else if(n < 0)
		{  
			if(errno == EAGAIN || errno == EWOULDBLOCK)
				break;
			else if(errno == EINTR)
				continue;
			else
			{
				Handle_Error();
				break;
			}
		}
		else
		{  
			break;
		}
	}
	
	if(ntotalwrite >= nwritenum) // no data in send buff, cancal write event
		DisWriting();

	buffmanage_->ReduceWriteNum(nwritenum);
	return nwritenum;
}
Ejemplo n.º 3
0
void CConnect::SendMsg(CMessage* Msg)
{
	if(state_  != CON_CONNECTED)
	{
		CMessageAlloctor::FreeMSG(&Msg);
		return;
	}

//	Msg->PrintData();
	
	if(MsgList_.empty())
	{	
		int msglen = Msg->len_ - Msg->cur_;
		ssize_t size = write(pSocket_->Get_Fd(), Msg->data_+Msg->cur_, msglen);
		if(size >= msglen)
		{
			CMessageAlloctor::FreeMSG(&Msg);
			State_send_info(size);
			return;
		}
		else if(size >= 0)
		{
			Msg->cur_ += size;
			MsgList_.push_back(Msg);
			State_send_info(size);
			if(!IsWriting())
				SetWriting();
		}
		else
		{
			if(errno != EAGAIN && errno != EWOULDBLOCK && errno != EINTR)
				Handle_Error();
		}
	}
	else
	{
		MsgList_.push_back(Msg);
		if(!IsWriting())
			SetWriting();
	}
}
Ejemplo n.º 4
0
void CConnect::ProcessActiveEvent()
{  
	Print_recieve_info(EventLoop_->GetExpireTime());
	if((active_event_ & EPOLLHUP) && !(active_event_&EPOLLIN))
	{  
		Handle_Close();
	}
	
	if(active_event_ & (EPOLLERR))// | EPOLLVAL))
	{  
		printf("Handle_Error\n");
	 	Handle_Error();
	}

	if(active_event_ & (EPOLLIN | EPOLLRDHUP | EPOLLPRI))
	{  
		Handle_Read();
	}

	if(active_event_ & EPOLLOUT)
	{  
		Handle_Write();
	}
}
Ejemplo n.º 5
0
/*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
!
!.func                       close_frame_fast()
!
!.purp             closes a currently active 2D frame 
!.desc
! close_frame(frame)	
!
! IMAGE2D *frame;       image structure
!.ed
-------------------------------------------------------------------- */
int 
close_frame_fast(IMAGE2D *frame)			/* close active frame */
{
  char  errtext[132], filename[lg_name+1];
  int   stat, int_datatype;
  float cuts[4];
#ifdef IRAF
  int one=1;
#endif
#ifdef FITS
  fitsfile *fptr;
  int npix;
#endif

  strcpy(filename,frame->name);

  if (frame->iomode == (int)I_MODE) {
    switch (frame->data_format) {
#ifdef MIDAS
    case MIDAS_FORMAT :
      stat = SCFCLO(frame->imno);
      break;
#endif
#ifdef IRAF
    case IRAF_FORMAT :
    case STSDAS_FORMAT :
      uimclo(&(frame->imno),&stat);
      break;
#endif
#ifdef FITS
    case FITS_A_FORMAT :
    case FITS_B_FORMAT :
      stat =0;
      fptr = (fitsfile *)frame->external_info;
      fits_close_file(fptr,&stat);
      free_frame_mem(frame);
      frame->external_info = NULL;
      break;
#endif
    }
    if (stat) {
      sprintf(errtext,"close_frame: frame %s",filename);
      stat = get_tiger_errcode(frame->data_format,stat);
      Handle_Error(errtext,stat);
    }
    return(stat);
  }

  /*
  if (frame->data.d_data != NULL) {
    image_minmax(frame);

    cuts[0]=(float)frame->min; cuts[2]=(float)frame->min;
    cuts[1]=(float)frame->max; cuts[3]=(float)frame->max;
    stat = WR_desc(frame,"LHCUTS",FLOAT,4,cuts);
  }
  */

  WR_history(frame, (Anyfile *)0);

  switch (frame->data_format) {
#ifdef MIDAS
  case MIDAS_FORMAT :
    stat = SCFCLO(frame->imno);
    break;
#endif
#ifdef IRAF
  case IRAF_FORMAT :
  case STSDAS_FORMAT :
    switch(frame->data_type) {
    case SHORT :
      uips2s(&(frame->imno),&one,&(frame->nx),&one,&(frame->ny),
             frame->data.s_data,&stat);
      break;
    case INT :
    case LONG :
      uips2l(&(frame->imno),&one,&(frame->nx),&one,&(frame->ny),
             frame->data.l_data,&stat);
      break;
    case FLOAT :
      uips2r(&(frame->imno),&one,&(frame->nx),&one,&(frame->ny),
             frame->data.f_data,&stat);
      break;
    case DOUBLE :
      uips2d(&(frame->imno),&one,&(frame->nx),&one,&(frame->ny),
             frame->data.d_data,&stat);
      break;
    }
    if (stat == 0)  
      uimclo(&(frame->imno),&stat);
    free_frame_mem(frame);
    break;
#endif
#ifdef FITS
  case FITS_A_FORMAT :
  case FITS_B_FORMAT :
    stat = 0;
    fptr = (fitsfile *)frame->external_info;
    if (frame->iomode != (int)I_MODE) {
      if (frame->data.d_data != NULL) {
        int_datatype = get_datatype_code(OutputIO.basic_io,frame->data_type);
        npix = frame->nx*frame->ny;
        if (fits_write_img(fptr,int_datatype,1L,npix,
                           frame->data.s_data,&stat)) {
          stat = ERR_WRIT;
        }
      }
    }
    if (! stat) {
      fits_close_file(fptr,&stat);
      stat = wcs_free(frame);
    }
    free_frame_mem(frame);
    frame->external_info = NULL;
    break;
#endif
  }
  if (stat) {
    sprintf(errtext,"close_frame: frame %s",filename);
    stat = get_tiger_errcode(frame->data_format,stat);
    Handle_Error(errtext,stat);
  } else {
    if (TK && (frame->iomode == O_MODE || frame->iomode == IO_MODE))
      {
        printf("@ N {%s}\n",filename);
      }
  }

  return(stat);
}
Ejemplo n.º 6
0
/*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
!
!.func                        open_frame_fast()
!
!.purp          opens a 2D frame and updates the image structure items
!.desc
! open_frame(frame,name,mode)	
!
! IMAGE2D *frame;       image structure
! char *name;           frame name
! char *mode;           open mode (Input,Ouput,IO)
!.ed
-------------------------------------------------------------------- */
int 
open_frame_fast(IMAGE2D *frame, char *name, char *mode)		
{
  char errtext[132], filename[lg_name+1];
  int status, nbaxes, iomode, int_datatype;
  float cuts[4];
  int info[5];
#ifdef IRAF
  int two_dim=2;
  int len;
#endif
#ifdef FITS
  fitsfile *fptr;
  int nbread;
  int npix;
  int group = 0;
  double pixref;
#endif

  memset(frame->ident,' ',lg_ident);
  frame->ident[lg_ident] = '\0';
  memset(frame->cunit,' ',lg_unit);
  frame->cunit[lg_unit] = '\0';
  memset(frame->history,' ',lg_hist);
  frame->history[lg_hist] = '\0';
  frame->external_info = NULL;
  frame->file_type = T_IMA2D;
  frame->data_format = InputIO.basic_io;

  strcpy(filename,name);
  first_blk(filename); 
  strcpy(frame->name,filename);
  append_ima_extension(frame->name,InputIO.basic_io);

  strcpy(filename,frame->name);

  if (!exist(filename)) { /* check if fil exists */
    status = ERR_OPEN;
    sprintf(errtext,"open_frame: frame %s",filename);
    Handle_Error(errtext,status);
    return(status);
  }

  switch(mode[0]) {
  case 'I' : 
    if (mode[1] == 'O')
      frame->iomode = (int)IO_MODE;
    else
      frame->iomode = (int)I_MODE;
    break;
  case 'O' : frame->iomode = (int)O_MODE;
    break;
  default  : frame->iomode = (int)I_MODE;
    break;
  }
	
  iomode = get_iomode_code(InputIO.basic_io,frame->iomode);

  switch (InputIO.basic_io) {

#ifdef MIDAS
  case MIDAS_FORMAT :
    status = SCFINF(filename,2,info);  
    if (status == 0) {
      status = SCIGET(filename, info[1], iomode, F_IMA_TYPE, 2, 
                      &nbaxes, &(frame->nx), &(frame->startx), &(frame->stepx),
                      frame->ident, frame->cunit, (char **)(&(frame->data)), 
                      &(frame->imno));
      frame->data_type = info[1];
      frame->data_type = decode_datatype(InputIO.basic_io,frame->data_type);

      if (nbaxes!=2) /* We open a spectrum like an image, and that's not good */
        status = ERR_OPEN; 

    }
    break;
#endif
#ifdef IRAF
  case IRAF_FORMAT :
  case STSDAS_FORMAT :
    len = strlen(filename);
    uimopn(filename,&iomode,&(frame->imno),&status,len);
    if (status != 0) 
      break;
    uimgid(&(frame->imno),&int_datatype,&two_dim,&(frame->nx),&status);
    frame->data_type = decode_datatype(InputIO.basic_io,(short)(int_datatype));
    if (status != 0)
      break;
    alloc_frame_mem(frame, datatype);
    switch(frame->data_type) {
    case SHORT :
      uigs2s(&(frame->imno),&one,&(frame->nx),&one,&(frame->ny),
             frame->data.s_data,&status);
      break;
    case INT :
    case LONG :
      uigs2l(&(frame->imno),&one,&(frame->nx),&one,&(frame->ny),
             frame->data.l_data,&status);
      break;
    case FLOAT :
      uigs2r(&(frame->imno),&one,&(frame->nx),&one,&(frame->ny),
             frame->data.f_data,&status);
      break;
    case DOUBLE :
      uigs2d(&(frame->imno),&one,&(frame->nx),&one,&(frame->ny),
             frame->data.d_data,&status);
      break;
    }
    disable_user_warnings();
    RD_desc(frame,"IDENT",CHAR,lg_ident,frame->ident);
    restore_user_warnings();
    break;
#endif
#ifdef FITS
  case FITS_A_FORMAT :
  case FITS_B_FORMAT :
    status =0;
    if (fits_open_file(&fptr,filename,iomode,&status)) {
      status = ERR_ACCESS; break;
    }
    frame->external_info = (void *)fptr;
    if (fits_read_key(fptr, TINT,"NAXIS", &nbaxes,NULL, &status)) {
      status = ERR_READ; break;
    }
    if (nbaxes != 2) {
      status = ERR_IMA_HEAD; break;
    }
    if (fits_read_key(fptr, TINT, "NAXIS1",
                      &(frame->nx), NULL, &status)) {
      status = ERR_READ; break;
    }
    if (fits_read_key(fptr, TINT, "NAXIS2",
                      &(frame->ny), NULL, &status)) {
      status = ERR_READ; break;
    }
    if (status == 0) {
      pixref = 1.0;
      fits_read_key(fptr, TDOUBLE, "CRPIX1", &pixref, NULL, &status);
      if (status) { status = 0; pixref = 1; }
      fits_read_key(fptr, TDOUBLE, "CRVAL1", &(frame->startx), NULL, &status);
      if (status) { status = 0; frame->startx = (double)1; }
      fits_read_key(fptr, TDOUBLE, "CDELT1", &(frame->stepx), NULL, &status);
      if (status) { status = 0; frame->stepx = (double)1; }
      frame->startx -= (pixref-1)*frame->stepx;
      pixref = 1.0;
      fits_read_key(fptr, TDOUBLE, "CRPIX2", &pixref, NULL, &status);
      if (status) { status = 0; pixref = 1; }
      fits_read_key(fptr, TDOUBLE, "CRVAL2", &(frame->starty), NULL, &status);
      if (status) { status = 0; frame->starty = (double)1; }
      fits_read_key(fptr, TDOUBLE, "CDELT2", &(frame->stepy), NULL, &status);
      if (status) { status = 0; frame->stepy = (double)1; }
      frame->starty -= (pixref-1)*frame->stepy;
    }
    else
      break;

    int_datatype = (fptr->Fptr->tableptr)->tdatatype;
    frame->data_type = decode_datatype(InputIO.basic_io,(short)int_datatype);
    if (frame->data_type == SHORT) {
      if (fptr->Fptr->tableptr[1].tscale == 1 && fptr->Fptr->tableptr[1].tzero == 32768)
        /* unsigned short !!! */
        frame->data_type = LONG;
    }

    if (alloc_frame_mem(frame, frame->data_type) < 0) {
      fits_close_file(fptr,&status);
      status = ERR_ALLOC;
      break;
    }

    npix = frame->nx*frame->ny;
    switch (frame->data_type) {
    case SHORT :
      if (fits_read_img_sht(fptr,group,1L,npix,(short)0,
                            frame->data.s_data,&nbread,&status)) {
        status = ERR_READ; break;
      }
      break;
    case LONG :
    case INT :
      if (fits_read_img_lng(fptr,group,1L,npix,(int)0,
                            frame->data.l_data,&nbread,&status)) {
        status = ERR_READ; break;
      }
      break;
    case FLOAT :
      if (fits_read_img_flt(fptr,group,1L,npix,(float)0,
                            frame->data.f_data,&nbread,&status)) {
        status = ERR_READ; break;
      }
      break;
    case DOUBLE :
      if (fits_read_img_dbl(fptr,group,1L,npix,(double)0,
                            frame->data.d_data,&nbread,&status)) {
        status = ERR_READ; break;
      }
      break;
    }
    break;
#endif
  }

  if (status) {
    sprintf(errtext,"open_frame: frame %s",filename);
    status = get_tiger_errcode(frame->data_format,status);
    Handle_Error(errtext,status);
  }
  else {
    disable_user_warnings();
    status = RD_desc(frame,"LHCUTS",FLOAT,4,cuts);
    RD_desc(frame,"HISTORY",CHAR,lg_hist,frame->history);
    restore_user_warnings();

    frame->endx = frame->startx + (frame->nx -1)*frame->stepx;
    frame->endy = frame->starty + (frame->ny -1)*frame->stepy;
    if (status <= 0) {
      /* image_minmax is a really slow routine, and most of the time useless */
      frame->min = -ut_big_value;
      frame->max = +ut_big_value;
      /*			image_minmax(frame); */
    }
    else {
      frame->min = cuts[2];
      frame->max = cuts[3];
    }
    status = 0;
    /* parse wcs if contained in file */
    status = parse_wcs(frame);
  }
  return(status);
}
Ejemplo n.º 7
0
void CConnect::SendAllMsg()
{
	if(state_ != CON_CONNECTED)
		return;

	if(MsgList_.empty())
	{
		if(IsWriting())
			DisWriting();
	}
	else
	{
		int msgnum = MsgList_.size();
		const int iov_count = MIN(msgnum, (IOV_MAX));
	
		struct iovec iow[iov_count];
		std::list<CMessage*>::iterator it = MsgList_.begin();
		int i = 0;
		int ntotalwrite = 0;
		for(; it != MsgList_.end(); ++it)
		{
			CMessage *Msg = *it;
			int msglen = Msg->len_ - Msg->cur_;
			iow[i].iov_base = Msg->data_ + Msg->cur_;
			iow[i].iov_len  = msglen;
			ntotalwrite += msglen;
			i++;
			if(i >= iov_count)
				break;
		}
		
		ssize_t  cur_iov = 0;
		ssize_t nwritenum = 0;	
		while(ntotalwrite > nwritenum)
		{  		
			ssize_t n = writev(pSocket_->Get_Fd(), &iow[cur_iov], iov_count - cur_iov);
		//	printf("total = %d  writenum = %d  n = %d\n", ntotalwrite, nwritenum, n);
			if(n > 0)
			{  
				nwritenum += n;
				while(static_cast<unsigned int>(n) >= iow[cur_iov].iov_len)
				{
					n -= iow[cur_iov++].iov_len;
					it = MsgList_.begin();
					CMessageAlloctor::FreeMSG(&(*it));
					MsgList_.erase(it);
				}
				if(n > 0)
				{
					iow[cur_iov].iov_base = (char*)iow[cur_iov].iov_base+n;
					iow[cur_iov].iov_len  -= n;
					it = MsgList_.begin();
					(*it)->cur_ += n;
				}

				if(MsgList_.empty() && IsWriting())
					DisWriting();
			}
			else if(n < 0)
			{  
				if(errno == EAGAIN || errno == EWOULDBLOCK)
					break;
				else if(errno == EINTR)
					continue;
				else
				{
					Handle_Error();
					break;
				}
			}
			else
			{  
				break;
			}
		}
		
		State_send_info(nwritenum);
		// all pending msg has beeb send
/*		if(static_cast<unsigned int>(iov_count) >= MsgList_.size() && ntotalwrite >= nwritenum)
		{
			it = MsgList_.begin();
			for(; it != MsgList_.end(); ++it)
				CMessageAlloctor::FreeMSG(&(*it));
			MsgList_.clear();
			if(IsWriting())
				DisWriting();
		}
		else
		{
			for(it = MsgList_.begin(); it != MsgList_.end()&& ntotalwrite>0; )
			{
				CMessage *msg = *it;
				int msglen = msg->len_ - msg->cur_;
				if(msglen <= ntotalwrite)
				{
					CMessageAlloctor::FreeMSG(&msg);
					it = MsgList_.erase(it);
					ntotalwrite -= msglen;
				}
				else
				{
					msg->cur_ += ntotalwrite;
					ntotalwrite = 0;
				}
			}
			if(!IsWriting())
				SetWriting();
		}
*/	}
}
Ejemplo n.º 8
0
int CConnect::Readv()
{  
	int nReadNum = 0;
	while(1)
	{  
		DataBuff *buff = buffmanage_->GetReadBuff();
		assert(buff);

		char tempbuf[65535];
		struct iovec iovec[2];
		int bufflen = buff->max - buff->len;
		iovec[0].iov_base = buff->data + buff->len;
		iovec[0].iov_len  = bufflen;
		iovec[1].iov_base = tempbuf;
		iovec[1].iov_len  = sizeof(tempbuf);

		ssize_t n = readv(pSocket_->Get_Fd(), &iovec[0], 2);
		
		if(n > 0)
		{  
			nReadNum += n;
			if (n <= bufflen)
			{
				buff->len += n;
			}
			else 
			{
				buff->len += bufflen;
				int size = n - bufflen;
				int ncout = 0;
				while(ncout < size)  // read num greater than bufflen
				{  
					buff = buffmanage_->GetReadBuff();
					int min = MIN((buff->max-buff->len), size - ncout);
					memcpy(buff->data+buff->len, tempbuf+ncout, min);
					ncout += min;
					buff->len += min;
				}
			}

			if(static_cast<unsigned int>(n) < bufflen + sizeof(tempbuf))
				break;
		}
		else if( n == 0)
		{  
			Handle_Close();
			return n;
		}
		else
		{
			if(errno == EAGAIN || errno == EWOULDBLOCK)
				break;
			else if(errno == EINTR)
				continue;
			else
			{
				Handle_Error();
				return n;
			}
		}
	}
	buffmanage_->AddReadNum(nReadNum);
	State_recieve_info(nReadNum);
	while(1)
	{
		CMessage *Msg = NULL;
		int nParase = buffmanage_->ParasePacket(&Msg);
		if(nParase > 0 && Msg) 
		{
			Msg->fd_ = GetFd();
			EventLoop_->PushMsg(Msg);
		}
		else if(nParase == 0)	
		{
			break;
		}
		else
		{
			Handle_Close();
			return -1;
		}
	}	
	
	return nReadNum;
}