00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027 #include "ArExport.h"
00028 #include "ariaOSDef.h"
00029 #include "ArTcpConnection.h"
00030 #include "ArLog.h"
00031 #include "ariaUtil.h"
00032
00033 ArTcpConnection::ArTcpConnection()
00034 {
00035 myStatus = STATUS_NEVER_OPENED;
00036 buildStrMap();
00037 myOwnSocket = true;
00038 mySocket = new ArSocket();
00039 }
00040
00041 ArTcpConnection::~ArTcpConnection()
00042 {
00043 if (myOwnSocket)
00044 delete mySocket;
00045 }
00046
00053 void ArTcpConnection::setSocket(ArSocket *socket)
00054 {
00055 if (myOwnSocket)
00056 {
00057 delete mySocket;
00058 myOwnSocket = false;
00059 }
00060 mySocket = socket;
00061 }
00062
00063 ArSocket *ArTcpConnection::getSocket(void)
00064 {
00065 return mySocket;
00066 }
00067
00068 void ArTcpConnection::setStatus(int status)
00069 {
00070 myStatus = status;
00071 }
00072
00073 void ArTcpConnection::setPort(const char *host, int port)
00074 {
00075 myPortNum = port;
00076
00077 if (host == NULL)
00078 myHostName = "localhost";
00079 else
00080 myHostName = host;
00081 }
00082
00083 bool ArTcpConnection::openSimple(void)
00084 {
00085 if (internalOpen() == 0)
00086 return true;
00087 else
00088 return false;
00089 }
00090
00097 int ArTcpConnection::open(const char *host, int port)
00098 {
00099 setPort(host, port);
00100 return internalOpen();
00101 }
00102
00103 int ArTcpConnection::internalOpen(void)
00104 {
00105 mySocket->init();
00106 if (mySocket->connect(const_cast<char *>(myHostName.c_str()), myPortNum,
00107 ArSocket::TCP))
00108 {
00109 myStatus = STATUS_OPEN;
00110 mySocket->setNonBlock();
00111 return 0;
00112 }
00113
00114 myStatus = STATUS_OPEN_FAILED;
00115 switch(mySocket->getError())
00116 {
00117 case ArSocket::NetFail:
00118 return OPEN_NET_FAIL;
00119 case ArSocket::ConBadHost:
00120 return OPEN_BAD_HOST;
00121 case ArSocket::ConNoRoute:
00122 return OPEN_NO_ROUTE;
00123 case ArSocket::ConRefused:
00124 return OPEN_CON_REFUSED;
00125 case ArSocket::NoErr:
00126 ArLog::log(ArLog::Terse, "ArTcpConnection::open: No error!\n");
00127 default:
00128 return -1;
00129 }
00130
00131 }
00132
00133 void ArTcpConnection::buildStrMap(void)
00134 {
00135 myStrMap[OPEN_NET_FAIL] = "Network failed.";
00136 myStrMap[OPEN_BAD_HOST] = "Could not find host.";
00137 myStrMap[OPEN_NO_ROUTE] = "No route to host.";
00138 myStrMap[OPEN_CON_REFUSED] = "Connection refused.";
00139 }
00140
00141 const char *ArTcpConnection::getOpenMessage(int messageNumber)
00142 {
00143 return myStrMap[messageNumber].c_str();
00144 }
00145
00146 bool ArTcpConnection::close(void)
00147 {
00148 myStatus = STATUS_CLOSED_NORMALLY;
00149 return mySocket->close();
00150 }
00151
00152 int ArTcpConnection::read(const char *data, unsigned int size,
00153 unsigned int msWait)
00154 {
00155 ArTime timeDone;
00156 unsigned int bytesRead = 0;
00157 int n;
00158
00159 if (getStatus() != STATUS_OPEN)
00160 {
00161 ArLog::log(ArLog::Terse,
00162 "ArTcpConnection::read: Attempt to use port that is not open.");
00163 return -1;
00164 }
00165
00166 int timeToWait;
00167 timeDone.setToNow();
00168 timeDone.addMSec(msWait);
00169
00170 do
00171 {
00172 timeToWait = timeDone.mSecTo();
00173 if (timeToWait < 0)
00174 timeToWait = 0;
00175 n = mySocket->read(const_cast<char *>(data) + bytesRead, size - bytesRead,
00176 timeToWait);
00177
00178
00179
00180
00181
00182
00183 if (n != -1)
00184 bytesRead += n;
00185 if (bytesRead >= size)
00186 return bytesRead;
00187 } while (timeDone.mSecTo() >= 0);
00188
00189 return bytesRead;
00190 }
00191
00192 int ArTcpConnection::write(const char *data, unsigned int size)
00193 {
00194 int ret;
00195
00196 if (getStatus() != STATUS_OPEN)
00197 {
00198 ArLog::log(ArLog::Terse,
00199 "ArTcpConnection::write: Attempt to use port that is not open.");
00200 return -1;
00201 }
00202 if ((ret = mySocket->write(data, size)) != -1)
00203 return ret;
00204
00205 ArLog::log(ArLog::Terse, "ArTcpConnection::write: Write failed, closing connection.");
00206 close();
00207 return -1;
00208 }
00209
00210
00215 std::string ArTcpConnection::getHost(void)
00216 {
00217 return myHostName;
00218 }
00219
00224 int ArTcpConnection::getPort(void)
00225 {
00226 return myPortNum;
00227 }
00228
00229 int ArTcpConnection::getStatus(void)
00230 {
00231 return myStatus;
00232 }
00233
00234 bool ArTcpConnection::isTimeStamping(void)
00235 {
00236 return false;
00237 }
00238
00239 ArTime ArTcpConnection::getTimeRead(int index)
00240 {
00241 ArTime now;
00242 now.setToNow();
00243 return now;
00244 }