Guest User

Untitled

a guest
Jan 4th, 2018
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 21.54 KB | None | 0 0
  1. ********************************************************************************************
  2. //Socket initialization, connection (send) , thread  pour receive , send pour envoyer une commande, typedef (function pointer)
  3.  
  4. #ifndef Connection_h__
  5. #define Connection_h__
  6.  
  7. #include <winsock2.h>
  8. #include <ws2tcpip.h>
  9. #include "IncWindow.h"
  10. #include "UserCommand.h"
  11.  
  12. class Server;
  13.  
  14. #pragma comment(lib, "ws2_32.lib")
  15.  
  16. enum ConnectionState
  17. {
  18.     CS_STARTED=0,
  19.     CS_ERROR_STARTING,
  20.     CS_ERROR_SENDING,
  21.     CS_ERROR_RECEIVING,
  22.     CS_ERROR_CLOSING,
  23.     CS_CLOSED,
  24. };
  25.  
  26. class Connection
  27. {
  28. public :   
  29.     static ConnectionState Begin(Server& serv);
  30.     static ConnectionState Close();
  31.     static void SendUserCommand(UserCommand cmd);
  32.  
  33. private :
  34.     static DWORD WINAPI Run(LPVOID lpParam);
  35.  
  36. private :
  37. static WSADATA mWSAData;
  38. static SOCKET mSock;
  39. static SOCKADDR_IN mSin;
  40. static HANDLE mRecvThread;
  41. static bool mRunning;
  42. };
  43.  
  44. #endif // Connexion_h__
  45. ********************************************************************************************
  46. #include "Connection.h"
  47. #include "IRCSession.h"
  48. #include "Server.h"
  49. #include "ServerCommand.h"
  50. #include <memory>
  51. WSADATA Connection::mWSAData;
  52. SOCKET Connection::mSock;
  53. SOCKADDR_IN Connection::mSin;
  54. HANDLE Connection::mRecvThread;
  55.  
  56. bool Connection::mRunning = false;
  57.  
  58. ConnectionState Connection::Begin(Server& serv)
  59. {
  60.     WSAStartup(MAKEWORD(2,0), &mWSAData);  
  61.     hostent* remotHost = gethostbyname(serv.GetHost().c_str());
  62.     if(remotHost == NULL) return CS_ERROR_STARTING;
  63.  
  64.     in_addr addr;
  65.     addr.s_addr =  *(u_long *)remotHost->h_addr_list[0];
  66.     mSin.sin_addr.s_addr = addr.s_addr;  
  67.     mSin.sin_family     = AF_INET;
  68.     mSin.sin_port       = htons(u_short(serv.GetPort()));
  69.     mSock = socket(AF_INET,SOCK_STREAM,IPPROTO_TCP);
  70.     if(mSock == INVALID_SOCKET)
  71.     {      
  72.         return CS_ERROR_STARTING;
  73.     }  
  74.     if(connect(mSock,(SOCKADDR*)&mSin,sizeof(mSin)) == SOCKET_ERROR)
  75.     {
  76.         printf("Error in connection : %d",WSAGetLastError());
  77.         return CS_ERROR_STARTING;
  78.     }
  79.     mRunning = true;
  80.     mRecvThread = CreateThread(NULL,0,(LPTHREAD_START_ROUTINE)Connection::Run,0,0,NULL);
  81.     if(mRecvThread == NULL)
  82.     {
  83.         return CS_ERROR_STARTING;
  84.     }
  85.     return CS_STARTED;
  86. }
  87.  
  88. ConnectionState Connection::Close()
  89. {
  90.     mRunning = false;
  91.     if(mSock)
  92.     {  
  93.     if(shutdown(mSock, SD_BOTH)) return CS_ERROR_CLOSING;
  94.     if(closesocket(mSock)) return CS_ERROR_CLOSING;
  95.     }
  96.     WSACleanup();  
  97.     if(!CloseHandle(mRecvThread)) return CS_ERROR_CLOSING; 
  98.    
  99.     return CS_CLOSED;
  100. }
  101.  
  102. void Connection::SendUserCommand(UserCommand cmd)
  103. {
  104.     char buffer[1024];
  105.     size_t dataLenght = cmd.ToString().length();
  106.     memcpy(buffer,cmd.ToString().c_str(),dataLenght);  
  107.     buffer[dataLenght] = 0x0D;
  108.     buffer[dataLenght + 1] = 0x0A;
  109.     int result = send(mSock,buffer,dataLenght + 2,0);
  110.     if(result == SOCKET_ERROR)
  111.     {  
  112.  
  113.         Close();
  114.     }
  115. }
  116.  
  117. DWORD WINAPI Connection::Run(LPVOID lpParam)
  118. {
  119.     char buffer[4096];
  120.     //Main receive loop
  121.     while(mRunning)
  122.     {
  123.         int rec = recv(mSock,buffer,sizeof(buffer),0);
  124.         if(rec > 0)
  125.         {
  126.         unsigned int i = 0;    
  127.         unsigned int crlfOffset=0;
  128.         while( i < rec - 1)
  129.         {
  130.             if(buffer[i] == 0xD  && buffer[i+1] == 0x0A)
  131.             {
  132.                
  133.                 buffer[i++]='\0';
  134.                 buffer[i] = '\0';
  135.                 ServerCommand serv;    
  136.                 serv.Parse(buffer + crlfOffset);
  137.                 IRCSession::OnReceiveCommand(serv);
  138.                 crlfOffset = i+1;
  139.             }
  140.             i++;
  141.         }  
  142.         }
  143.                
  144.         Sleep(100);
  145.     }
  146.     return 0;
  147. }
  148. ********************************************************************************************
  149. #ifndef Event_h__
  150. #define Event_h__
  151. #include "EventType.h"
  152. struct Event
  153. {
  154. public:
  155.     Event(EventType type = EVT_MAX) : mType(type){}
  156.     virtual ~Event() {};
  157. private:
  158.  
  159. public:
  160.     EventType mType;
  161. };
  162. #endif // Event_h__
  163. ********************************************************************************************
  164. #ifndef EventPing_h__
  165. #define EventPing_h__
  166. #include "Event.h"
  167. #include "Ping.h"
  168.  
  169. struct EventPing : Event
  170. {
  171. public :
  172.     EventPing(Ping p) : Event(EVT_RCV_PING) , mPing(p){}
  173.     EventPing(string serverCode): Event(EVT_RCV_PING){mPing.mServerCode = serverCode;}
  174.     virtual ~EventPing(){}
  175. public:
  176.     Ping mPing;
  177. };
  178. #endif // EventPing_h__
  179. ********************************************************************************************
  180. #ifndef EventStartSession_h__
  181. #define EventStartSession_h__
  182. #include "Event.h"
  183. class User;
  184. class Server;
  185.  
  186. struct EventStartSession : Event
  187. {
  188. public:
  189.     EventStartSession(User user , Server serv) : Event(EVT_START_SESSION), mUser(user), mServer(serv) {}
  190.     virtual ~EventStartSession() {}
  191. public:
  192.     User mUser;
  193.     Server mServer;
  194. };
  195. #endif // EventStartSession_h__
  196. ********************************************************************************************
  197. #ifndef EventType_h__
  198. #define EventType_h__
  199. enum EventType
  200. {  
  201. EVT_START_SESSION=0,
  202. EVT_RCV_PING,
  203. EVT_RCV_PRIVMSG,
  204. EVT_MAX,
  205. };
  206. #endif // EventType_h__
  207. ********************************************************************************************
  208. #ifndef FastNumeric_h__
  209. #define FastNumeric_h__
  210. #include <cctype>
  211. #include <string>
  212. inline bool isNumeric(const char* val)
  213. {
  214.     if(strlen(val) < 1)
  215.     {
  216.     return false;
  217.     }
  218.  
  219.     bool result = true;        
  220.     for(char* p = (char*)val; *p ; p++){result += (bool)(isdigit(*p));}
  221.     return result;
  222. }
  223.  
  224. #endif // FastNumeric_h__
  225. ********************************************************************************************
  226. #ifndef FastString_h__
  227. #define FastString_h__
  228. #include "SafeDelete.h"
  229. #include <string>
  230. //fast string manipulations
  231.  
  232. unsigned int GetWordCount(const char* s)
  233. {
  234.     unsigned int i =0;
  235.     unsigned int result=0;
  236.     while(i < strlen(s))
  237.     {
  238.         if(s[i] == ' ')
  239.         {
  240.             result++;
  241.         }
  242.         i++;
  243.     }
  244.     return result + 1;
  245. }
  246.  
  247. unsigned int GetWordOffset(const char* s,unsigned int position)
  248. {
  249.     unsigned int offset = 0;
  250.     unsigned int i =0;
  251.     bool found = false;
  252.  
  253.     while(i < strlen(s) && !found && position >= 1)
  254.     {
  255.         if(s[i] == ' ')
  256.         {
  257.             if(position == 0)
  258.             {
  259.                 found = true;
  260.             }
  261.             position--;
  262.         }
  263.         offset++;
  264.         i++;
  265.     }
  266.     return offset;
  267. }
  268.  
  269. unsigned int GetWordLenght(const char* s,unsigned int position)
  270. {
  271.     unsigned int offset = GetWordOffset(s,position);
  272.     unsigned int i =0;
  273.     while((offset +i) < strlen(s) && s[offset+i] != ' ')
  274.     {
  275.         i++;
  276.     }
  277.     return i;
  278. }
  279.  
  280. char** GetWords(const char* s)
  281. {
  282.     unsigned int wordCount = GetWordCount(s);
  283.     char** result = new char*[wordCount];  
  284.     for(unsigned int i=0 ; i < wordCount ; i++)
  285.     {
  286.         unsigned int lenght = GetWordLenght(s,i);
  287.         result[i] = new char[lenght+1];
  288.         const char* word = (s+GetWordOffset(s,i));
  289.         memcpy(result[i],word,lenght);
  290.         result[i][lenght]= '\0';
  291.     }
  292.     return result;
  293. }
  294.  
  295. void DeleteWords(char** words,unsigned int count)
  296. {
  297.     SAFE_DELETE_ARRAY_2D(words,count);
  298. }
  299.  
  300. #endif // FastString_h__
  301. ********************************************************************************************
  302. #ifndef Queue_h__
  303. #define Queue_h__
  304. #include <queue>
  305. using namespace std;
  306. #endif // Queue_h__
  307. ********************************************************************************************
  308.  
  309. #ifndef Session_h__
  310. #define Session_h__
  311. #include "IncWindow.h"
  312. #include "IncQueue.h"
  313. #include "SharedPointer.h"
  314. class ServerCommand;
  315. class Server;
  316. class User;
  317. struct Event;
  318.  
  319. typedef void (*PtrReceiveMessage)(const char* message);
  320.  
  321. class IRCSession
  322. {
  323. public :
  324.     static void OnReceiveCommand(ServerCommand cmd);   
  325.  
  326.     static void HandleChatBoxMssg(const char* msg);
  327.  
  328.     static void SetReceiveMessageFunction(PtrReceiveMessage fct);
  329.    
  330.     static void HandleEvent(SharedPointer<Event> evt);
  331.  
  332. private :
  333.     static DWORD WINAPI Run(LPVOID lpParam);
  334.  
  335. private :
  336.     static Server mServer;
  337.     static User mUser;
  338.     static PtrReceiveMessage mRcvMsgFct;
  339.     static HANDLE mRunThread;
  340.     static bool mRunning;
  341.     static CRITICAL_SECTION mSection;
  342.     static queue<SharedPointer<Event>> mEventQueue;
  343. };
  344.  
  345.  
  346. #endif // Session_h__
  347.  
  348. ********************************************************************************************
  349. #ifndef IncString_h__
  350. #define IncString_h__
  351. #include <string>
  352. using namespace std;
  353. #endif // IncString_h__
  354.  
  355. ********************************************************************************************
  356. #ifndef IncVector_h__
  357. #define IncVector_h__
  358. #include <vector>
  359. using namespace std;
  360. #endif // IncVector_h__
  361.  
  362. ********************************************************************************************
  363. #ifndef IncWindow_h__
  364. #define IncWindow_h__
  365. #ifndef WIN32_LEAN_AND_MEAN
  366. #define WIN32_LEAN_AND_MEAN
  367. #include <windows.h>
  368. #endif
  369. #endif // IncWindow_h__
  370. ********************************************************************************************
  371. #include "IRCSession.h"
  372. #include "FastString.h"
  373. #include "Connection.h"
  374. #include "ServerCommand.h"
  375. #include "Server.h"
  376. #include "User.h"
  377. #include "Event.h"
  378. #include "EventStartSession.h"
  379. #include "EventPing.h"
  380.  
  381. PtrReceiveMessage IRCSession::mRcvMsgFct;
  382. HANDLE IRCSession::mRunThread;
  383. bool IRCSession::mRunning;
  384. queue<SharedPointer<Event>> IRCSession::mEventQueue;
  385.  
  386. CRITICAL_SECTION IRCSession::mSection;
  387.  
  388.  void IRCSession::OnReceiveCommand(ServerCommand cmd)
  389. {
  390.     /*try
  391.     {
  392.         mRcvMsgFct(cmd.GetCommand().c_str());
  393.     }
  394.     catch (int e)
  395.     {
  396.         printf("error here");
  397.     }
  398.     */
  399.     if(cmd.GetCommand().compare("PING") ==0)
  400.     {
  401.         SharedPointer<EventPing> evt(new EventPing(cmd.GetFinalParameter()));
  402.         HandleEvent(evt);
  403.  
  404.     }
  405.     else if(cmd.GetCommand().compare("433")==0)
  406.     {
  407.         mRcvMsgFct("Error your nickname is already used");
  408.         //TODO : use alternative nick name or
  409.         //dc the guy
  410.     }
  411.     else if(cmd.GetCommand().compare("PRIVMSG") ==0)
  412.     {
  413.         mRcvMsgFct(("[IRC]" + cmd.GetSender() + " > " +cmd.GetFinalParameter()).c_str());
  414.     }
  415. }
  416.  
  417. DWORD WINAPI IRCSession::Run(LPVOID lpParam)
  418. {  
  419.     while(mRunning)
  420.     {
  421.         EnterCriticalSection(&mSection);
  422.         if(!mEventQueue.empty())
  423.         {      
  424.         SharedPointer<Event> evt = mEventQueue.front();
  425.  
  426.         switch(evt->mType)
  427.         {
  428.            
  429.         case EVT_START_SESSION:
  430.             {
  431.                 mRcvMsgFct("Starting IRC Session...");
  432.                 SharedPointer<EventStartSession> sEvt = evt;
  433.                 if(Connection::Begin(sEvt->mServer)== CS_STARTED)
  434.                 {
  435.                    
  436.                     auto nickCommand = sEvt->mUser.GenerateNickNameCommand();
  437.                     Connection::SendUserCommand(*nickCommand);
  438.                     auto registerCommand = sEvt->mUser.GenerateRegisterUserCommand();
  439.                     Connection::SendUserCommand(*registerCommand); 
  440.                     if(!sEvt->mUser.Password().empty())
  441.                     {
  442.                         auto identCommand = sEvt->mUser.GenerateIdentCommand();
  443.                         Connection::SendUserCommand(*identCommand);
  444.                         mRcvMsgFct("IRC Session started");
  445.                     }
  446.                 }  
  447.                 else
  448.                 {
  449.                     mRcvMsgFct("Error starting irc Session");
  450.                 }
  451.             }              
  452.             break; 
  453.         case EVT_RCV_PING:
  454.             SharedPointer<EventPing> pEvt =evt;
  455.             auto pongCommand = pEvt->mPing.GeneratePongCommand();
  456.             Connection::SendUserCommand(*pongCommand);
  457.             mRcvMsgFct(pongCommand->ToString().c_str());
  458.             break;
  459.         }
  460.         mEventQueue.pop();
  461.         }
  462.         LeaveCriticalSection(&mSection);
  463.         Sleep(100);
  464.     }  
  465.     return 0;
  466. }
  467.  
  468. void IRCSession::HandleEvent(SharedPointer<Event> evt)
  469. {  
  470.         EnterCriticalSection(&mSection);
  471.         mEventQueue.push(evt);
  472.         LeaveCriticalSection(&mSection);   
  473. }
  474.  
  475. void IRCSession::HandleChatBoxMssg(const char* msg)
  476. {      
  477.     char** words = GetWords(msg);
  478.     unsigned int wordCount = GetWordCount(msg);
  479.     if(wordCount >= 1)
  480.     {  
  481.         if(words[0][0] != '/')
  482.         {
  483.             DeleteWords(words,wordCount);
  484.             return; //it's not a command
  485.         }
  486.         char* cmd = words[0];
  487.         if(!strcmp(cmd,"/helpirc"))
  488.         {
  489.             mRcvMsgFct("Here is the full list of available IRC commands :");
  490.             mRcvMsgFct("/startirc 'ip' 'port' 'nickname' : start the irc");
  491.             mRcvMsgFct("/joinchannel 'name'");
  492.             mRcvMsgFct("/stopirc");
  493.         }
  494.         else if(!strcmp(cmd,"/startirc"))
  495.         {
  496.             if(wordCount >= 4)
  497.             {
  498.                 mRunning = true;
  499.                 mRunThread = CreateThread(NULL,0,(LPTHREAD_START_ROUTINE)Run,0,0,NULL);
  500.                 InitializeCriticalSection(&mSection);
  501.  
  502.                 Server server;
  503.                 server.SetHost(words[1]);
  504.                 server.SetPort(atoi(words[2]));
  505.                 User user;             
  506.                 user.UserName(words[3]);
  507.                 user.NickName(words[3]);               
  508.                 if(wordCount == 5)
  509.                 {
  510.                     user.Password(words[4]);
  511.                 }
  512.                 else
  513.                 {
  514.                     user.Password("");
  515.                 }
  516.                 SharedPointer<EventStartSession> evt(new EventStartSession(user,server));
  517.                 HandleEvent(evt);
  518.             }
  519.            
  520.             else
  521.             {
  522.  
  523.                 mRcvMsgFct("Wrong parameter count for /startirc");
  524.             }
  525.         }
  526.         else if(!strcmp(cmd,"/stopirc"))
  527.         {  
  528.             //should make an event to do : Connection::Close();    
  529.             Sleep(1000);           
  530.             mRunning = false;
  531.             CloseHandle(mRunThread);
  532.             DeleteCriticalSection(&mSection);
  533.             mRcvMsgFct("IRC Connection stopped");
  534.         }
  535.  
  536.     }
  537.     DeleteWords(words,wordCount);
  538. }
  539.  
  540. void IRCSession::SetReceiveMessageFunction(PtrReceiveMessage fct)
  541. {
  542.     mRcvMsgFct = fct;
  543. }
  544.  
  545. ********************************************************************************************
  546. #ifndef Ping_h__
  547. #define Ping_h__
  548. #include "UserCommand.h"
  549. #include "IncString.h"
  550. class Ping
  551. {
  552. public:
  553.     std::auto_ptr<UserCommand> GeneratePongCommand()
  554.     {
  555.         std::auto_ptr<UserCommand> cmd (new UserCommand("PONG"));      
  556.         cmd->FinalParameter(mServerCode);
  557.         return cmd;
  558.     }
  559. public:
  560.     string mServerCode;
  561.    
  562. };
  563. #endif // Ping_h__
  564.  
  565. ********************************************************************************************
  566. // #ifndef Null_h__
  567. #define Null_h__
  568.  
  569. #ifndef NULL
  570. # define NULL 0
  571. #endif
  572.  
  573. #define ISNULL(x) (x == NULL)
  574. #endif // Null_h__
  575.  
  576. ********************************************************************************************
  577. #ifndef UserCommand_h__
  578. #define UserCommand_h__
  579. #include "IncString.h"
  580. #include "IncVector.h"
  581.  
  582. class UserCommand
  583. {
  584. public :
  585.     UserCommand(string command)
  586.     {
  587.         mCommand=command;        
  588.     }
  589.     ~UserCommand()
  590.     {
  591.  
  592.     }
  593.     string GetCommand() const { return mCommand; }
  594.     void SetCommand(string val) { mCommand = val; }
  595.  
  596.     void AddParameter(string val)
  597.     {
  598.         mParameter.push_back(val);
  599.     }
  600.  
  601.     void DeleteParameter(unsigned int index)
  602.     {              
  603.         if(index < mParameter.size())
  604.         {
  605.             mParameter.erase(mParameter.begin()+index);
  606.         }      
  607.     }
  608.     string FinalParameter() const { return mFinalParameter; }
  609.     void FinalParameter(string val) { mFinalParameter = val; }
  610.  
  611.     string ToString()
  612.     {
  613.         string result = mCommand;          
  614.         for(int i = 0; i < mParameter.size();i++)
  615.         {
  616.             result.append(" ");
  617.             result.append(mParameter[i]);
  618.         }
  619.         if(mFinalParameter.size()>0)
  620.         {
  621.             result.append(" : ");
  622.             result.append(mFinalParameter);
  623.         }
  624.         return result;
  625.     }
  626. private:
  627.     string mCommand;   
  628.     vector<string> mParameter;
  629.     string mFinalParameter;
  630. };
  631. #endif // UserCommand_h__
  632.  
  633. ********************************************************************************************
  634. #ifndef RFCDefinition_h__
  635. #define RFCDefinition_h__
  636. #include <map>
  637. #include <string>
  638. using namespace std;
  639. /*
  640. map<int , string> gRFCNumbers;
  641. gRFCNumbers[1] = "";
  642. */
  643.  
  644. #endif // RFCDefinition_h__
  645.  
  646. ********************************************************************************************
  647. #ifndef SafeDelete_h__
  648. #define SafeDelete_h__
  649. #include "Null.h"
  650. #define SAFE_DELETE(x) if(x){ delete x; x = NULL; }
  651. #define SAFE_DELETE_2D(x, y) if(x){ for(unsigned int _i = 0; _i < y; ++_i){ delete x[_i]; } delete [] x; x = NULL; }
  652.  
  653. #define SAFE_DELETE_ARRAY(x) if(x){ delete [] x; x = NULL; }
  654. #define SAFE_DELETE_ARRAY_2D(x, y) if(x){ for(unsigned int _i = 0; _i < y; ++_i){ delete [] x[_i]; } delete [] x; x = NULL; }
  655. #endif // SafeDelete_h__
  656. ********************************************************************************************
  657. #ifndef Server_h__
  658. #define Server_h__
  659. #include "IncString.h"
  660.  
  661. class Server
  662. {
  663. public:
  664.     string GetHost() const { return mHost; }
  665.     void SetHost(string val) { mHost = val; }
  666.     int GetPort() const { return mPort; }
  667.     void SetPort(int val) { mPort = val; }
  668. private :
  669.     string mHost;  
  670.     int mPort;
  671.    
  672. };
  673. #endif // Server_h__
  674.  
  675. ********************************************************************************************
  676. #include "ServerCommand.h"
  677. ServerCommand::ServerCommand()
  678. {
  679.  
  680. }
  681.  
  682. ServerCommand::~ServerCommand()
  683. {
  684.  
  685. }
  686.  
  687. void ServerCommand::Parse(char* s)
  688. {
  689.     /*
  690.         <message> ::= [':' <préfixe> <espace> ] <command> <params> <crlf>
  691.         <préfixe> ::= <nom de serveur> | <pseudo> [ '!' <utilisateur> ] [ '@' <hôte> ]
  692.         <command> ::= <lettre> { <lettre> } | <nombre> <nombre> <nombre>
  693.         <espace> ::= ' ' { ' ' }
  694.         <params> ::= <espace> [ ':' <fin> | <milieu> <params> ]
  695.         <milieu> ::= <Toute séquence non vide d'octets à l'exclusion de ESPACE, NUL, CR, LF, le premier d'entre eux étant différent de ':'>
  696.         <fin> ::= <Toute suite, éventuellement vide, d'octets, à l'exception de NUL, CR et LF>
  697.         <crlf> ::= CR LF
  698.         */
  699.  
  700.         //calcul du sender
  701.         char* u = s;
  702.         if(s[0] == ':')
  703.         {          
  704.             while(*u && *u !=' ') u++;
  705.             *u++ = '\0';
  706.             string mSender = s+1;          
  707.         }
  708.         else
  709.         {
  710.             string mSender = "";
  711.         }
  712.        
  713.         //calcul de la commande
  714.         char* v = u;
  715.         while(*v && *v !=' ') v++;
  716.         *v++ = '\0';
  717.         mCommand = u;
  718.  
  719.         unsigned int paramsLength = 0;
  720.         unsigned int i = 0;
  721.         while(v[i] != ':' && i < strlen(v))
  722.         {
  723.             if(v[i] == ' ')
  724.             {
  725.                 v[i] ='\0';
  726.                 mParameter.push_back(v+paramsLength);                          
  727.                 paramsLength=i+1;
  728.             }
  729.             i++;
  730.         }
  731.  
  732.         if(v[i] == ':')
  733.         {
  734.            
  735.             mFinalParameter = v + ++i ;
  736.         }
  737. }
  738.  
  739. ********************************************************************************************
  740.  
  741. #ifndef ServerCommand_h__
  742. #define ServerCommand_h__
  743. #include "String.h"
  744. #include "IncVector.h"
  745.  
  746. class ServerCommand
  747. {
  748. public :
  749.     ServerCommand();
  750.    
  751.     ~ServerCommand();  
  752.  
  753.     void Parse(char* s);   
  754.    
  755.     string GetCommand() const { return mCommand; }
  756.     void SetCommand(string val) { mCommand = val; }
  757.    
  758.     vector<string> GetParameter() const { return mParameter; }
  759.     void SetParameter(vector<string> val) { mParameter = val; }
  760.  
  761.     std::string GetFinalParameter() const { return mFinalParameter; }
  762.     void SetFinalParameter(std::string val) { mFinalParameter = val; }
  763.    
  764.     std::string GetSender() const { return mSender; }
  765.     void SetSender(std::string val) { mSender = val; }
  766.  
  767.     string ToString()
  768.     {
  769.         string result = mCommand;          
  770.         for(int i = 0; i < mParameter.size();i++)
  771.         {
  772.             result.append(" ");
  773.             result.append(mParameter[i]);
  774.         }
  775.         if(mFinalParameter.size()>0)
  776.         {
  777.             result.append(" : ");
  778.             result.append(mFinalParameter);
  779.         }
  780.         return result;
  781.     }
  782. private:
  783.     string mSender;
  784.     string mCommand;   
  785.     vector<string> mParameter; 
  786.     string mFinalParameter;
  787.    
  788. };
  789. #endif // ServerCommand_h__
  790. ********************************************************************************************
  791. #ifndef SharedPointer_h__
  792. #define SharedPointer_h__
  793. template<class T> class SharedPointer
  794. {
  795. public:
  796.     struct SmartObjet
  797.     {
  798.     public:
  799.     SmartObjet(T* val) : mT(val),mReferenceCount(1){}
  800.     unsigned long int mReferenceCount;
  801.     T* mT;
  802.     }* mObjet; 
  803.  
  804. public :   
  805.     typedef T elementType;
  806.  
  807.     explicit SharedPointer(T* t =0) : mObjet(0) {if(t)mObjet = new (nothrow) SmartObjet(t);}
  808.  
  809.     ~SharedPointer()
  810.     {
  811.         release();
  812.     }
  813.     SharedPointer(const SharedPointer & other) throw()
  814.     {
  815.         acquire(other.mObjet);
  816.     }
  817.     SharedPointer& operator=(SharedPointer const& other)
  818.     {
  819.         if(this != &other)
  820.         {          
  821.         release();
  822.         acquire(other.mObjet);
  823.         }
  824.         return *this;
  825.     }
  826.     T* operator->() const
  827.     {
  828.         return mObjet->mT;
  829.     }
  830.     T& operator*() const
  831.  
  832.         return *(mObjet->mT);
  833.     }
  834.    
  835.     //Member templates 
  836.    
  837.     template<class Y> SharedPointer(SharedPointer<Y> & other) throw()
  838.     {
  839.        
  840.         acquire((SmartObjet*)other.mObjet);
  841.     }
  842.    
  843.     template<class Y> SharedPointer& operator=(SharedPointer<Y> & other)
  844.     {
  845.         if(this != &other)
  846.         {
  847.             release();
  848.             acquire(other.mObjet);
  849.         }
  850.         return *this;
  851.     }  
  852.    
  853.     T* get() const
  854.     {
  855.         return mObjet? mObjet->mT : 0;
  856.     }
  857.    
  858.     bool unique()
  859.     {
  860.         return mObjet? (mObjet->mReferenceCount ==1) : true;
  861.     }
  862.  
  863.     unsigned long int use_count()
  864.     {
  865.         return mObjet? mObjet->mReferenceCount : 0;
  866.     }
  867.     void swap(SharedPointer& other)
  868.     {
  869.         SmartObjet* temp = mObjet;
  870.         mObjet = other.mObjet;
  871.         other.mObjet = temp;
  872.     }
  873.     bool operator==(SharedPointer& other)
  874.     {
  875.         return (mObjet == other.mObjet);
  876.     }
  877.     bool operator!=(SharedPointer& other)
  878.     {
  879.         return !(*this == other);
  880.     }
  881. public :
  882.     void release()
  883.     {
  884.         if(mObjet)
  885.         {
  886.         mObjet->mReferenceCount--;
  887.         if(!mObjet->mReferenceCount && mObjet->mT)
  888.         {
  889.             delete mObjet->mT;
  890.         }
  891.         }
  892.     }
  893.     void acquire(SmartObjet* obj) throw()
  894.     {
  895.        
  896.         mObjet = obj;
  897.         if(mObjet) obj->mReferenceCount++;
  898.     }  
  899. };
  900. #endif // SharedPointer_h__
  901.  
  902.  
  903. ********************************************************************************************
  904. #ifndef User_h__
  905. #define User_h__
  906. #include <memory>
  907. class UserCommand;
  908.  
  909. class User
  910. {
  911. public :
  912. string NickName() const { return mNickName; }
  913. void NickName(string val) { mNickName = val; }
  914.  
  915. std::auto_ptr<UserCommand> GenerateNickNameCommand()
  916. {
  917.     std::auto_ptr<UserCommand> usrcmd(new UserCommand("NICK"));
  918.     usrcmd->AddParameter(mNickName);
  919.     return usrcmd;
  920. }
  921.  
  922. std::auto_ptr<UserCommand> GenerateRegisterUserCommand()
  923. {
  924.     std::auto_ptr<UserCommand> usrcmd(new UserCommand("USER"));
  925.     usrcmd->AddParameter(mUserName);
  926.     usrcmd->AddParameter("0");
  927.     usrcmd->AddParameter("*");
  928.     usrcmd->FinalParameter(mUserName); 
  929.     return usrcmd;
  930. }
  931. std::auto_ptr<UserCommand> GenerateIdentCommand()
  932. {
  933.     std::auto_ptr<UserCommand> usrcmd(new UserCommand("AUTH"));
  934.     usrcmd->AddParameter(mNickName);
  935.     usrcmd->AddParameter(mPassword);   
  936.     return usrcmd;
  937. }
  938. string UserName() const { return mUserName; }
  939. void UserName(string val) { mUserName = val; }
  940.  
  941. int Mode() const { return mMode; }
  942. void Mode(int val) { mMode = val; }
  943.  
  944. std::string Password() const { return mPassword; }
  945. void Password(std::string val) { mPassword = val; }
  946.  
  947. private :
  948. string mUserName;
  949. string mNickName;
  950. string mPassword;
  951.  
  952. int mMode;
  953. };
  954. #endif // User_h__
  955.  
  956. ********************************************************************************************
Add Comment
Please, Sign In to add comment