The Machine Perception Toolbox

[Introduction]- [News]- [Download]- [Screenshots]- [Manual (pdf)]- [Forums]- [API Reference]- [Repository ]

 

Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Namespace Members | Class Members | File Members | Related Pages

socketio.cpp

Go to the documentation of this file.
00001 /*
00002  * socketio.cpp
00003  *
00004  * Copyright (c) 2003 Machine Perception Laboratory
00005  * University of California San Diego.
00006  * Please read the disclaimer and notes about redistribution
00007  * at the end of this file.
00008  *
00009  * Authors: Josh Susskind
00010  */
00011 
00012 #include <winsock.h>
00013 #include "io.h"
00014 
00015 #include "../common.h"
00016 
00017 static SOCKET clientSock;
00018 
00019 #define VERIFY(x) if ( (x) == SOCKET_ERROR ) \
00020     {_isConnect = FALSE;}
00021 
00022 // ================================================================
00023 
00024 MPSocketIO::MPSocketIO(char _hostName[],int _hostPort,int _isConvert)
00025 {
00026         //hostName = new char[strlen(_hostName)+1];
00027         strcpy(hostName,_hostName);
00028         hostPort = _hostPort;
00029         isConvert = _isConvert;
00030         _isConnect = FALSE;
00031 }
00032 
00033 // ================================================================
00034 
00035 MPSocketIO::~MPSocketIO()
00036 {
00037         disconnectServer();
00038 }
00039 
00040 // ================================================================
00041 
00042 BOOL MPSocketIO::isConnect()
00043 {
00044         return _isConnect;
00045 }
00046 
00047 // ================================================================
00048 
00049 BOOL MPSocketIO::connectServer(void)
00050 {
00051         if((clientSock = socket(AF_INET,SOCK_STREAM,0)) < 0 ) { _isConnect = FALSE; return FALSE; }
00052         
00053         sockaddr_in cin;
00054         cin.sin_addr.s_addr = inet_addr(hostName);
00055         cin.sin_family = AF_INET;
00056         cin.sin_port= htons(hostPort);
00057         
00058         if(connect(clientSock,(struct sockaddr*)&(cin),sizeof(struct sockaddr)) < 0) {
00059                 closesocket(clientSock);
00060                 clientSock = -1;
00061                 _isConnect = FALSE;
00062         }
00063         else {
00064                 _isConnect = TRUE;
00065         }
00066         return isConnect();
00067 }
00068 
00069 // ================================================================
00070 
00071 void MPSocketIO::disconnectServer()
00072 {
00073         if(isConnect()) closesocket(clientSock);
00074         _isConnect = FALSE;
00075         clientSock = -1;
00076 }
00077 
00078 // ================================================================
00079 
00080 void MPSocketIO::writeConvert(char byte[],int numByte)
00081 {
00082         if(sizeof(byte) < numByte) return;
00083         for(int i=numByte-1;i>0;i--)
00084         {
00085                 VERIFY(send(clientSock,&byte[i],1,0));
00086         }
00087 }
00088 
00089 // ================================================================
00090 
00091 void MPSocketIO::writeByte(const char &b)
00092 {
00093         if(isConnect()) {
00094                 VERIFY(send(clientSock,&b,1,0));
00095         }
00096 }
00097 
00098 // ================================================================
00099 
00100 void MPSocketIO::write(const int &i)
00101 {
00102         if(isConnect()) {
00103                 int_data.data = i;
00104                 if(isConvert) {
00105                         writeConvert(int_data.byte,4);
00106                 } else {
00107                         VERIFY(send(clientSock,int_data.byte,4,0));
00108                 }
00109         }
00110 }
00111 
00112 // ================================================================
00113 
00114 void MPSocketIO::write(const long &l)
00115 {
00116         if(isConnect()) {
00117                 long_data.data = l;
00118                 if(isConvert) {
00119                         writeConvert(long_data.byte,8);
00120                 } else {
00121                         VERIFY(send(clientSock,long_data.byte,8,0));
00122                 }
00123         }
00124 }
00125 
00126 // ================================================================
00127 
00128 void MPSocketIO::write(const char &ch)
00129 {
00130         if(isConnect()) {
00131                 VERIFY(send(clientSock,&ch,1,0));
00132         }
00133 }
00134 
00135 // ================================================================
00136 
00137 void MPSocketIO::write(const char *s)
00138 {
00139         if(clientSock>0) {
00140                 VERIFY(send(clientSock,s,strlen(s),0));
00141         }
00142 }
00143 
00144 // ================================================================
00145 
00146 void MPSocketIO::write(const float &f)
00147 {
00148         
00149         if(isConnect()) {
00150                 float_data.data = f;
00151                 if(isConvert) {
00152                         writeConvert(float_data.byte,4);
00153                 } else {
00154                         VERIFY(send(clientSock,float_data.byte,4,0));
00155                 }
00156         }
00157 }
00158 
00159 // ================================================================
00160 
00161 void MPSocketIO::write(const double &d)
00162 {
00163         if(isConnect()) {
00164                 double_data.data = d;
00165                 if(isConvert) {
00166                         writeConvert(double_data.byte,8);
00167                 } else {
00168                         VERIFY(send(clientSock,double_data.byte,8,0));
00169                 }
00170         }
00171 }
00172 
00173 // ================================================================
00174 
00175 char MPSocketIO::readByte()
00176 {
00177         char ch;
00178         readFull(&ch,1);
00179         return ch;
00180 }
00181 
00182 // ================================================================
00183 
00184 int MPSocketIO::readInt()
00185 {
00186         if(isConvert) {
00187                 readConvert(int_data.byte,4);
00188         } else {
00189                 readFull(int_data.byte,4);
00190         }
00191         return int_data.data;
00192 }
00193 
00194 // ================================================================
00195 
00196 long MPSocketIO::readLong()
00197 {
00198         if(isConvert) {
00199                 readConvert(long_data.byte,8);
00200         } else {
00201                 readFull(long_data.byte,8);
00202         }
00203         return long_data.data;
00204 }
00205 
00206 // ================================================================
00207 
00208 float MPSocketIO::readFloat()
00209 {
00210         if(isConvert) {
00211                 readConvert(float_data.byte,4);
00212         } else {
00213                 readFull(float_data.byte,4);
00214         }
00215         return float_data.data;
00216 }
00217 
00218 // ================================================================
00219 
00220 double MPSocketIO::readDouble()
00221 {
00222         if(isConvert) {
00223                 readConvert(double_data.byte,8);
00224         } else {
00225                 readFull(double_data.byte,8);
00226         }
00227         return double_data.data;
00228 }
00229 
00230 // ================================================================
00231 
00232 int MPSocketIO::readConvert(char byte[],int numRead)
00233 {
00234         int i , readbyte;
00235         if(isConnect()) {
00236                 if(sizeof(byte) < numRead) return -1;
00237                 for(i=numRead-1;i>=0;i--)
00238                 {
00239                         readbyte = recv(clientSock,&byte[i],1,0);
00240                         if(readbyte < 0) return -1;
00241                 }
00242                 return numRead;
00243         }
00244         return -1;
00245 }
00246 
00247 // ================================================================
00248 
00249 int MPSocketIO::readFull(char byte[],int numRead)
00250 {
00251         int i , readbyte;
00252         if(isConnect()) {
00253                 if(sizeof(byte) < numRead) return -1;
00254                 for(i=0;i<numRead;i++)
00255                 {
00256                         readbyte = recv(clientSock,&byte[i],1,0);
00257                         if(readbyte < 0) { return -1; }
00258                 }
00259                 return numRead;
00260         }
00261         return -1;
00262 }
00263 
00264 // ================================================================
00265 
00266 /*
00267  * 
00268  * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
00269  * 
00270  *    1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
00271  *    2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
00272  *    3. The name of the author may not be used to endorse or promote products derived from this software without specific prior written permission.
00273  * 
00274  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00275  * 
00276  */

Generated on Mon Nov 8 17:07:52 2004 for MPT by  doxygen 1.3.9.1