00001
00002 #include "remotesocket.h"
00003
00004 #ifndef _SENDRECVSTRUCT_H
00005 #define _SENDRECVSTRUCT_H
00006 #define PRINT_RECV_STATUS ANIDEBUG
00007 #ifndef PRINT_RECV_STATUS
00008 #define PRINT_RECV_STATUS
00009 #endif
00010
00011 template <class myStruct>
00012 inline int RemoteSocket::SendStruct(myStruct *S, Semaphore *sem)
00013 {
00014 unsigned char *data;
00015 int totsize = 0;
00016
00017 if (!S)
00018 {
00019 cout << "SendStruct: Error! Custom structure empty!!" << endl << flush;
00020 SendVar<int>(-1);
00021 return -1;
00022 }
00023 if (sem) sem->Wait();
00024 int ptr = packStruct(S, &data, totsize);
00025 if (sem) sem->Post();
00026 #ifdef ANIDEBUG
00027 cout << "RemoteSocket::SendStruct: Sending " << totsize << " bytes." << endl << flush;
00028 #endif
00029
00030 if (ptr != totsize)
00031 {
00032 cout << "packStruct(): Error!! [ptr -> " << ptr << " != totsize -> " << totsize << "]" << endl << flush;
00033 exit(-1);
00034 }
00035
00036 SendVar<int>(totsize);
00037 SendBytes(totsize, data);
00038
00039 FREE1D(&data, totsize);
00040 return totsize;
00041 }
00042
00043 template <class myStruct>
00044 inline double RemoteSocket::RecvStruct(myStruct *S, Semaphore *sem)
00045 {
00046 int time1 = myTimer();
00047 double trans_rate;
00048 unsigned char *data;
00049 int size = RecvVar<int>();
00050 if (size == 0 || size == -1)
00051 {
00052 cout << "RemoteSocket::RecvStruct: Received empty structure!!" << endl << flush;
00053 return 0.0;
00054 }
00055
00056 ALLOC1D(&data, size);
00057 int t1 = myTimer();
00058 RecvBytes(size, data);
00059
00060 int tot_time = myTimer()-t1+1;
00061 trans_rate = 8.0*size/tot_time/1000.0;
00062
00063 if (print_flag)
00064 {
00065 cout.precision(3);
00066 cout << endl << "RemoteSocket::RecvStruct: " << size << " bytes in " << tot_time << " ms [" << trans_rate << " Mb/sec] (Tot: " << flush;
00067 }
00068
00069 if (sem) sem->Wait();
00070 int ptr = unpackStruct(S, data, size);
00071 if (sem) sem->Post();
00072
00073 if (ptr != size)
00074 {
00075 cout << "unpackStruct(): Error!! [ptr -> " << ptr << " != totsize -> " << size << "]" << endl << flush;
00076 exit(-1);
00077 }
00078
00079 if (print_flag)
00080 {
00081 cout << myTimer()-time1 << " ms)" << endl << endl << flush;
00082 }
00083
00084 FREE1D(&data, size);
00085
00086 return trans_rate;
00087 }
00088
00089 #endif // _SENDRECVSTRUCT_H
00090