KjClientConn.cpp

Go to the documentation of this file.
00001 
00014 #include "KjClientConn.h"
00015 
00016 
00017 KjClientConn::KjClientConn(KjSslServer *_pServer, pthread_t *_pThread)
00018 {
00019         m_pServer = _pServer;
00020         m_pSslConn = NULL;
00021         m_pThread = _pThread;
00022         m_bIsConnected = false;
00023         m_bIsLockNewThread = true;
00024         m_strReadMsg = "";
00025         m_strWriteMsg = "";
00026         m_bCanRead = false;
00027 }
00028 
00029 KjClientConn::~KjClientConn()
00030 {
00031 }
00032 
00035 void KjClientConn::DeleteEndMsg(string &_strMsg)
00036 {
00037         size_t iFound = _strMsg.find(KJ_END_MSG);
00038         if (iFound!=string::npos)
00039         {
00040                 string strEndMsg = KJ_END_MSG;
00041                 _strMsg.erase(iFound , strEndMsg.size());
00042         }
00043 }
00044 
00047 void KjClientConn::ParseClientRequest(string &_strMsg)
00048 {
00049         // Delete end message string if exists 
00050         DeleteEndMsg(_strMsg);
00051         // Set read message
00052         m_strReadMsg = _strMsg;
00053         m_bCanRead = true;
00054 }
00055 
00058 bool KjClientConn::ChatWithClient()
00059 {
00060         if(m_pSslConn)
00061         {
00062                 for(;;)
00063                 {
00064                         string strClientMsg;
00065                         string strServerMsg;
00066                         strClientMsg.clear();
00067                         strServerMsg.clear();
00068                         // Read message from client
00069                         for(;;) //do
00070                         {
00071                                 if(m_pSslConn)
00072                                 {
00073                                         string strPacket;
00074                                         strPacket.clear();
00075                                         // Server read packet from client
00076                                         m_pSslConn->Read(strPacket);
00077                                         if(strPacket.empty())
00078                                         {
00079                                                 cout << "Client is disconnected ... '" << endl;
00080                                                 return false;
00081                                         }
00082                                         strClientMsg += strPacket;
00083                                         if(strPacket.find(KJ_END_MSG) != string::npos)
00084                                         {
00085                                                 break;
00086                                         }
00087                                 }
00088                                 else
00089                                 {
00090                                         cerr << "Error:: void KjServer::ChatWithClient(KjSslConnection * pCon) -- pCon doesn`t allocated !!!" << endl;
00091                                         return false;
00092                                 }
00093                         }
00094                         
00095                         if(m_pSslConn)
00096                         {
00097                                 strServerMsg = strClientMsg;
00098                                 // Parse client request and get server message to client
00099                                 ParseClientRequest(strServerMsg);
00100                         }
00101                         else
00102                         {
00103                                 cerr << "Error:: void KjServer::ChatWithClient(KjSslConnection * pCon) -- pCon doesn`t allocated !!!" << endl;
00104                                 return false;
00105                         }
00106                 }
00107         }
00108         else
00109         {
00110                 cerr << "Error:: void KjServer::ChatWithClient(KjSslConnection * pCon) -- pCon doesn`t allocated !!!" << endl;
00111                 return false;
00112         }
00113         return true;
00114 }
00115 
00117 void KjClientConn::NewClient()
00118 {
00119         cout << "KjServerThread::NewClient()" << endl;
00120         m_bIsConnected = false;
00121         m_bIsLockNewThread = true;
00122         m_pSslConn = m_pServer->NextClient();
00123         if(m_pSslConn)
00124         {
00125                 // New client is connected
00126                 m_bIsConnected = true;
00127                 // Unlock new thread for new client connection
00128                 m_bIsLockNewThread = false;
00129                 m_iThreadId = pthread_self();
00130                 cout << "Thread id is " << m_iThreadId << endl;
00131                 cout << "m_pThread = " << m_pThread << endl;
00132                 cout << "m_pSslConn = " << m_pSslConn << endl;
00133                 // Chat with client
00134                 if(ChatWithClient() == false)
00135                 {
00136                         cout << "KjServerThread::NewClient -- Delete client ..." << endl;
00137                         m_pServer->DeleteClient(m_pSslConn);
00138                 }
00139         }
00140         else
00141         {
00142                 cerr << "Error:: void KjServer::NextClient() -- No connection!" << endl;
00143         }
00144 }
00145 
00148 string &KjClientConn::ReadMsg()
00149 {
00150         // Clear write message
00151         m_strWriteMsg.clear();
00152         m_bCanRead = false;
00153         return m_strReadMsg;
00154 }
00155 
00158 void KjClientConn::WriteMsg(const string &_strMsg)
00159 {
00160         m_strWriteMsg = _strMsg;
00161         if(m_pSslConn)
00162         {
00163                 // Write message to client
00164                 cout << "Server send message to client  '" << m_strWriteMsg << "'" << endl;
00165                 m_pSslConn->Write(m_strWriteMsg.c_str());
00166                 // Clear read message
00167                 m_strReadMsg.clear();
00168         }
00169         else
00170         {
00171                 cerr << "Error:: void KjClientConn::WriteMsg(const string &_strMsg) -- pCon doesn`t allocated !!!" << endl;
00172         }
00173 }
00174 
00175 
00176 

Generated on Tue Jul 24 10:03:07 2007 for KjSslServer by  doxygen 1.4.6