void sendMatrix(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[]) { NetClient *nc = GetNetClient(nrhs, prhs); if (nrhs != 2) mexErrMsgTxt("Two arguments required: handle, matrix."); unsigned long datalen = mxGetN(prhs[1]) * mxGetM(prhs[1]); switch(mxGetClassID(prhs[1])) { case mxINT8_CLASS: case mxUINT8_CLASS: case mxCHAR_CLASS: datalen *= sizeof(char); break; case mxINT16_CLASS: case mxUINT16_CLASS: datalen *= sizeof(short); break; case mxUINT32_CLASS: case mxINT32_CLASS: datalen *= sizeof(int); break; case mxDOUBLE_CLASS: datalen *= sizeof(double); break; case mxSINGLE_CLASS: datalen *= sizeof(float); break; default: mexErrMsgTxt("Argument 2 must be a matrix of numeric type."); } void *theMatrix = mxGetPr(prhs[1]); try { nc->sendData(theMatrix, datalen); } catch (const SocketException & e) { const std::string why (e.why()); if (why.length()) mexWarnMsgTxt(why.c_str()); RETURN_NULL(); } RETURN(1); }
void readString(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[]) { if(nlhs < 1) mexErrMsgTxt("One output argument required."); NetClient *nc = GetNetClient(nrhs, prhs); try { std::string theString ( nc->receiveString() ); plhs[0] = mxCreateString(theString.c_str()); } catch (const SocketException & e) { mexWarnMsgTxt(e.why().c_str()); RETURN_NULL(); // note empty return.. } }
void readLines(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[]) { if(nlhs < 1) mexErrMsgTxt("One output argument required."); NetClient *nc = GetNetClient(nrhs, prhs); try { char **lines = nc->receiveLines(); int m; for (m = 0; lines[m]; m++) {} // count number of lines plhs[0] = mxCreateCharMatrixFromStrings(m, const_cast<const char **>(lines)); NetClient::deleteReceivedLines(lines); } catch (const SocketException &e) { mexWarnMsgTxt(e.why().c_str()); RETURN_NULL(); // empty return set } }
void tryConnection(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[]) { NetClient *nc = GetNetClient(nrhs, prhs); //if(nlhs < 1) mexErrMsgTxt("One output argument required."); bool ok = false; try { ok = nc->connect(); } catch (const SocketException & e) { mexWarnMsgTxt(e.why().c_str()); RETURN_NULL(); } if (!ok) { mexWarnMsgTxt(nc->errorReason().c_str()); RETURN_NULL(); } RETURN(1); }
void sendMatrix(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[]) { NetClient *nc = GetNetClient(nrhs, prhs); if(nrhs != 2) mexErrMsgTxt("Two arguments required: handle, matrix."); //if(nlhs < 1) mexErrMsgTxt("One output argument required."); if(mxGetClassID(prhs[0]) != mxDOUBLE_CLASS) mexErrMsgTxt("Argument 2 must be a matrix of doubles."); double *theMatrix = mxGetPr(prhs[1]); int msglen = mxGetN(prhs[1]) * mxGetM(prhs[1]) * sizeof(double); try { nc->sendData(theMatrix, msglen); } catch (const SocketException & e) { mexWarnMsgTxt(e.why().c_str()); RETURN_NULL(); } RETURN(1); }
void sendString(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[]) { NetClient *nc = GetNetClient(nrhs, prhs); if(nrhs != 2) mexErrMsgTxt("Two arguments required: handle, string."); //if(nlhs < 1) mexErrMsgTxt("One output argument required."); if(mxGetClassID(prhs[1]) != mxCHAR_CLASS) mexErrMsgTxt("Argument 2 must be a string."); char *tmp = mxArrayToString(prhs[1]); std::string theString (tmp); mxFree(tmp); try { nc->sendString(theString); } catch (const SocketException & e) { mexWarnMsgTxt(e.why().c_str()); RETURN_NULL(); } RETURN(1); }
void tryConnection(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[]) { NetClient *nc = GetNetClient(nrhs, prhs); //if(nlhs < 1) mexErrMsgTxt("One output argument required."); bool ok = false; try { ok = nc->connect(); if (ok) nc->setSocketOption(Socket::TCPNoDelay, true); } catch (const SocketException & e) { const std::string why (e.why()); if (why.length()) mexWarnMsgTxt(why.c_str()); RETURN_NULL(); } if (!ok) { mexWarnMsgTxt(nc->errorReason().c_str()); RETURN_NULL(); } RETURN(1); }
void readMatrix(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[]) { NetClient *nc = GetNetClient(nrhs, prhs); int ndims, datalen; if (!nlhs) mexErrMsgTxt("output (lhs) parameter is required."); if (nrhs < 3 || !mxIsChar(prhs[1]) || !mxIsDouble(prhs[2]) || (ndims=mxGetN(prhs[2])) < 2 || mxGetM(prhs[2]) != 1) mexErrMsgTxt("'readMatrix' needs arguments:\n Argument 1 handle\n Argument 2, a string 'double' or 'single' or 'uint8'\n Argument 3, a 1x4, 1x3 or 1x2 vector of dimensions for m,n[,o,p]"); int buflen = (mxGetM(prhs[1]) * mxGetN(prhs[1]) * sizeof(mxChar)) + 1; std::string stdstr(buflen, '0'); mxGetString(prhs[1], &stdstr[0], buflen); const char *str = stdstr.c_str(); const double *dims_dbls = mxGetPr(prhs[2]); if (ndims > 4) ndims = 4; const int dims[] = { static_cast<int>(dims_dbls[0]), static_cast<int>(dims_dbls[1]), ndims >= 3 ? static_cast<int>(dims_dbls[2]) : 1, ndims >= 4 ? static_cast<int>(dims_dbls[3]) : 1 }; datalen = dims[0]*dims[1]*dims[2]*dims[3]; mxClassID cls; if (!strcmpi(str, "double")) cls = mxDOUBLE_CLASS, datalen *= sizeof(double); else if (!strcmpi(str, "single")) cls = mxSINGLE_CLASS, datalen *= sizeof(float); else if (!strcmpi(str, "int8")) cls = mxINT8_CLASS, datalen *= sizeof(char); else if (!strcmpi(str, "uint8")) cls = mxUINT8_CLASS, datalen *= sizeof(char); else if (!strcmpi(str, "int16")) cls = mxINT16_CLASS, datalen *= sizeof(short); else if (!strcmpi(str, "uint16")) cls = mxUINT16_CLASS, datalen *= sizeof(short); else if (!strcmpi(str, "int32")) cls = mxINT32_CLASS, datalen *= sizeof(int); else if (!strcmpi(str, "uint32")) cls = mxUINT32_CLASS, datalen *= sizeof(int); else mexErrMsgTxt("Output matrix type must be one of 'single', 'double', 'int8', 'uint8', 'int16', 'int16', 'int32', 'uint32'."); plhs[0] = mxCreateNumericArray(ndims, dims, cls, mxREAL); try { nc->receiveData(mxGetData(plhs[0]), datalen, true); } catch (const SocketException & e) { const std::string why (e.why()); if (why.length()) mexWarnMsgTxt(why.c_str()); mxDestroyArray(plhs[0]); plhs[0] = 0; // nullify (empty) return.. RETURN_NULL(); } }
void readInt32Matrix(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[]) { NetClient *nc = GetNetClient(nrhs, prhs); if(nrhs != 3 || !mxIsDouble(prhs[1]) || !mxIsDouble(prhs[2]) ) mexErrMsgTxt("Output matrix size M, N as 2 real argument are required."); //if(nlhs < 1) mexErrMsgTxt("One output argument required."); const int m = static_cast<int>(*mxGetPr(prhs[1])), n = static_cast<int>(*mxGetPr(prhs[2])); const int dataLen = m*n*sizeof(int32); const int dims[] = { m, n }; plhs[0] = mxCreateNumericArray(2, dims, mxINT32_CLASS, mxREAL); try { nc->receiveData(mxGetData(plhs[0]), dataLen, true); } catch (const SocketException & e) { mexWarnMsgTxt(e.why().c_str()); mxDestroyArray(plhs[0]); plhs[0] = 0; // nullify (empty) return.. RETURN_NULL(); } }
void sendInt32Matrix(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[]) { NetClient *nc = GetNetClient(nrhs, prhs); if(nrhs != 2) mexErrMsgTxt("Two arguments required: handle, vector."); if(mxGetClassID(prhs[0]) != mxDOUBLE_CLASS) mexErrMsgTxt("Argument 2 must be a 1x1 scalar of doubles."); if(mxGetClassID(prhs[1]) != mxINT32_CLASS) mexErrMsgTxt("Argument 3 must be a matrix of int32s."); int32 *theInts = reinterpret_cast<int32 *>(mxGetData(prhs[1])); int msglen = mxGetN(prhs[1]) * mxGetM(prhs[1]) * sizeof(int32); try { nc->sendData(theInts, msglen); } catch (const SocketException & e) { mexWarnMsgTxt(e.why().c_str()); RETURN_NULL(); } RETURN(1); }
void closeSocket(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[]) { NetClient *nc = GetNetClient(nrhs, prhs); nc->disconnect(); RETURN(1); }