![]() |
ChatSocket.cppGo to the documentation of this file.00001 //ChatSocket.cpp 00002 /* 00003 Copyright (C) 2004 Anders Hedstrom 00004 00005 This program is free software; you can redistribute it and/or 00006 modify it under the terms of the GNU General Public License 00007 as published by the Free Software Foundation; either version 2 00008 of the License, or (at your option) any later version. 00009 00010 This program is distributed in the hope that it will be useful, 00011 but WITHOUT ANY WARRANTY; without even the implied warranty of 00012 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00013 GNU General Public License for more details. 00014 00015 You should have received a copy of the GNU General Public License 00016 along with this program; if not, write to the Free Software 00017 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. 00018 */ 00019 00020 //#include <stdio.h> 00021 00022 #include "DChatHandler.h" 00023 #include "ChatSocket.h" 00024 00025 00026 00027 00028 ChatSocket::ChatSocket(ISocketHandler& h) 00029 :TcpSocket(h) 00030 ,m_state(STATE_LOGIN) 00031 ,m_name("") 00032 { 00033 SetLineProtocol(); 00034 } 00035 00036 00037 ChatSocket::~ChatSocket() 00038 { 00039 } 00040 00041 00042 void ChatSocket::OnAccept() 00043 { 00044 Send("Welcome.\n"); 00045 SendPrompt(); 00046 } 00047 00048 00049 void ChatSocket::OnLine(const std::string& line) 00050 { 00051 switch (m_state) 00052 { 00053 case STATE_LOGIN: 00054 if (line.size()) 00055 { 00056 m_name = line; 00057 m_state = STATE_PROMPT; 00058 } 00059 SendPrompt(); 00060 break; 00061 case STATE_PROMPT: 00062 if (line.size()) 00063 { 00064 if (line == "/who") 00065 { 00066 static_cast<DChatHandler&>(Handler()).Who(this); 00067 } 00068 else 00069 if (line == "/quit") 00070 { 00071 m_state = STATE_QUIT; 00072 } 00073 else 00074 { 00075 static_cast<DChatHandler&>(Handler()).Talk(m_name,line); 00076 Send("You say '" + line + "'\n"); 00077 } 00078 } 00079 SendPrompt(); 00080 break; 00081 case STATE_QUIT: 00082 break; 00083 } 00084 } 00085 00086 00087 void ChatSocket::SendPrompt() 00088 { 00089 switch (m_state) 00090 { 00091 case STATE_LOGIN: 00092 Send("Enter name: "); 00093 break; 00094 case STATE_PROMPT: 00095 Send("> "); 00096 break; 00097 case STATE_QUIT: 00098 Send("Goodbye!\n"); 00099 SetCloseAndDelete(); 00100 break; 00101 } 00102 } 00103 00104 |