Sleipnir
src/seeknetwork.cpp
00001 #include "seeknetwork.h"
00002 
00003 namespace Sleipnir {
00004 
00005 //send message header:
00006 //byte #1-4:
00007 // size of one element of the data structure (in unsigned int)
00008 //byte #5-8:
00009 // (in unsigned int):
00010 // total number of elements to be sent, 1 if a single value, or else
00011 // the size of the array
00012 //byte #9-:
00013 // actual data
00014 
00015 int CSeekNetwork::Send(int new_fd, const string &str){
00016     char *s = (char*)malloc(2*sizeof(unsigned int));
00017     unsigned int *p = (unsigned int*) s;
00018     *p = sizeof(char); p++;
00019     *p = str.length()+1;
00020     if(CSeekNetwork::Send(new_fd, s, 2*sizeof(unsigned int))==-1){
00021         fprintf(stderr, "Bad 1\n");
00022         free(s);
00023         return -1;
00024     }
00025     free(s);
00026     
00027     char *c = (char*)malloc(str.length()+1);
00028     strcpy(c, str.c_str());
00029     if(CSeekNetwork::Send(new_fd, c, str.length()+1)==-1){
00030         fprintf(stderr, "Bad 2\n");
00031         free(c);
00032         return -1;
00033     }
00034     free(c);
00035     return 0;
00036 }
00037 
00038 int CSeekNetwork::Send(int new_fd, const vector<float> &f){
00039     char *s = (char*)malloc(2*sizeof(unsigned int));
00040     unsigned int *p = (unsigned int*) s;
00041     *p = sizeof(float); p++;
00042     *p = f.size();
00043     if(CSeekNetwork::Send(new_fd, s, 2*sizeof(unsigned int))==-1){
00044         fprintf(stderr, "Bad 1\n");
00045         free(s);
00046         return -1;
00047     }
00048     free(s);
00049     
00050     char *c = (char*)malloc(sizeof(float)*f.size());
00051     float *fp = (float*)c;
00052     int i;
00053     for(i=0; i<f.size(); i++){
00054         *fp = f[i];
00055         fp++;
00056     }
00057     if(CSeekNetwork::Send(new_fd, c, sizeof(float)*f.size())==-1){
00058         fprintf(stderr, "Bad 2\n");
00059         free(c);
00060         return -1;
00061     }
00062     free(c);
00063     return 0;
00064 }
00065 
00066 int CSeekNetwork::Send(int new_fd, char *c, int size){
00067     int tmp_size = size;
00068     int beg = 0;
00069 
00070     int r  = -1;
00071     while(1){
00072         char *p = (char*)malloc(tmp_size);
00073         Copy(p, c, beg, tmp_size);
00074         r = send(new_fd, p, tmp_size, 0);
00075         if(r==-1){
00076             fprintf(stderr, "client exits");
00077             break;
00078         }
00079         if(r==tmp_size){
00080             break;
00081         }
00082         tmp_size = size - tmp_size;
00083         beg = beg + r;
00084         free(p);
00085     }
00086 
00087     return r;
00088 }
00089 
00090 void CSeekNetwork::Clear(char *b, int size){
00091     int i = 0;
00092     for(i=0; i<size; i++){
00093         b[i] = '\0';
00094     }
00095 }
00096 
00097 int CSeekNetwork::Copy(char *d, char *s, int beg, int num){
00098     int i;
00099     for(i=0; i<num; i++){
00100         d[beg+i] = s[i];
00101     }
00102     return beg+num;
00103 }
00104 
00105 int CSeekNetwork::Receive(int new_fd, vector<float> &f){
00106     char *ar = (char*)malloc(4);
00107     char *p = &ar[0];
00108     int tmp_size = 4;
00109     int receive_size = -1;
00110     while(1){
00111         receive_size = recv(new_fd, p, tmp_size, 0);
00112         if(receive_size==tmp_size){
00113             break;
00114         }
00115         else if(receive_size==-1){
00116             fprintf(stderr, "client exits\n");
00117             break;
00118         }
00119         tmp_size = tmp_size - receive_size;
00120         p+=receive_size;
00121     }
00122     if(receive_size==-1)
00123         return -1;
00124 
00125     int *iP = (int*)ar;
00126     int length = *iP;
00127     f.resize(length);
00128 
00129     char *cStr = (char*)malloc(length*sizeof(float));
00130     tmp_size = length*sizeof(float);
00131     receive_size = -1;
00132     p = &cStr[0];
00133 
00134     while(1){
00135         receive_size = recv(new_fd, p, tmp_size, 0);
00136         if(receive_size==tmp_size){
00137             break;
00138         }
00139         else if(receive_size==-1){
00140             fprintf(stderr, "client exits\n");
00141             break;
00142         }
00143         tmp_size = tmp_size - receive_size;
00144         p+=receive_size;
00145     }
00146     if(receive_size==-1)
00147         return -1;
00148 
00149     int i;
00150     float *fp = (float*)cStr;
00151     for(i=0; i<length; i++){
00152         f[i] = *fp;
00153         fp++;
00154     }
00155     free(ar);
00156     free(cStr);
00157     return 0;
00158 }
00159 
00160 
00161 int CSeekNetwork::Receive(int new_fd, string &s){
00162     char *ar = (char*)malloc(4);
00163     char *p = &ar[0];
00164     int tmp_size = 4;
00165     int receive_size = -1;
00166     while(1){
00167         receive_size = recv(new_fd, p, tmp_size, 0);
00168         if(receive_size==tmp_size){
00169             break;
00170         }
00171         else if(receive_size==-1){
00172             fprintf(stderr, "client exits\n");
00173             break;
00174         }
00175         tmp_size = tmp_size - receive_size;
00176         p+=receive_size;
00177     }
00178 
00179     if(receive_size==-1){
00180         return -1;
00181     }
00182     int *iP = (int*)ar;
00183     int length = *iP;
00184     char *cStr = (char*)malloc(length);
00185     tmp_size = length;
00186     receive_size = -1;
00187     p = &cStr[0];
00188     
00189     while(1){
00190         receive_size = recv(new_fd, p, tmp_size, 0);
00191         if(receive_size==tmp_size){
00192             break;
00193         }
00194         else if(receive_size==-1){
00195             fprintf(stderr, "client exits\n");
00196             break;
00197         }
00198         tmp_size = tmp_size - receive_size;
00199         p+=receive_size;
00200     }
00201     if(receive_size==-1){
00202         return -1;
00203     }
00204 
00205     s = cStr; //copy result into string
00206     free(ar);
00207     free(cStr);
00208     return 0;
00209 }
00210 
00211 }
00212