/********************************************************************************************** errorType STLServerCommandOpenDevice::Read(const wxString &m_device) ;will have already read the Socket command type and the command id A polymorphic Read() ***********************************************************************************************/ errorType STLServerCommandOpenDeviceXXX::Read(wxSocketBase &sock) { wxInt16 length; errorType rv; //sock.SetFlags(wxSOCKET_WAITALL); rv=ReadFixedFields(sock); //reads qflag, at_tick if(rv) return rv; //reading a string, so first read its length, followed by the buffer //----read the data length in bytes----------------- //socket server will understand this format GetSock().Read(&length,sizeof(wxInt16)); if(GetSock().Error()) return(errFail); AppendToReadFromSocketString(length); //-----Read the data, a string in this instance ---- char *buff=DEBUG_NEW char[length]; GetSock().Read(buff,length); //Read the str m_DeviceStr=wxString(buff, length); AppendToReadFromSocketString(m_DeviceStr); delete buff; if(GetSock().Error()) return(errFail); SetReadFlag(true); return errNone; }
void Acceptor::HandleRead() { Sock listen_fd=GetSock(); Sock log_fd=accept(listen_fd,NULL,NULL); LogHandler *lg=new LogHandler(log_fd);//这里应该怎样处理呢??? 内存没有被释放掉。。?? std::cout<<"HandlerRead"<<endl; }
TError TNlLink::AddAddress(const TNlAddr &addr) { struct rtnl_addr *a = rtnl_addr_alloc(); if (!a) return TError(EError::Unknown, "Cannot allocate address"); rtnl_addr_set_link(a, Link); rtnl_addr_set_family(a, nl_addr_get_family(addr.Addr)); rtnl_addr_set_flags(a, IFA_F_NODAD); int ret = rtnl_addr_set_local(a, addr.Addr); if (ret < 0) { rtnl_addr_put(a); return Error(ret, "Cannot set local address"); } ret = rtnl_addr_add(GetSock(), a, 0); if (ret < 0) { rtnl_addr_put(a); return Error(ret, "Cannot add address"); } rtnl_addr_put(a); return TError::Success(); }
TError TNlLink::AddDirectRoute(const TNlAddr &addr) { struct rtnl_route *route; struct rtnl_nexthop *nh; int ret; route = rtnl_route_alloc(); if (!route) return TError(EError::Unknown, "Cannot allocate route"); ret = rtnl_route_set_dst(route, addr.Addr); if (ret < 0) { rtnl_route_put(route); return Error(ret, "Cannot set route destination"); } nh = rtnl_route_nh_alloc(); if (!route) { rtnl_route_put(route); return TError(EError::Unknown, "Cannot allocate next hop"); } rtnl_route_nh_set_ifindex(nh, GetIndex()); rtnl_route_add_nexthop(route, nh); Dump("add", route); ret = rtnl_route_add(GetSock(), route, NLM_F_CREATE | NLM_F_REPLACE); rtnl_route_put(route); if (ret < 0) return Error(ret, "Cannot add direct route"); return TError::Success(); }
TError TNlLink::Remove() { Dump("remove"); int ret = rtnl_link_delete(GetSock(), Link); if (ret) return Error(ret, "Cannot remove"); return TError::Success(); }
errorType STLServerCommandCloseDeviceXXX::Read(wxSocketBase &sock) { GSIDevHandle hDev; errorType rv; sock.SetFlags(wxSOCKET_WAITALL); rv=ReadFixedFields(sock); //reads qflag, at_tick if(rv) return rv; //reading a string, so first read its length, followed by the buffer //----read the data length in bytes----------------- //socket server will understand this format GetSock().Read(&hDev,sizeof(hDev)); if(GetSock().Error()) return(errFail); AppendToReadFromSocketString(hDev); SetDeviceHandle(hDev); SetReadFlag(true); return errNone; }
TError TNlLink::Load() { struct rtnl_link *link; int ret; ret = rtnl_link_get_kernel(GetSock(), rtnl_link_get_ifindex(Link), rtnl_link_get_name(Link), &link); if (ret) return Error(ret, "Cannot load link"); rtnl_link_put(Link); Link = link; return TError::Success(); }
TError TNlLink::ChangeNs(const std::string &newName, int nsFd) { auto change = rtnl_link_alloc(); if (!change) return Error(-NLE_NOMEM, "Cannot allocate link"); rtnl_link_set_name(change, newName.c_str()); rtnl_link_set_ns_fd(change, nsFd); Dump("change ns", change); int ret = rtnl_link_change(GetSock(), Link, change, 0); rtnl_link_put(change); if (ret < 0) return Error(ret, "Cannot change ns"); return TError::Success(); }
TError TNlLink::Enslave(const std::string &name) { struct rtnl_link *link; int ret; link = rtnl_link_alloc(); rtnl_link_set_name(link, name.c_str()); rtnl_link_set_master(link, GetIndex()); rtnl_link_set_flags(link, IFF_UP); Dump("mod", link); ret = rtnl_link_change(GetSock(), link, link, 0); if (ret < 0) { Dump("del", link); (void)rtnl_link_delete(GetSock(), link); rtnl_link_put(link); return Error(ret, "Cannot enslave interface " + name); } rtnl_link_put(link); return TError::Success(); }
TError TNlLink::Up() { Dump("up"); auto change = rtnl_link_alloc(); if (!change) return Error(-NLE_NOMEM, "Cannot allocate link"); rtnl_link_set_flags(change, IFF_UP); int ret = rtnl_link_change(GetSock(), Link, change, 0); rtnl_link_put(change); if (ret < 0) return Error(ret, "Cannot set up"); return TError::Success(); }
TError TNlLink::WaitAddress(int timeout_s) { struct nl_cache *cache; int ret; L() << "Wait for autoconf at " << GetDesc() << std::endl; ret = rtnl_addr_alloc_cache(GetSock(), &cache); if (ret < 0) return Nl->Error(ret, "Cannot allocate addr cache"); do { for (auto obj = nl_cache_get_first(cache); obj; obj = nl_cache_get_next(obj)) { auto addr = (struct rtnl_addr *)obj; if (!rtnl_addr_get_local(addr) || rtnl_addr_get_ifindex(addr) != GetIndex() || rtnl_addr_get_family(addr) != AF_INET6 || rtnl_addr_get_scope(addr) >= RT_SCOPE_LINK || (rtnl_addr_get_flags(addr) & (IFA_F_TENTATIVE | IFA_F_DEPRECATED))) continue; L() << "Got " << TNlAddr(rtnl_addr_get_local(addr)).Format() << " at " << GetDesc() << std::endl; nl_cache_free(cache); return TError::Success(); } usleep(1000000); ret = nl_cache_refill(GetSock(), cache); if (ret < 0) return Nl->Error(ret, "Cannot refill address cache"); } while (--timeout_s > 0); nl_cache_free(cache); return TError(EError::Unknown, "Network autoconf timeout"); }
TError TNlLink::AddVeth(const std::string &name, const std::string &hw, int mtu, int nsFd) { struct rtnl_link *veth, *peer; int ret; peer = rtnl_link_veth_alloc(); if (!peer) return TError(EError::Unknown, "Unable to allocate veth"); rtnl_link_set_name(peer, rtnl_link_get_name(Link)); veth = rtnl_link_veth_get_peer(peer); rtnl_link_set_name(veth, name.c_str()); if (nsFd >= 0) rtnl_link_set_ns_fd(veth, nsFd); if (mtu > 0) { rtnl_link_set_mtu(peer, mtu); rtnl_link_set_mtu(veth, mtu); } if (!hw.empty()) { TNlAddr addr; TError error = addr.Parse(AF_LLC, hw.c_str()); if (error) return error; rtnl_link_set_addr(veth, addr.Addr); } rtnl_link_set_flags(peer, IFF_UP); Dump("add", veth); rtnl_link_put(veth); Dump("add", peer); ret = rtnl_link_add(GetSock(), peer, NLM_F_CREATE | NLM_F_EXCL); if (ret < 0) { rtnl_link_put(peer); return Error(ret, "Cannot add veth"); } rtnl_link_put(peer); return Load(); }
int ReceiveFCNum(char *argv, int portnum) { int sock; // socket descriptor int bytesRcvd, totalBytesRcvd=0; // bytes read in single rcv() and total bytes read char errorString[255]; char sendBuffer[] = "3"; //CODE OF 3 ASKS SENDER FOR FCNUM char rcvbyte; // return arg char log_string[255]; sock = GetSock(argv, portnum); bytesRcvd = 0; totalBytesRcvd = 0; // tell server we're ready for the fcnum if(send(sock, sendBuffer, 1, 0) != 1) { p_log_errorno((char*)"send() from client to request data failed"); exit(0); } // wait for full block to be received while(totalBytesRcvd < 1) { if((bytesRcvd = recv(sock, &rcvbyte, 1, 0)) <= 0) { sprintf(errorString, "recv() failed or connection closed prematurely, %d bytes received", totalBytesRcvd); close(sock); p_log_errorno(errorString); exit(0); } totalBytesRcvd += bytesRcvd; } if(shutdown(sock, 2) < 0) { p_log_errorno((char*)"ERROR shutdown(sock)"); }//close(sock); return (int) rcvbyte; }
/************************************************************************* errorType STLServerCommandSetAmplitudeSPL::Read(wxSocketBase s&ock) will have already read the Socket command type and the command id now need to read the command specific data from the client. Should only be called from a GSISocket server or derived class Note that the corresponding ::Write function will be waiting for a ServerReturnRecord. This function does not return this record as it doesn't know if the task was completed without error. The calling code will need to perform the required hardware (or other) tasks and return the ServerReturnRecord indicating status of the function *************************************************************************/ errorType STLServerCommandSetAmplitudeSPLXXX::Read(wxSocketBase & sock) { //read amplitude wxInt16 amplitude; errorType rv; rv=ReadFixedFields(GetSock()); //reads qflag, at_tick sock.Read(&litude,sizeof(amplitude)); SetReadFlag(true); AppendToReadFromSocketString(amplitude); if(sock.Error()) { MapWxSocketError sockErr(sock.LastError()); SetLastError(sockErr.GetErrorType()); return(sockErr.GetErrorType()); } SetAmplitude(amplitude); FillGSIRecord(); //perform LUT conversion return errNone; }
TError TNlLink::SetDefaultGw(const TNlAddr &addr) { struct rtnl_route *route; struct rtnl_nexthop *nh; TError error; TNlAddr all; int ret; error = all.Parse(addr.Family(), "default"); if (error) return error; route = rtnl_route_alloc(); if (!route) return TError(EError::Unknown, "Unable to allocate route"); ret = rtnl_route_set_dst(route, all.Addr); if (ret < 0) { rtnl_route_put(route); return Error(ret, "Cannot set route destination"); } nh = rtnl_route_nh_alloc(); if (!route) { rtnl_route_put(route); return TError(EError::Unknown, "Unable to allocate next hop"); } rtnl_route_nh_set_gateway(nh, addr.Addr); rtnl_route_nh_set_ifindex(nh, GetIndex()); rtnl_route_add_nexthop(route, nh); Dump("add", route); ret = rtnl_route_add(GetSock(), route, NLM_F_MATCH); rtnl_route_put(route); if (ret < 0) return Error(ret, "Cannot set default gateway"); return TError::Success(); }
/*! errorType STLServerCommandAcquireXXX::Read() will have already read the Socket command type and the command id now need to read the command specific data from the client. Should only be called from a GSISocket server or derived class Note that the corresponding ::Write function will be waiting for a ServerReturnRecord. This function does not return this record as it doesn't know if the task was completed without error. The calling code will need to perform the required hardware (or other) tasks and return the ServerReturnRecord indicating status of the function Reads wxInt16 queuedFlag wxUint16 atTick wxInt16 channel *************************************************************************/ errorType STLServerCommandAcquireXXX::Read(wxSocketBase &sock) { //read amplitude wxUint16 numDataPoints; errorType rv; rv=ReadFixedFields(GetSock()); //reads qflag, at_tick sock.Read(&numDataPoints,sizeof(numDataPoints)); SetReadFlag(true); AppendToReadFromSocketString(numDataPoints); if(sock.Error()) { MapWxSocketError sockErr(sock.LastError()); SetLastError(sockErr.GetErrorType()); return(sockErr.GetErrorType()); } SetNumDataPoints(numDataPoints); FillGSIRecord(); AppendText("Read STLCommandAcquireXXX from client\n"); return errNone; }
TError TNl::OpenLinks(std::vector<std::shared_ptr<TNlLink>> &links, bool all) { struct nl_cache *cache; int ret; ret = rtnl_link_alloc_cache(GetSock(), AF_UNSPEC, &cache); if (ret < 0) return Error(ret, "Cannot allocate link cache"); for (auto obj = nl_cache_get_first(cache); obj; obj = nl_cache_get_next(obj)) { auto link = (struct rtnl_link *)obj; if (!all && ((rtnl_link_get_flags(link) & (IFF_LOOPBACK | IFF_RUNNING)) != IFF_RUNNING)) continue; auto l = std::make_shared<TNlLink>(shared_from_this(), link); links.push_back(l); } nl_cache_free(cache); return TError::Success(); }
void LogHandler::HandleRead() { char buffer[100]; Sock sock_fd=GetSock(); int i=read(sock_fd,buffer,100); }
SaErrorT cSession::DoRpc( uint32_t id, ClientRpcParams& iparams, ClientRpcParams& oparams ) { SaErrorT rv; cHpiMarshal * hm = HpiMarshalFind( id ); if ( !hm ) { return SA_ERR_HPI_UNSUPPORTED_API; } int cc; char data[dMaxPayloadLength]; uint32_t data_len; uint8_t rp_type; uint32_t rp_id; int rp_byte_order; cc = HpiMarshalRequest( hm, data, iparams.const_array ); if ( cc < 0 ) { return SA_ERR_HPI_INTERNAL_ERROR; } data_len = cc; bool rc = false; for ( size_t attempt = 0; attempt < RPC_ATTEMPTS; ++attempt ) { if ( attempt > 0 ) { DBG( "Session: RPC request %u, Attempt %u\n", id, (unsigned int)attempt ); } cClientStreamSock * sock; rv = GetSock( sock ); if ( rv != SA_OK ) { return rv; } rc = sock->WriteMsg( eMhMsg, id, data, data_len ); if ( rc ) { rc = sock->ReadMsg( rp_type, rp_id, data, data_len, rp_byte_order ); if ( rc ) { break; } } #if GLIB_CHECK_VERSION (2, 32, 0) wrap_g_static_private_set( &m_sockets, 0);// close socket #else wrap_g_static_private_set( &m_sockets, 0, 0 ); // close socket #endif g_usleep( NEXT_RPC_ATTEMPT_TIMEOUT ); } if ( !rc ) { return SA_ERR_HPI_NO_RESPONSE; } oparams.SetFirst( &rv ); cc = HpiDemarshalReply( rp_byte_order, hm, data, oparams.array ); if ( ( cc <= 0 ) || ( rp_type != eMhMsg ) || ( id != rp_id ) ) { //Closing main socket(the socket that was used for saHpiSessionOpen) // may disrupt HPI session on remote side. // TODO: Investigate and fix on OpenHPI daemon side and then uncomment. //g_static_private_set( &m_sockets, 0, 0 ); // close socket return SA_ERR_HPI_NO_RESPONSE; } return rv; }
/*! errorType STLServerCommandXXX::ReadFixedFields() Read: qflag at_tick */ errorType STLServerCommandXXX::ReadFixedFields(StartleRecord &r) { /*----------Read the STL QUEUED/IMMEDIATE Flag field--------------------*/ wxInt16 queuedFlag; wxUint16 atTick; wxString s; errorType rv=errNone; #if STL_DEBUG_USING_XXX queuedFlag; atTick; s; rv; r; #else wxString s1=""; GetSock().Read(&queuedFlag,sizeof (queuedFlag)); if(GetSock().Error()) { MapWxSocketError sockErr(GetSock().LastError()); return(sockErr.GetErrorCode()); } s1.Printf("%i ",queuedFlag); s.Printf("flag=%i: ",queuedFlag); GetStatusText()->AppendText(s); if( (queuedFlag != STL_QUEUED) && (queuedFlag != STL_IMMEDIATE) ) { GetStatusText()->AppendText("\n--- Flag error! ---\n"); rv=errParameter; } else { if(queuedFlag== STL_QUEUED) GetStatusText()->AppendText("Queued\n"); else GetStatusText()->AppendText("Immediate\n"); } /*-----------------------------------------------------*/ /*--------------Read the atTick field----------------*/ GetSock().Read(&atTick,sizeof(atTick)); if(GetSock().Error()) { MapWxSocketError sockErr(GetSock().LastError()); return(sockErr.GetErrorCode()); } s1=s1+atTick+" "; s.Printf("at tick=%i \n",atTick); GetStatusText()->AppendText(s); //----Read all fixed record elements--------------------------------------------- SetAtTick(atTick); SetQueuedFlag(static_cast<STLQueuedFlag>(queuedFlag)); AppendToReadFromSocketString(s1); #endif return rv; }
TError TNlLink::AddXVlan(const std::string &vlantype, const std::string &master, uint32_t type, const std::string &hw, int mtu) { TError error = TError::Success(); int ret; uint32_t masterIdx; struct nl_msg *msg; struct nlattr *linkinfo, *infodata; struct ifinfomsg ifi = { 0 }; struct ether_addr *ea = nullptr; auto Name = GetName(); if (hw.length()) { // FIXME THREADS ea = ether_aton(hw.c_str()); if (!ea) return TError(EError::Unknown, "Invalid " + vlantype + " mac address " + hw); } TNlLink masterLink(Nl, master); error = masterLink.Load(); if (error) return error; masterIdx = masterLink.GetIndex(); msg = nlmsg_alloc_simple(RTM_NEWLINK, NLM_F_CREATE); if (!msg) return TError(EError::Unknown, "Unable to add " + vlantype + ": no memory"); ret = nlmsg_append(msg, &ifi, sizeof(ifi), NLMSG_ALIGNTO); if (ret < 0) { error = TError(EError::Unknown, "Unable to add " + vlantype + ": " + nl_geterror(ret)); goto free_msg; } /* link configuration */ ret = nla_put(msg, IFLA_LINK, sizeof(uint32_t), &masterIdx); if (ret < 0) { error = TError(EError::Unknown, std::string("Unable to put IFLA_LINK: ") + nl_geterror(ret)); goto free_msg; } ret = nla_put(msg, IFLA_IFNAME, Name.length() + 1, Name.c_str()); if (ret < 0) { error = TError(EError::Unknown, std::string("Unable to put IFLA_IFNAME: ") + nl_geterror(ret)); goto free_msg; } if (mtu > 0) { ret = nla_put(msg, IFLA_MTU, sizeof(int), &mtu); if (ret < 0) { error = TError(EError::Unknown, std::string("Unable to put IFLA_MTU: ") + nl_geterror(ret)); goto free_msg; } } if (ea) { struct nl_addr *addr = nl_addr_build(AF_LLC, ea, ETH_ALEN); ret = nla_put(msg, IFLA_ADDRESS, nl_addr_get_len(addr), nl_addr_get_binary_addr(addr)); if (ret < 0) { error = TError(EError::Unknown, std::string("Unable to put IFLA_ADDRESS: ") + nl_geterror(ret)); goto free_msg; } nl_addr_put(addr); } /* link type */ linkinfo = nla_nest_start(msg, IFLA_LINKINFO); if (!linkinfo) { error = TError(EError::Unknown, "Unable to add " + vlantype + ": can't nest IFLA_LINKINFO"); goto free_msg; } ret = nla_put(msg, IFLA_INFO_KIND, vlantype.length() + 1, vlantype.c_str()); if (ret < 0) { error = TError(EError::Unknown, std::string("Unable to put IFLA_INFO_KIND: ") + nl_geterror(ret)); goto free_msg; } /* xvlan specific */ infodata = nla_nest_start(msg, IFLA_INFO_DATA); if (!infodata) { error = TError(EError::Unknown, "Unable to add " + vlantype + ": can't nest IFLA_INFO_DATA"); goto free_msg; } if (vlantype == "macvlan") { ret = nla_put(msg, IFLA_MACVLAN_MODE, sizeof(uint32_t), &type); if (ret < 0) { error = TError(EError::Unknown, std::string("Unable to put IFLA_MACVLAN_MODE: ") + nl_geterror(ret)); goto free_msg; } #ifdef IFLA_IPVLAN_MAX } else if (vlantype == "ipvlan") { uint16_t mode = type; ret = nla_put(msg, IFLA_IPVLAN_MODE, sizeof(uint16_t), &mode); if (ret < 0) { error = TError(EError::Unknown, std::string("Unable to put IFLA_IPVLAN_MODE: ") + nl_geterror(ret)); goto free_msg; } #endif } nla_nest_end(msg, infodata); nla_nest_end(msg, linkinfo); L() << "netlink: add " << vlantype << " " << Name << " master " << master << " type " << type << " hw " << hw << " mtu " << mtu << std::endl; ret = nl_send_sync(GetSock(), msg); if (ret) return Error(ret, "Cannot add " + vlantype); return Load(); free_msg: nlmsg_free(msg); return error; }
void ReceiveData(char *argv, int portnum, short unsigned int *reg_pix_list_xcols, short unsigned int *reg_pix_list_yrows, FILE *full_pixel_list, short unsigned int *beadvals, FILE *beadfile, FILE *sumfile, FILE *imgsumfile, FILE *reglogfile, int curr_fcnum) { int sock; // socket descriptor unsigned int data_size; // length of data block to receive int bytesRcvd, totalBytesRcvd=0; // bytes read in single rcv() and total bytes read int imagesRcvd=0; char errorString[255]; char log_string[255]; char sendBuffer[] = "18"; //CODE OF 1/8 ASKS SENDER FOR DATA short unsigned int *inputBuffer;// short unsigned int img_err_code; int i, j, k; int *offsetx_history, *offsety_history; clock_t time1, time2; int num_loops; int connection_isok=1; char command_buffer[255]; char image_dir[255]; sprintf(image_dir, "%s/polonator/G.007/acquisition/images", getenv("HOME")); sprintf(command_buffer, "mkdir -p %s", image_dir); system(command_buffer); #ifdef SAVE_FL_IMAGES char outputfilename[255]; FILE *imgfile; #endif sock = GetSock(argv, portnum); data_size = ((NUM_XCOLS * NUM_YROWS)+4) * sizeof(short unsigned int); if( (inputBuffer=(short unsigned int *)malloc(data_size)) == NULL) { p_log_errorno((char*)"malloc() failed"); } //ALLOCATE MEMORY FOR OFFSET HISTORY ARRAYS (USED BY PROCESSIMAGE_REGISTER) if((offsetx_history=(int*)malloc(OFFSET_HISTORY_LENGTH * sizeof(int))) == NULL) { p_log_errorno((char*)"malloc() offsetx_history failed"); exit(0); } if((offsety_history=(int*)malloc(OFFSET_HISTORY_LENGTH * sizeof(int))) == NULL) { p_log_errorno((char*)"malloc() offsety_history failed"); exit(0); } for(i=0; i < OFFSET_HISTORY_LENGTH; i++) { *(offsetx_history + i) = 0; *(offsety_history + i) = 0; } //TELL SENDER WE WANT TO RECEIVE DATA if(send(sock, sendBuffer, 1, 0)!=1) { if(errno = EPIPE) { p_log_errorno((char*)"ERROR:\tReceivData: send() failed because connection is broken"); connection_isok=0; } else { p_log_errorno((char*)"send() from client to request data failed"); exit(0); } } if(send(sock, sendBuffer+1, 1, 0) !=1 ) { if(errno = EPIPE) { p_log_errorno((char*)"ERROR:\tReceivData: send() failed because connection is broken"); connection_isok=0; } else { p_log_errorno((char*)"send() from client to request data failed"); exit(0); } } //CONSUME 'PRIMIMG' DATA SINCE FIRST IMAGE XFER //IS VERY SLOW if(connection_isok) { bytesRcvd=0; totalBytesRcvd=0; while(totalBytesRcvd < data_size) { if((bytesRcvd=recv(sock, ((char*)inputBuffer)+totalBytesRcvd, data_size-totalBytesRcvd, 0)) < 0) { sprintf(errorString, "recv() failed or connection closed prematurely, %d bytes received (%d)", totalBytesRcvd, bytesRcvd); close(sock); p_log_errorno(errorString); exit(0); } else if(bytesRcvd == 0) { p_log((char*)"ERROR:\tReceiveData: connection is broken"); connection_isok = 0; totalBytesRcvd=data_size; } totalBytesRcvd += bytesRcvd; } } #ifdef SAVE_FL_IMAGES sprintf(outputfilename, "mkdir -p %s/%s", image_dir, CYCLE_NAME); system(outputfilename); #endif // loop through all images, receiving blocks of data (1 image + header per block) if(connection_isok) { for(i=curr_fcnum; i < curr_fcnum + 1; i++) { for(j=0; j < ARRAYS_PER_FC; j++) { for(k=0; k < IMGS_PER_ARRAY; k++) { bytesRcvd = 0; totalBytesRcvd = 0; // tell server we're ready for the next block #ifdef DEBUG1 p_log((char*)"STATUS:\tReceiveData: request data from server"); p_log((char*)"STATUS:\tReceiveData: send first byte to request"); #endif if(send(sock, sendBuffer, 1, 0) != 1) { if(errno = EPIPE) { p_log_errorno((char*)"ERROR:\tsend() failed because connection is broken"); connection_isok=0; totalBytesRcvd=data_size; } else { p_log_errorno((char*)"ERROR:\tsend() from client to request data failed"); exit(0); } } #ifdef DEBUG1 p_log((char*)"STATUS:\tReceiveData: send second byte to request"); #endif if(send(sock, sendBuffer+1, 1, 0) !=1 ) { if(errno = EPIPE) { p_log_errorno((char*)"ERROR:\tsend() failed because connection is broken"); connection_isok = 0; totalBytesRcvd = data_size; } else { p_log_errorno((char*)"ERROR:\tsend() from client to request data failed"); exit(0); } } #ifdef DEBUG1 p_log((char*)"STATUS:\tReceiveData: start receiving data"); p_log((char*)"STATUS:\tReceiveData: wait for data to arrive"); #endif // wait for full block to be received while(totalBytesRcvd < data_size) { // was there an error during the recv? if so, this is bad; crash if((bytesRcvd = recv(sock, ((char*)inputBuffer) + totalBytesRcvd, data_size-totalBytesRcvd, 0)) < 0) { sprintf(errorString, "recv() failed, %d bytes received (%d)", totalBytesRcvd, bytesRcvd); close(sock); p_log_errorno(errorString); // do a graceful restart here connection_isok = 0; totalBytesRcvd = data_size; bytesRcvd = 0; } // was the connection broken? if so, the acq software was probably stopped prematurely; recover gracefully else if(bytesRcvd == 0) { sprintf(log_string, "connection to acq appears to be broken while trying to receive image %d %d %d, received %d bytes so far", i, j, k, totalBytesRcvd); p_log(log_string); connection_isok = 0; totalBytesRcvd = data_size; } /* #ifdef DEBUG1 sprintf(log_string, "STATUS:\tReceiveData: received %d bytes of image data", bytesRcvd); p_log(log_string); #endif */ totalBytesRcvd += bytesRcvd; num_loops++; } if(!connection_isok) { i = curr_fcnum + 1; j = ARRAYS_PER_FC; k = IMGS_PER_ARRAY; p_log((char*)"ERROR:\tReceiveData: connection was broken; exiting"); break; } p_log((char*)"STATUS:\tReceiveData: all data received for current image"); imagesRcvd++; num_loops = 0; // NOW THAT WE HAVE THE IMAGE, DO SOMETHING WITH IT #ifdef SAVE_FL_IMAGES if( (k % 100) == 0) { sprintf(outputfilename, "%s/%s/%02d_%04d.raw", image_dir, CYCLE_NAME, *(inputBuffer+1), *(inputBuffer+2)); sprintf(log_string, "STATUS:\tReceiveData: write received data to image file %s", outputfilename); p_log(log_string); imgfile = fopen(outputfilename, "w"); fwrite(inputBuffer+4, sizeof(short unsigned int), 1000000, imgfile); fclose(imgfile); } #endif #ifdef DEBUG1 sprintf(log_string, "STATUS:\tReceiveData: calling ProcessImage for frame %d %d %d, expecting %d %d %d", *(inputBuffer), *(inputBuffer+1), *(inputBuffer+2), i, j, k); p_log(log_string); #endif ProcessImage(reg_pix_list_xcols, reg_pix_list_yrows, full_pixel_list, inputBuffer + 4, inputBuffer, beadvals, beadfile, sumfile, imgsumfile, reglogfile, offsetx_history, offsety_history); } // end for k } // end for j } // end for i } // end if connection_isok // tell acq we're finished receiving all data (we don't want it to close the connection // before the processor finishes receiving the last image) if(connection_isok) { if(send(sock, sendBuffer, 1, 0) != 1) { p_log_errorno((char*)"send() from client to signal all data received failed"); //exit(0); } if(shutdown(sock, 2) < 0) { p_log_errorno((char*)"ERROR on shutdown(sock)"); //exit(0); }//close(sock); } free(offsetx_history); free(offsety_history); free(inputBuffer); }