diff --git a/libraries/SITL/examples/JSON/MATLAB/tcp_udp_ip_2.0.6/pnet.c b/libraries/SITL/examples/JSON/MATLAB/tcp_udp_ip_2.0.6/pnet.c index 7d0324ff85..83b545d59f 100644 --- a/libraries/SITL/examples/JSON/MATLAB/tcp_udp_ip_2.0.6/pnet.c +++ b/libraries/SITL/examples/JSON/MATLAB/tcp_udp_ip_2.0.6/pnet.c @@ -1,5 +1,5 @@ /********************************************************** - + MEX file for the tcpip toolbox. */ #define VERSION "Version 2.0.5 2003-09-16" @@ -33,33 +33,33 @@ % pnet.c (.dll) as a plug-in as it is intended and let it be (dynamical) % linked to MATLAB(R) or any compiled stand alone application. - + Notes for Unix implementation Compile this with: - + mex -O pnet.c - + Notes for Windows implementation - + When using LCC, compile this with: mex -O pnet.c {MATLAB_INSTALL_DIR}\sys\lcc\lib\wsock32.lib -DWIN32 When using Visual C++, compile this with: mex -O pnet.c ws2_32.lib -DWIN32 - - + + == Main Authour == == Windows support == == Earlie/Basic UDP support == Peter Rydesäter Mario Bergeron Mike Medeiros at 23-Jan-2001. LYRtech Östersund, Sweden Québec, Canada - +46 70 560 68 16 + +46 70 560 68 16 Peter.Rydesater@mh.se Mario.Bergeron@lyrtech.com SE MORE: http://www.rydesater.com - + **********************************************************/ - + /******* GENERAL DEFINES *********/ #include #include @@ -69,7 +69,7 @@ /******* WINDOWS ONLY DEFINES *********/ #ifdef WIN32 -#define IFWINDOWS(dothis) dothis +#define IFWINDOWS(dothis) dothis #define IFUNIX(dothis) //#include #include @@ -82,7 +82,7 @@ /******* NON WINDOWS DEFINES *********/ #else -#define IFWINDOWS(dothis) +#define IFWINDOWS(dothis) #define IFUNIX(dothis) dothis #include @@ -91,7 +91,7 @@ #include #include #include -#include +#include #include #include #define nonblockingsocket(s) fcntl(s,F_SETFL,O_NONBLOCK) @@ -131,7 +131,7 @@ /* Different status of a con_info struct handles a file descriptor */ #define STATUS_FREE -1 #define STATUS_NOCONNECT 0 // Disconnected pipe that is note closed -#define STATUS_TCP_SOCKET 1 +#define STATUS_TCP_SOCKET 1 #define STATUS_IO_OK 5 // Used for IS_... test #define STATUS_UDP_CLIENT 6 #define STATUS_UDP_SERVER 8 @@ -181,29 +181,29 @@ int con_index=0; /* Current index possition for list of handle unsigned long mex_call_counter=0; /* Counter that counts how many calls that have been done to pnet */ /***********************************************************************/ -void Print_Start_Message(){ +void Print_Start_Message() { mexPrintf("\n===============================================================================\n" - "Loaded pnet MEX-file for the tcp/udp/ip-toolbox Compiled @ " - __DATE__ " " __TIME__ "\n" - VERSION "\n" - "Copyright (C) Peter Rydesäter, Sweden, et al. , 1998 - 2003\n" - "GNU General Public License, se license.txt for full license notis.\n" - "You are allowed to (dynamicaly) link this file with non-free code. \n\n" - " http://www.rydesater.com \n\n" - "===============================================================================\n\n" - ); + "Loaded pnet MEX-file for the tcp/udp/ip-toolbox Compiled @ " + __DATE__ " " __TIME__ "\n" + VERSION "\n" + "Copyright (C) Peter Rydesäter, Sweden, et al. , 1998 - 2003\n" + "GNU General Public License, se license.txt for full license notis.\n" + "You are allowed to (dynamicaly) link this file with non-free code. \n\n" + " http://www.rydesater.com \n\n" + "===============================================================================\n\n" + ); } /***********************************************************************/ int myoptstrcmp(const char *s1,const char *s2) { int val; - while( (val= toupper(*s1) - toupper(*s2))==0 ){ - if(*s1==0 || *s2==0) return 0; - s1++; - s2++; - while(*s1=='_') s1++; - while(*s2=='_') s2++; + while( (val= toupper(*s1) - toupper(*s2))==0 ) { + if(*s1==0 || *s2==0) return 0; + s1++; + s2++; + while(*s1=='_') s1++; + while(*s2=='_') s2++; } return val; } @@ -214,7 +214,7 @@ void *myrealloc(char *ptr,int newsize) { ptr=realloc(ptr,newsize); if(ptr==NULL && newsize>0) - mexErrMsgTxt("Internal out of memory!"); + mexErrMsgTxt("Internal out of memory!"); return ptr; } @@ -223,23 +223,23 @@ void *myrealloc(char *ptr,int newsize) void newbuffsize(io_buff *buff,int newsize) { // fprintf(stderr,"NEWSIZE:%d\n",newsize); - if(newsize==-1){ - free(buff->ptr); - buff->ptr=NULL; - return; + if(newsize==-1) { + free(buff->ptr); + buff->ptr=NULL; + return; } if(newsizepos) - newsize=buff->pos; + newsize=buff->pos; if(newsize<256) - newsize=256; - if(newsize>buff->len){ // Grow.... - // fprintf(stderr,"NEWSIZE UP %d -> %d\n",buff->len,newsize*2); - buff->ptr=myrealloc(buff->ptr,newsize*2); - buff->len=newsize*2; - }else if(newsize*4 < buff->len){ // Decrease... - // fprintf(stderr,"NEWSIZE DOWN %d -> %d\n",buff->len,newsize*2); - buff->ptr=myrealloc(buff->ptr,newsize*2); - buff->len=newsize*2; + newsize=256; + if(newsize>buff->len) { // Grow.... + // fprintf(stderr,"NEWSIZE UP %d -> %d\n",buff->len,newsize*2); + buff->ptr=myrealloc(buff->ptr,newsize*2); + buff->len=newsize*2; + } else if(newsize*4 < buff->len) { // Decrease... + // fprintf(stderr,"NEWSIZE DOWN %d -> %d\n",buff->len,newsize*2); + buff->ptr=myrealloc(buff->ptr,newsize*2); + buff->len=newsize*2; } } @@ -284,33 +284,85 @@ void perror(const char *context ) mexPrintf( "[%s]: WSA error: ", context ); switch ( wsa_err ) { - case WSANOTINITIALISED: mexPrintf( "WSANOTINITIALISED\n" ); break; - case WSAENETDOWN: mexPrintf( "WSAENETDOWN \n" ); break; - case WSAEADDRINUSE: mexPrintf( "WSAEADDRINUSE \n" ); break; - case WSAEACCES: mexPrintf( "WSAEACCES \n" ); break; - case WSAEINTR: mexPrintf( "WSAEINTR \n" ); break; - case WSAEINPROGRESS: mexPrintf( "WSAEINPROGRESS \n" ); break; - case WSAEALREADY: mexPrintf( "WSAEALREADY \n" ); break; - case WSAEADDRNOTAVAIL: mexPrintf( "WSAEADDRNOTAVAIL \n" ); break; - case WSAEAFNOSUPPORT: mexPrintf( "WSAEAFNOSUPPORT \n" ); break; - case WSAEFAULT: mexPrintf( "WSAEFAULT \n" ); break; - case WSAENETRESET: mexPrintf( "WSAENETRESET \n" ); break; - case WSAENOBUFS: mexPrintf( "WSAENOBUFS \n" ); break; - case WSAENOTSOCK: mexPrintf( "WSAENOTSOCK \n" ); break; - case WSAEOPNOTSUPP: mexPrintf( "WSAEOPNOTSUPP \n" ); break; - case WSAESHUTDOWN: mexPrintf( "WSAESHUTDOWN \n" ); break; - case WSAEWOULDBLOCK: mexPrintf( "WSAEWOULDBLOCK \n" ); break; - case WSAEMSGSIZE: mexPrintf( "WSAEMSGSIZE \n" ); break; - case WSAEHOSTUNREACH: mexPrintf( "WSAEHOSTUNREACH \n" ); break; - case WSAEINVAL: mexPrintf( "WSAEINVAL \n" ); break; + case WSANOTINITIALISED: + mexPrintf( "WSANOTINITIALISED\n" ); + break; + case WSAENETDOWN: + mexPrintf( "WSAENETDOWN \n" ); + break; + case WSAEADDRINUSE: + mexPrintf( "WSAEADDRINUSE \n" ); + break; + case WSAEACCES: + mexPrintf( "WSAEACCES \n" ); + break; + case WSAEINTR: + mexPrintf( "WSAEINTR \n" ); + break; + case WSAEINPROGRESS: + mexPrintf( "WSAEINPROGRESS \n" ); + break; + case WSAEALREADY: + mexPrintf( "WSAEALREADY \n" ); + break; + case WSAEADDRNOTAVAIL: + mexPrintf( "WSAEADDRNOTAVAIL \n" ); + break; + case WSAEAFNOSUPPORT: + mexPrintf( "WSAEAFNOSUPPORT \n" ); + break; + case WSAEFAULT: + mexPrintf( "WSAEFAULT \n" ); + break; + case WSAENETRESET: + mexPrintf( "WSAENETRESET \n" ); + break; + case WSAENOBUFS: + mexPrintf( "WSAENOBUFS \n" ); + break; + case WSAENOTSOCK: + mexPrintf( "WSAENOTSOCK \n" ); + break; + case WSAEOPNOTSUPP: + mexPrintf( "WSAEOPNOTSUPP \n" ); + break; + case WSAESHUTDOWN: + mexPrintf( "WSAESHUTDOWN \n" ); + break; + case WSAEWOULDBLOCK: + mexPrintf( "WSAEWOULDBLOCK \n" ); + break; + case WSAEMSGSIZE: + mexPrintf( "WSAEMSGSIZE \n" ); + break; + case WSAEHOSTUNREACH: + mexPrintf( "WSAEHOSTUNREACH \n" ); + break; + case WSAEINVAL: + mexPrintf( "WSAEINVAL \n" ); + break; - case WSAECONNREFUSED: mexPrintf( "WSAECONNREFUSED \n" ); break; - case WSAECONNABORTED: mexPrintf( "WSAECONNABORTED \n" ); break; - case WSAECONNRESET: mexPrintf( "WSAECONNRESET \n" ); break; - case WSAEISCONN: mexPrintf( "WSAEISCONN \n" ); break; - case WSAENOTCONN: mexPrintf( "WSAENOTCONN \n" ); break; - case WSAETIMEDOUT: mexPrintf( "WSAETIMEDOUT \n" ); break; - default: mexPrintf( "Unknown(%d) error!\n", wsa_err ); break; + case WSAECONNREFUSED: + mexPrintf( "WSAECONNREFUSED \n" ); + break; + case WSAECONNABORTED: + mexPrintf( "WSAECONNABORTED \n" ); + break; + case WSAECONNRESET: + mexPrintf( "WSAECONNRESET \n" ); + break; + case WSAEISCONN: + mexPrintf( "WSAEISCONN \n" ); + break; + case WSAENOTCONN: + mexPrintf( "WSAENOTCONN \n" ); + break; + case WSAETIMEDOUT: + mexPrintf( "WSAETIMEDOUT \n" ); + break; + default: + mexPrintf( "Unknown(%d) error!\n", wsa_err ); + break; } return; } @@ -331,25 +383,32 @@ void byteswapdata(char *ptr,const int elements,const int elementsize,int mode) if(elementsize<2) return; if(is_intel_order[0]==1 && mode==2) mode=1; if(is_intel_order[0]==0 && mode==3) mode=1; - if(mode==1){ - int e; - // fprintf(stderr,"SWAP DATA\n"); - switch(elementsize){ - case 2: for(e=0;e=MAX_CON) - mexErrMsgTxt("Unvalid value of handler!\n"); + mexErrMsgTxt("Unvalid value of handler!\n"); if(idx>=0) { - con_index=idx; - if(con[con_index].status==STATUS_FREE) - mexErrMsgTxt("No valid handler! already closed?"); - return 1; + con_index=idx; + if(con[con_index].status==STATUS_FREE) + mexErrMsgTxt("No valid handler! already closed?"); + return 1; } debug_view_con_status("START MOVE"); if(idx==CON_FREE) /* Move con_index until it find a free non used struct */ { - for(con_index=0;con_indexargno) - return 1; + return 1; return 0; } @@ -473,7 +532,7 @@ const mxArray *my_mexInputArg(const int argno) { // fprintf(stderr,"GET_INPUT_ARG NO:%d\n",argno); // DEBUG if(!my_mexIsInputArgOK(argno)) - mexErrMsgTxt("Missing input argument."); + mexErrMsgTxt("Missing input argument."); return gprhs[argno]; } @@ -484,8 +543,8 @@ const char *my_mexInputOptionString(const int argno) static char buff[80+1]; buff[0]=0; if(my_mexIsInputArgOK(argno)) - if(mxIsChar(my_mexInputArg(argno))) - mxGetString(my_mexInputArg(argno),buff,80); + if(mxIsChar(my_mexInputArg(argno))) + mxGetString(my_mexInputArg(argno),buff,80); return buff; } @@ -496,11 +555,11 @@ const char *my_mexFindInputString(int argno) const char *str; static char buff[1]; buff[0]=0; - while(my_mexIsInputArgOK(argno)){ - str=my_mexInputOptionString(argno); - if(!isdigit(str[0]) && str[0]!=0 ) - return str; - argno++; + while(my_mexIsInputArgOK(argno)) { + str=my_mexInputOptionString(argno); + if(!isdigit(str[0]) && str[0]!=0 ) + return str; + argno++; } return buff; } @@ -512,11 +571,11 @@ int my_mexFindInputOption(int argno,const char *opt) char buff[80+1]; while(my_mexIsInputArgOK(argno)) { - buff[0]=0; - mxGetString(my_mexInputArg(argno),buff,80); - if(myoptstrcmp(buff,opt)==0) - return 1; - argno++; + buff[0]=0; + mxGetString(my_mexInputArg(argno),buff,80); + if(myoptstrcmp(buff,opt)==0) + return 1; + argno++; } return 0; } @@ -526,9 +585,9 @@ int my_mexFindInputOption(int argno,const char *opt) double my_mexInputCell(const int argno,int cell_no) { if(!mxIsDouble(my_mexInputArg(argno))) - mexErrMsgTxt("Argument has wrong datatype."); + mexErrMsgTxt("Argument has wrong datatype."); if(cell_no>=mxGetNumberOfElements(my_mexInputArg(argno))) - mexErrMsgTxt("Argument has wrong number of cells"); + mexErrMsgTxt("Argument has wrong number of cells"); return mxGetPr(my_mexInputArg(argno))[cell_no]; } @@ -538,21 +597,21 @@ double my_mexInputCell(const int argno,int cell_no) int my_mexInputSize(const int argno) { if(!my_mexIsInputArgOK(argno)) - return DEFAULT_INPUT_SIZE; - if(mxIsChar(my_mexInputArg(argno))){ - const int ch=(my_mexInputOptionString(argno)[0]); - if(!isdigit(ch)) - return DEFAULT_INPUT_SIZE; - else - return atoi(my_mexInputOptionString(argno)); + return DEFAULT_INPUT_SIZE; + if(mxIsChar(my_mexInputArg(argno))) { + const int ch=(my_mexInputOptionString(argno)[0]); + if(!isdigit(ch)) + return DEFAULT_INPUT_SIZE; + else + return atoi(my_mexInputOptionString(argno)); } - if(mxGetNumberOfElements(my_mexInputArg(argno))>1){ - int val=1,n=0,el=mxGetNumberOfElements(my_mexInputArg(argno)); - for(n=0;n1) { + int val=1,n=0,el=mxGetNumberOfElements(my_mexInputArg(argno)); + for(n=0; npos+len*si); - if(id==mxCHAR_CLASS){ - mxChar *ptr = (mxChar *)mxGetPr(gprhs[argno]); - int a; - for(a=0;aptr[buff->pos++]=(char)(unsigned char)ptr[a]; - }else{ - char *ptr = (char *)mxGetPr(gprhs[argno]); - byteswapcopy(&buff->ptr[buff->pos],ptr,len,si,swap); - buff->pos+=(len*si); + if(id==mxCHAR_CLASS) { + mxChar *ptr = (mxChar *)mxGetPr(gprhs[argno]); + int a; + for(a=0; aptr[buff->pos++]=(char)(unsigned char)ptr[a]; + } else { + char *ptr = (char *)mxGetPr(gprhs[argno]); + byteswapcopy(&buff->ptr[buff->pos],ptr,len,si,swap); + buff->pos+=(len*si); } return len; } @@ -598,14 +657,14 @@ int my_mexInputArray2Buff(const int argno,io_buff *buff) void my_mexReturnValue(double val) { if((gret_args>gnlhs) && (gret_args>1)) - return; + return; gplhs[gret_args]=mxCreateDoubleMatrix(1,1,mxREAL); if(gplhs[gret_args]==NULL) - mexErrMsgTxt("Matrix creation error! Lack of memory?"); + mexErrMsgTxt("Matrix creation error! Lack of memory?"); else { - *mxGetPr(gplhs[gret_args])=val; - gret_args++; + *mxGetPr(gplhs[gret_args])=val; + gret_args++; } // mexPrintf("DEBUG MEX RETURN VALUE:%g\n",val); // DEBUG } @@ -616,10 +675,10 @@ void my_mexReturnMatrix(int rows, int cols, double *vals) { double *pr; if((gret_args>gnlhs) && (gret_args>1)) - return; + return; gplhs[gret_args] = mxCreateDoubleMatrix( rows, cols, mxREAL); if( gplhs[gret_args] == NULL) - mexErrMsgTxt("Matrix creation error"); + mexErrMsgTxt("Matrix creation error"); pr = (double *)mxGetPr(gplhs[gret_args]); memcpy(pr,vals,rows*cols*sizeof(double)); gret_args++; @@ -630,10 +689,10 @@ void my_mexReturnMatrix(int rows, int cols, double *vals) void my_mexReturnString(const char *str) { if(gret_args>gnlhs && gret_args>1) - return; + return; gplhs[gret_args]=mxCreateString(str); if(gplhs[gret_args]==NULL) - mexErrMsgTxt("String creation error! Lack of memory?"); + mexErrMsgTxt("String creation error! Lack of memory?"); gret_args++; } @@ -643,7 +702,7 @@ void my_mexReturnArrayFromBuff(const int argno,io_buff *buff,const int line) { const int maxelements=my_mexInputSize(argno); const mxClassID id=str2classid(my_mexInputOptionString(argno+1)); - mwSize dims[20]={0,0,0,0,0, 0,0,0,0,0, 0,0,0,0,0, 0,0,0,0,0 }; + mwSize dims[20]= {0,0,0,0,0, 0,0,0,0,0, 0,0,0,0,0, 0,0,0,0,0 }; const int si=classid2size(id); int returnelements= ( (buff->pos/si)< maxelements )?(buff->pos/si):maxelements; int deleteelements=returnelements; @@ -655,63 +714,63 @@ void my_mexReturnArrayFromBuff(const int argno,io_buff *buff,const int line) if(my_mexFindInputOption(argno+1,"NETWORK")) swap=2; if(my_mexFindInputOption(argno+1,"INTEL")) swap=3; if(return_no_dims>20) - mexErrMsgTxt("To many dimentions to return."); + mexErrMsgTxt("To many dimentions to return."); debug_view_con_status("GET_ARRAY"); - if(line){ - int n=-7; - if(id!=mxCHAR_CLASS && return_no_dims) - mexErrMsgTxt("'READLINE' works only with datatype char and non fixed blocksize"); - for(n=0;nptr[n]=='\n') - break; - if(n==maxelements) // If no new-line found inside limit... - deleteelements=returnelements=maxelements; // ...return first part of splited line. - else if(n==returnelements) // If new-line not recived inside limit... - deleteelements=returnelements=0; // ...return empty string, and delete nothing. - else if(n>0 && buff->ptr[n-1]=='\r') // If(*3) new-line, return line of char but not nl chars. - deleteelements=2+(returnelements=n-1); - else - deleteelements=1+(returnelements=n); - return_no_dims=1; + if(line) { + int n=-7; + if(id!=mxCHAR_CLASS && return_no_dims) + mexErrMsgTxt("'READLINE' works only with datatype char and non fixed blocksize"); + for(n=0; nptr[n]=='\n') + break; + if(n==maxelements) // If no new-line found inside limit... + deleteelements=returnelements=maxelements; // ...return first part of splited line. + else if(n==returnelements) // If new-line not recived inside limit... + deleteelements=returnelements=0; // ...return empty string, and delete nothing. + else if(n>0 && buff->ptr[n-1]=='\r') // If(*3) new-line, return line of char but not nl chars. + deleteelements=2+(returnelements=n-1); + else + deleteelements=1+(returnelements=n); + return_no_dims=1; } - if(return_no_dims>1){ // If shape of return argument is specified..... - if(returnelements==maxelements){ // ...then only accept correct shape. - int n; - for(n=0;n0){ // else... Just return row of available elements - dims[0]=1; - dims[1]=returnelements; - return_no_dims=2; + if(return_no_dims>1) { // If shape of return argument is specified..... + if(returnelements==maxelements) { // ...then only accept correct shape. + int n; + for(n=0; n0) { // else... Just return row of available elements + dims[0]=1; + dims[1]=returnelements; + return_no_dims=2; } - if(! (gret_args>gnlhs && gret_args>1) ){ - /* Create a 2-Dimensional character mxArray.*/ - if( dims[0]==0) - gplhs[gret_args] = mxCreateNumericArray(0,dims,id,mxREAL); - else - gplhs[gret_args] = mxCreateNumericArray(return_no_dims,dims,id,mxREAL); - if(gplhs[gret_args] == NULL) - mexErrMsgTxt("Could not create return array."); - if(dims[0]!=0){ - if(id==mxCHAR_CLASS){ - int i; - mxChar *p=(mxChar *)mxGetPr(gplhs[gret_args]); - for(i=0;iptr[i]; - }else{ - char *p=(char *)mxGetPr(gplhs[gret_args]); - byteswapcopy(p,buff->ptr,returnelements,si,swap); - } - } - gret_args++; + if(! (gret_args>gnlhs && gret_args>1) ) { + /* Create a 2-Dimensional character mxArray.*/ + if( dims[0]==0) + gplhs[gret_args] = mxCreateNumericArray(0,dims,id,mxREAL); + else + gplhs[gret_args] = mxCreateNumericArray(return_no_dims,dims,id,mxREAL); + if(gplhs[gret_args] == NULL) + mexErrMsgTxt("Could not create return array."); + if(dims[0]!=0) { + if(id==mxCHAR_CLASS) { + int i; + mxChar *p=(mxChar *)mxGetPr(gplhs[gret_args]); + for(i=0; iptr[i]; + } else { + char *p=(char *)mxGetPr(gplhs[gret_args]); + byteswapcopy(p,buff->ptr,returnelements,si,swap); + } + } + gret_args++; } - // debug_view_con_status("GET_ARRAY NÄSTAN KLAR"); + // debug_view_con_status("GET_ARRAY NÄSTAN KLAR"); // Delete from read buffer if not "VIEW" option and dims filled - if(my_mexFindInputOption(argno+1,"VIEW")==0 && deleteelements>0 ){ - buff->pos-=deleteelements*si; - memmove(buff->ptr,&buff->ptr[deleteelements*si],buff->pos); - newbuffsize(buff,buff->pos); + if(my_mexFindInputOption(argno+1,"VIEW")==0 && deleteelements>0 ) { + buff->pos-=deleteelements*si; + memmove(buff->ptr,&buff->ptr[deleteelements*si],buff->pos); + newbuffsize(buff,buff->pos); } // mexPrintf("DEBUG MEX RETURN ARRAY OF:%d\n",returnelements); // DEBUG @@ -726,17 +785,17 @@ int ipv4_lookup(const char *hostname,int port) struct in_addr addr; /* my address information */ /* Try IP address */ addr.s_addr=inet_addr(hostname); - if (addr.s_addr==INADDR_NONE){ - /*Can't resolve host string as dot notation IP number... - try lookup IP from hostname */ - struct hostent *he; - // fprintf(stderr,"Trying nameserverlookup:%s\n",hostname); - he=gethostbyname(hostname); - if(he==NULL){ - mexPrintf("\nUNKNOWN HOST:%s\n",hostname); - return -1; /* Can not lookup hostname */ - } - addr = *((struct in_addr *)he->h_addr); + if (addr.s_addr==INADDR_NONE) { + /*Can't resolve host string as dot notation IP number... + try lookup IP from hostname */ + struct hostent *he; + // fprintf(stderr,"Trying nameserverlookup:%s\n",hostname); + he=gethostbyname(hostname); + if(he==NULL) { + mexPrintf("\nUNKNOWN HOST:%s\n",hostname); + return -1; /* Can not lookup hostname */ + } + addr = *((struct in_addr *)he->h_addr); } con[con_index].remote_addr.sin_family=AF_INET; con[con_index].remote_addr.sin_port=htons(port); @@ -758,37 +817,37 @@ int writedata() int retval=0; int lastsize=1000000; if(con[con_index].status65534 ) // TODO: Ta bort! // len=65534; while(sentlen0?retval:0; - sentlen+=lastsize; - /* if( retval==0){ - mexPrintf("\nREMOTE HOST DISCONNECTED\n"); - con[con_index].status=STATUS_NOCONNECT; - break; - }*/ - if(retval<0 && s_errno!=EWOULDBLOCK - // IFWINDOWS( && s_errno!=WSAECONNRESET ) // DEBUG: REMOVE THIS LINE? - ){ - con[con_index].status=STATUS_NOCONNECT; - perror( "sendto() / send()" ); - mexPrintf("\nREMOTE HOST DISCONNECTED\n"); - break; - } - if( !IS_STATUS_TCP_CONNECTED(con[con_index].status) && sentlen>0 ) - break; - if(timeoutat<=my_now()) - break; + if(lastsize<1000) + usleep(DEFAULT_USLEEP); + if(IS_STATUS_UDP_NO_CON(con[con_index].status)) + retval = sendto(fid,&ptr[sentlen],len-sentlen,MSG_NOSIGNAL, + (struct sockaddr *)&con[con_index].remote_addr,sizeof(struct sockaddr)); + else + retval=send(fid,&ptr[sentlen],len-sentlen,MSG_NOSIGNAL); + lastsize=retval>0?retval:0; + sentlen+=lastsize; + /* if( retval==0){ + mexPrintf("\nREMOTE HOST DISCONNECTED\n"); + con[con_index].status=STATUS_NOCONNECT; + break; + }*/ + if(retval<0 && s_errno!=EWOULDBLOCK + // IFWINDOWS( && s_errno!=WSAECONNRESET ) // DEBUG: REMOVE THIS LINE? + ) { + con[con_index].status=STATUS_NOCONNECT; + perror( "sendto() / send()" ); + mexPrintf("\nREMOTE HOST DISCONNECTED\n"); + break; + } + if( !IS_STATUS_TCP_CONNECTED(con[con_index].status) && sentlen>0 ) + break; + if(timeoutat<=my_now()) + break; } con[con_index].write.pos-=sentlen; memmove(con[con_index].write.ptr, &con[con_index].write.ptr[sentlen], con[con_index].write.pos); @@ -812,9 +871,9 @@ void init_con(int fid,int status) void close_con() { if(con[con_index].fid>=0) - close(con[con_index].fid); + close(con[con_index].fid); else - mexWarnMsgTxt("Closing already closed connection!"); + mexWarnMsgTxt("Closing already closed connection!"); newbuffsize(&con[con_index].write,-1); newbuffsize(&con[con_index].read,-1); init_con(-1,STATUS_FREE); @@ -825,11 +884,11 @@ void close_con() int closeall(void) { int flag=0; - for(con_index=0;con_index=0) { /* Already closed?? */ - close_con(); - flag=1; - } + for(con_index=0; con_index=0) { /* Already closed?? */ + close_con(); + flag=1; + } return flag; } @@ -838,7 +897,7 @@ int closeall(void) void CleanUpMex(void) { if(closeall()) /* close all still open connections...*/ - mexWarnMsgTxt("Unloading mex file. Unclosed tcp/udp/ip connections will be closed!"); + mexWarnMsgTxt("Unloading mex file. Unclosed tcp/udp/ip connections will be closed!"); IFWINDOWS( WSACleanup(); ); } @@ -850,68 +909,68 @@ int read2buff(const int len,int newline,int noblock) int retval=-1; if(len0){ - if(IS_STATUS_CONNECTED(con[con_index].status)) - retval=recv(con[con_index].fid,&con[con_index].read.ptr[con[con_index].read.pos],readlen ,MSG_NOSIGNAL); - else{ - struct sockaddr_in my_addr; - int fromlen=sizeof(my_addr); + if(readlen>0) { + if(IS_STATUS_CONNECTED(con[con_index].status)) + retval=recv(con[con_index].fid,&con[con_index].read.ptr[con[con_index].read.pos],readlen,MSG_NOSIGNAL); + else { + struct sockaddr_in my_addr; + int fromlen=sizeof(my_addr); - // Copy 0.0.0.0 adress and 0 port to remote_addr as init-value. - memset(&my_addr,0,sizeof(my_addr)); - con[con_index].remote_addr.sin_addr = my_addr.sin_addr; - con[con_index].remote_addr.sin_port = my_addr.sin_port; - retval=recvfrom(con[con_index].fid,&con[con_index].read.ptr[con[con_index].read.pos], - readlen,MSG_NOSIGNAL,(struct sockaddr *)&my_addr, &fromlen); - if (retval>0){ - con[con_index].remote_addr.sin_addr = my_addr.sin_addr; - con[con_index].remote_addr.sin_port = htons((unsigned short int)ntohs(my_addr.sin_port)); - } - } - if( retval==0){ - mexPrintf("\nREMOTE HOST DISCONNECTED\n"); - con[con_index].status=STATUS_NOCONNECT; - break; - } - if(retval<0 && s_errno!=EWOULDBLOCK - // IFWINDOWS( && s_errno!=WSAECONNRESET )// DEBUG: REMOVE THIS LINE? - ) { - con[con_index].status=STATUS_NOCONNECT; - perror( "recvfrom() or recv()" ); - break; - } - } - // fprintf(stderr,"RET:%d/%d ",retval,s_errno); - readlen=retval>0?retval:0; - con[con_index].read.pos+=readlen; - if( !IS_STATUS_TCP_CONNECTED(con[con_index].status) && con[con_index].read.pos>0 ) - break; - if( con[con_index].read.pos>=len ) - break; - if(timeoutat<=my_now() || noblock) - break; - if(newline){ - int n; - for(n=0;n0) { + con[con_index].remote_addr.sin_addr = my_addr.sin_addr; + con[con_index].remote_addr.sin_port = htons((unsigned short int)ntohs(my_addr.sin_port)); + } + } + if( retval==0) { + mexPrintf("\nREMOTE HOST DISCONNECTED\n"); + con[con_index].status=STATUS_NOCONNECT; + break; + } + if(retval<0 && s_errno!=EWOULDBLOCK + // IFWINDOWS( && s_errno!=WSAECONNRESET )// DEBUG: REMOVE THIS LINE? + ) { + con[con_index].status=STATUS_NOCONNECT; + perror( "recvfrom() or recv()" ); + break; + } + } + // fprintf(stderr,"RET:%d/%d ",retval,s_errno); + readlen=retval>0?retval:0; + con[con_index].read.pos+=readlen; + if( !IS_STATUS_TCP_CONNECTED(con[con_index].status) && con[con_index].read.pos>0 ) + break; + if( con[con_index].read.pos>=len ) + break; + if(timeoutat<=my_now() || noblock) + break; + if(newline) { + int n; + for(n=0; n -1) + while(1) { + if ((new_fd = accept(sock_fd, (struct sockaddr *)&con[con_index].remote_addr,&sin_size)) > -1) break; if(timeoutat<=my_now()|| noblock) - return -1; - usleep(DEFAULT_USLEEP); + return -1; + usleep(DEFAULT_USLEEP); } nonblockingsocket(new_fd); /* Non blocking read! */ setsockopt(new_fd,SOL_SOCKET,SO_KEEPALIVE,(void *)1,0); /* realy needed? */ @@ -1003,17 +1062,17 @@ int tcpiplisten(int noblock) /*****************************************************************/ /* Make a UDP socket be a "connected" UDP socket */ int udp_connect(const char *hostname,int port) -{ +{ if(con[con_index].status < STATUS_UDP_CLIENT) - mexErrMsgTxt("Must pass UDP client or server handler!"); + mexErrMsgTxt("Must pass UDP client or server handler!"); if(ipv4_lookup(hostname,port)==-1) - return -1; + return -1; if(connect(con[con_index].fid,(struct sockaddr *)&con[con_index].remote_addr,sizeof(struct sockaddr)) == -1) - return -1; /*Can't connect to remote host. */ + return -1; /*Can't connect to remote host. */ if(con[con_index].status == STATUS_UDP_CLIENT) - con[con_index].status = STATUS_UDP_CLIENT_CONNECT; + con[con_index].status = STATUS_UDP_CLIENT_CONNECT; else - con[con_index].status = STATUS_UDP_SERVER_CONNECT; + con[con_index].status = STATUS_UDP_SERVER_CONNECT; nonblockingsocket(con[con_index].fid); /* Non blocking read! */ return con[con_index].status; } @@ -1027,27 +1086,27 @@ void mexFunction( mxArray *plhs[], /* array of pointers to output arguments */ int nrhs, /* number of inputs */ const mxArray *prhs[] /* array of pointers to input arguments */ - ) +) { char fun[80+1]; /* Initialization on first call to the mex file */ if(mex_call_counter==0) { #ifdef WIN32 - WORD wVersionRequested; - WSADATA wsaData; - int wsa_err; - wVersionRequested = MAKEWORD( 2, 0 ); - wsa_err = WSAStartup( wVersionRequested, &wsaData ); - if (wsa_err) - mexErrMsgTxt("Error starting WINSOCK32."); + WORD wVersionRequested; + WSADATA wsaData; + int wsa_err; + wVersionRequested = MAKEWORD( 2, 0 ); + wsa_err = WSAStartup( wVersionRequested, &wsaData ); + if (wsa_err) + mexErrMsgTxt("Error starting WINSOCK32."); #endif - Print_Start_Message(); + Print_Start_Message(); - mexAtExit(CleanUpMex); - /* Init all connecttions to to free */ - for(con_index=0;con_index=0){ - move_con(STATUS_FREE); - init_con(fd,STATUS_TCP_SOCKET); - my_mexReturnValue(con_index); - }else - my_mexReturnValue(-1); - return; - } - if(myoptstrcmp(fun,"UDPSOCKET")==0){ - const int fd=tcp_udp_socket((int)my_mexInputScalar(1), 1); - if(fd>=0){ - move_con(STATUS_FREE); - init_con(fd,STATUS_UDP_CLIENT); - my_mexReturnValue(con_index); - }else - my_mexReturnValue(-1); - return; - } - if(myoptstrcmp(fun,"DOCALLBACKS")==0){ - return; - } + + /* Find of the function name corresponds to a non connection associated function */ + if(myoptstrcmp(fun,"CLOSEALL")==0) { + closeall(); + return; + } + if(myoptstrcmp(fun,"TCPCONNECT")==0) { + const int port=(int)my_mexInputScalar(2); + const char *hostname=my_mexInputOptionString(1); + move_con(STATUS_FREE); + my_mexReturnValue(tcp_connect(hostname,port)); + return; + } + if(myoptstrcmp(fun,"TCPSOCKET")==0) { + const int fd=tcp_udp_socket((int)my_mexInputScalar(1), 0); + if(fd>=0) { + move_con(STATUS_FREE); + init_con(fd,STATUS_TCP_SOCKET); + my_mexReturnValue(con_index); + } else + my_mexReturnValue(-1); + return; + } + if(myoptstrcmp(fun,"UDPSOCKET")==0) { + const int fd=tcp_udp_socket((int)my_mexInputScalar(1), 1); + if(fd>=0) { + move_con(STATUS_FREE); + init_con(fd,STATUS_UDP_CLIENT); + my_mexReturnValue(con_index); + } else + my_mexReturnValue(-1); + return; + } + if(myoptstrcmp(fun,"DOCALLBACKS")==0) { + return; + } } /* Get connection handler and suppose that it is a connection assosiated function */ /* Find given handel */ // if(strncasecmp(fun,"DEF",3)!=0) - if(move_con((int)my_mexInputScalar(0))==0) - mexErrMsgTxt("Unknown connection handler"); + if(move_con((int)my_mexInputScalar(0))==0) + mexErrMsgTxt("Unknown connection handler"); strncpy(fun,my_mexInputOptionString(1),80); - // mexPrintf("DEBUG MEX(2):[%d] %s\n",con_index,fun); // DEBUG +// mexPrintf("DEBUG MEX(2):[%d] %s\n",con_index,fun); // DEBUG debug_view_con_status("CON_MOVED!!"); - - if(myoptstrcmp(fun,"CLOSE")==0){ - close_con(); - return; + + if(myoptstrcmp(fun,"CLOSE")==0) { + close_con(); + return; } - if(myoptstrcmp(fun,"TCPLISTEN")==0){ - if(con[con_index].status!=STATUS_TCP_SOCKET) - mexErrMsgTxt("Invalid socket for LISTEN, Already open, or UDP?..."); - my_mexReturnValue(tcpiplisten(my_mexFindInputOption(2,"noblock"))); - return; + if(myoptstrcmp(fun,"TCPLISTEN")==0) { + if(con[con_index].status!=STATUS_TCP_SOCKET) + mexErrMsgTxt("Invalid socket for LISTEN, Already open, or UDP?..."); + my_mexReturnValue(tcpiplisten(my_mexFindInputOption(2,"noblock"))); + return; } /* MATLAB called with status = UDP_CONNECT(fid, ip, port) */ - if(myoptstrcmp(fun,"UDPCONNECT")==0){ - const int port=(int)my_mexInputScalar(3); - const char *hostname=my_mexInputOptionString(2); - my_mexReturnValue(udp_connect(hostname,port)); - return; + if(myoptstrcmp(fun,"UDPCONNECT")==0) { + const int port=(int)my_mexInputScalar(3); + const char *hostname=my_mexInputOptionString(2); + my_mexReturnValue(udp_connect(hostname,port)); + return; } - if(myoptstrcmp(fun,"WRITE")==0){ - my_mexInputArray2Buff(2,&con[con_index].write); - if(IS_STATUS_TCP_CONNECTED(con[con_index].status)) - writedata(); - return; + if(myoptstrcmp(fun,"WRITE")==0) { + my_mexInputArray2Buff(2,&con[con_index].write); + if(IS_STATUS_TCP_CONNECTED(con[con_index].status)) + writedata(); + return; } - if(myoptstrcmp(fun,"PRINTF")==0){ - mxArray *plhs[1]={NULL}; - if(gnrhs<3) return; - mexCallMATLAB(1,plhs, gnrhs-2, (mxArray **)&(gprhs[2]),"sprintf"); - gprhs=(const mxArray **)plhs; gnrhs=1; // HACK: Move return arg from sprintf to input arg of this mex. - my_mexInputArray2Buff(0,&con[con_index].write); - if(IS_STATUS_TCP_CONNECTED(con[con_index].status)) - writedata(); - return; + if(myoptstrcmp(fun,"PRINTF")==0) { + mxArray *plhs[1]= {NULL}; + if(gnrhs<3) return; + mexCallMATLAB(1,plhs, gnrhs-2, (mxArray **)&(gprhs[2]),"sprintf"); + gprhs=(const mxArray **)plhs; + gnrhs=1; // HACK: Move return arg from sprintf to input arg of this mex. + my_mexInputArray2Buff(0,&con[con_index].write); + if(IS_STATUS_TCP_CONNECTED(con[con_index].status)) + writedata(); + return; } - if(myoptstrcmp(fun,"READ")==0){ - if(IS_STATUS_TCP_CONNECTED(con[con_index].status)) - readtype2buff( (int)my_mexInputSize(2),str2classid(my_mexFindInputString(2)),0,my_mexFindInputOption(2,"noblock")); - my_mexReturnArrayFromBuff(2,&con[con_index].read,0); - return; + if(myoptstrcmp(fun,"READ")==0) { + if(IS_STATUS_TCP_CONNECTED(con[con_index].status)) + readtype2buff( (int)my_mexInputSize(2),str2classid(my_mexFindInputString(2)),0,my_mexFindInputOption(2,"noblock")); + my_mexReturnArrayFromBuff(2,&con[con_index].read,0); + return; } - if(myoptstrcmp(fun,"READLINE")==0){ - // mexPrintf("DEBUG: READLINE....\n"); - if(IS_STATUS_TCP_CONNECTED(con[con_index].status)) - read2buff(my_mexInputSize(2),1,my_mexFindInputOption(2,"noblock")); - my_mexReturnArrayFromBuff(2,&con[con_index].read,1); - // mexPrintf("DEBUG: READLINE END\n"); - return; + if(myoptstrcmp(fun,"READLINE")==0) { + // mexPrintf("DEBUG: READLINE....\n"); + if(IS_STATUS_TCP_CONNECTED(con[con_index].status)) + read2buff(my_mexInputSize(2),1,my_mexFindInputOption(2,"noblock")); + my_mexReturnArrayFromBuff(2,&con[con_index].read,1); + // mexPrintf("DEBUG: READLINE END\n"); + return; } - if(myoptstrcmp(fun,"READTOFILE")==0){ - FILE *f=NULL; - const int blocklen=my_mexInputSize(2+1); - int readlen=blocklen; - int writelen=0; - if(IS_STATUS_TCP_CONNECTED(con[con_index].status)) - readlen= read2buff(blocklen,0,(int)my_mexFindInputOption(2+1,"noblock")); - if(readlen>0) - f=fopen(my_mexInputOptionString(2),(int)my_mexFindInputOption(2+1,"append")?"ab":"wb"); - if(f){ - writelen=fwrite(con[con_index].read.ptr,1,readlen,f); - fclose(f); - } - // Delete from read buffer if not "VIEW" option and dims filled - if(my_mexFindInputOption(2+1,"VIEW")==0 ){ - con[con_index].read.pos-=writelen; - memmove(con[con_index].read.ptr,&con[con_index].read.ptr[writelen],con[con_index].read.pos); - newbuffsize(&con[con_index].read,con[con_index].read.pos); - } - my_mexReturnValue(writelen); - return; + if(myoptstrcmp(fun,"READTOFILE")==0) { + FILE *f=NULL; + const int blocklen=my_mexInputSize(2+1); + int readlen=blocklen; + int writelen=0; + if(IS_STATUS_TCP_CONNECTED(con[con_index].status)) + readlen= read2buff(blocklen,0,(int)my_mexFindInputOption(2+1,"noblock")); + if(readlen>0) + f=fopen(my_mexInputOptionString(2),(int)my_mexFindInputOption(2+1,"append")?"ab":"wb"); + if(f) { + writelen=fwrite(con[con_index].read.ptr,1,readlen,f); + fclose(f); + } + // Delete from read buffer if not "VIEW" option and dims filled + if(my_mexFindInputOption(2+1,"VIEW")==0 ) { + con[con_index].read.pos-=writelen; + memmove(con[con_index].read.ptr,&con[con_index].read.ptr[writelen],con[con_index].read.pos); + newbuffsize(&con[con_index].read,con[con_index].read.pos); + } + my_mexReturnValue(writelen); + return; } - if(myoptstrcmp(fun,"WRITEFROMFILE")==0){ - int len=1024*1024*1024;int start=0; - FILE *f=fopen(my_mexInputOptionString(2),"rb"); - if(f==NULL) - my_mexReturnValue(-1); - if(my_mexIsInputArgOK(2+1)) - start=(int)my_mexInputScalar(2+1); - if(my_mexIsInputArgOK(2+2)) - len=(int)my_mexInputScalar(2+2); - else{ - fseek(f,0,SEEK_END); - len=ftell(f)-start; - } - fseek(f,start,SEEK_SET); - newbuffsize(&con[con_index].write,con[con_index].write.pos+len); - len=fread(&con[con_index].write.ptr[con[con_index].write.pos],1,len,f); - con[con_index].write.pos+=len; - fclose(f); - if(IS_STATUS_TCP_CONNECTED(con[con_index].status)) - writedata(); - if(len<0) - my_mexReturnValue(-1); - else - my_mexReturnValue(len); - return; + if(myoptstrcmp(fun,"WRITEFROMFILE")==0) { + int len=1024*1024*1024; + int start=0; + FILE *f=fopen(my_mexInputOptionString(2),"rb"); + if(f==NULL) + my_mexReturnValue(-1); + if(my_mexIsInputArgOK(2+1)) + start=(int)my_mexInputScalar(2+1); + if(my_mexIsInputArgOK(2+2)) + len=(int)my_mexInputScalar(2+2); + else { + fseek(f,0,SEEK_END); + len=ftell(f)-start; + } + fseek(f,start,SEEK_SET); + newbuffsize(&con[con_index].write,con[con_index].write.pos+len); + len=fread(&con[con_index].write.ptr[con[con_index].write.pos],1,len,f); + con[con_index].write.pos+=len; + fclose(f); + if(IS_STATUS_TCP_CONNECTED(con[con_index].status)) + writedata(); + if(len<0) + my_mexReturnValue(-1); + else + my_mexReturnValue(len); + return; } - if(myoptstrcmp(fun,"READPACKET")==0){ - con[con_index].read.pos=0; - my_mexReturnValue(readtype2buff(my_mexInputSize(2),str2classid(my_mexInputOptionString(2+1)),0, - my_mexFindInputOption(2+1,"noblock"))); - return; + if(myoptstrcmp(fun,"READPACKET")==0) { + con[con_index].read.pos=0; + my_mexReturnValue(readtype2buff(my_mexInputSize(2),str2classid(my_mexInputOptionString(2+1)),0, + my_mexFindInputOption(2+1,"noblock"))); + return; } - if(myoptstrcmp(fun,"WRITEPACKET")==0){ - if(IS_STATUS_UDP_NO_CON(con[con_index].status)) - ipv4_lookup(my_mexInputOptionString(2),my_mexInputScalar(3)); - my_mexReturnValue(writedata()); - con[con_index].write.pos=0; - return; + if(myoptstrcmp(fun,"WRITEPACKET")==0) { + if(IS_STATUS_UDP_NO_CON(con[con_index].status)) + ipv4_lookup(my_mexInputOptionString(2),my_mexInputScalar(3)); + my_mexReturnValue(writedata()); + con[con_index].write.pos=0; + return; } - if(myoptstrcmp(fun,"STATUS")==0){ - my_mexReturnValue(con[con_index].status); - return; + if(myoptstrcmp(fun,"STATUS")==0) { + my_mexReturnValue(con[con_index].status); + return; } - if(myoptstrcmp(fun,"GETHOST")==0){ - int n; - double ip_bytes[4] = {0, 0, 0, 0}; - const unsigned char *ipnr=(const unsigned char *)&con[con_index].remote_addr.sin_addr; - for(n=0;n<4;n++) - ip_bytes[n] = (double)ipnr[n]; - my_mexReturnMatrix(1,4,ip_bytes); - my_mexReturnValue((int)ntohs(con[con_index].remote_addr.sin_port)); - return; + if(myoptstrcmp(fun,"GETHOST")==0) { + int n; + double ip_bytes[4] = {0, 0, 0, 0}; + const unsigned char *ipnr=(const unsigned char *)&con[con_index].remote_addr.sin_addr; + for(n=0; n<4; n++) + ip_bytes[n] = (double)ipnr[n]; + my_mexReturnMatrix(1,4,ip_bytes); + my_mexReturnValue((int)ntohs(con[con_index].remote_addr.sin_port)); + return; } - if(myoptstrcmp(fun,"SETCALLBACK")==0){ - strncpy(con[con_index].callback, my_mexInputOptionString(2), CBNAMELEN); - return; + if(myoptstrcmp(fun,"SETCALLBACK")==0) { + strncpy(con[con_index].callback, my_mexInputOptionString(2), CBNAMELEN); + return; } - if(myoptstrcmp(fun,"GETCALLBACK")==0){ - my_mexReturnString(con[con_index].callback); - return; + if(myoptstrcmp(fun,"GETCALLBACK")==0) { + my_mexReturnString(con[con_index].callback); + return; } - if(myoptstrcmp(fun,"SETWRITETIMEOUT")==0){ - con[con_index].writetimeout=my_mexInputScalar(2); - return; + if(myoptstrcmp(fun,"SETWRITETIMEOUT")==0) { + con[con_index].writetimeout=my_mexInputScalar(2); + return; } - if(myoptstrcmp(fun,"SETREADTIMEOUT")==0){ - con[con_index].readtimeout=my_mexInputScalar(2); - return; + if(myoptstrcmp(fun,"SETREADTIMEOUT")==0) { + con[con_index].readtimeout=my_mexInputScalar(2); + return; } - if(myoptstrcmp(fun,"debug")==0){ - mexPrintf(" FID:%d\n",con[con_index].fid); - mexPrintf(" STATUS:%d\n",con[con_index].status); - mexPrintf("WRITE TO:%g\n",con[con_index].writetimeout); - mexPrintf("WRITE PTR:%x\n",(int)con[con_index].write.ptr); - mexPrintf("WRITE POS:%d\n",con[con_index].write.pos); - mexPrintf("WRITE LEN:%d\n",con[con_index].write.len); - mexPrintf("READ TO:%g\n",con[con_index].readtimeout); - mexPrintf("READ PTR:%x\n",(int)con[con_index].read.ptr); - mexPrintf("READ POS:%d\n",con[con_index].read.pos); - mexPrintf("READ LEN:%d\n",con[con_index].read.len); - return; + if(myoptstrcmp(fun,"debug")==0) { + mexPrintf(" FID:%d\n",con[con_index].fid); + mexPrintf(" STATUS:%d\n",con[con_index].status); + mexPrintf("WRITE TO:%g\n",con[con_index].writetimeout); + mexPrintf("WRITE PTR:%x\n",(int)con[con_index].write.ptr); + mexPrintf("WRITE POS:%d\n",con[con_index].write.pos); + mexPrintf("WRITE LEN:%d\n",con[con_index].write.len); + mexPrintf("READ TO:%g\n",con[con_index].readtimeout); + mexPrintf("READ PTR:%x\n",(int)con[con_index].read.ptr); + mexPrintf("READ POS:%d\n",con[con_index].read.pos); + mexPrintf("READ LEN:%d\n",con[con_index].read.len); + return; } mexErrMsgTxt("Unknown 'function name' in argument."); }