00001
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034 #include "KjPerlServer.h"
00035
00036 KjPerlServer::KjPerlServer()
00037 {
00038 cout << "KjPerlServer::KjPerlServer()" << endl;
00039 m_pServer = NULL;
00040 m_strReadMsg = "";
00041 m_strWriteMsg = "";
00042 }
00043
00044 KjPerlServer::~KjPerlServer()
00045 {
00046 cout << "KjPerlServer::~KjPerlServer()" << endl;
00047 }
00048
00051 bool KjPerlServer::CanRead(const int _iClientId)
00052 {
00053 if(m_pServer)
00054 {
00055 KjClientConn *pClient = m_pServer->GetClient(_iClientId);
00056 if(pClient)
00057 {
00058 if(pClient->GetConn())
00059 {
00060 if(pClient->GetConn()->GetIsValid() == true)
00061 {
00062 if(pClient->GetCanRead())
00063 {
00064 return true;
00065 }
00066 }
00067 }
00068 }
00069 }
00070 return false;
00071 }
00072
00074 const char *KjPerlServer::Read(const int _iClientId)
00075 {
00076 if(m_pServer)
00077 {
00078 KjClientConn *pClient = m_pServer->GetClient(_iClientId);
00079 if(pClient)
00080 {
00081 if(pClient->GetConn())
00082 {
00083 if(pClient->GetConn()->GetIsValid() == true)
00084 {
00085 m_strReadMsg = pClient->ReadMsg();
00086 return m_strReadMsg.c_str();
00087 }
00088 }
00089 }
00090 }
00091 m_strReadMsg = "Error: Client is disconnected !!!";
00092 return m_strReadMsg.c_str();
00093 }
00094
00096 void KjPerlServer::Write(const int _iClientId, const char* _strMsg)
00097 {
00098 if(m_pServer)
00099 {
00100 KjClientConn *pClient = m_pServer->GetClient(_iClientId);
00101 if(pClient)
00102 {
00103 if(pClient->GetConn())
00104 {
00105 if(pClient->GetConn()->GetIsValid() == true)
00106 {
00107 m_strWriteMsg = _strMsg;
00108 pClient->WriteMsg(m_strWriteMsg);
00109 return;
00110 }
00111 }
00112 }
00113 }
00114 cout << "Client id " << _iClientId << " is disconnected !!!" << endl;;
00115 }
00116
00118 int KjPerlServer::GetNumClients()
00119 {
00120 return m_pServer->GetNumClients();
00121 }
00122
00127 void KjPerlServer::Connect(char* _strConn, const char* _strCertFile,const char* _strKeyFile)
00128 {
00129 cout << "KjPerlServer::Connect()" << endl;
00130 m_pServer = new KjServerThread(_strConn, _strCertFile, _strKeyFile);
00131 }
00132
00133 int KjPerlServer::Start()
00134 {
00135 cout << "int KjPerlServer::Start()" << endl;
00136 cout << "this = " << this << endl;
00137 cout << "m_pServer = " << m_pServer << endl;
00138 if(m_pServer)
00139 {
00140 if(!m_pServer->Start())
00141 {
00142 if(m_pServer)
00143 {
00144 delete m_pServer;
00145 m_pServer = NULL;
00146 }
00147 }
00148 }
00149 return 0;
00150 }
00151