Logo
~Sockets~
~Examples~
~Contact~

InSocket Class Reference
[Networking and events]

Connection. More...

#include <InSocket.h>

Collaboration diagram for InSocket:

Collaboration graph
[legend]
List of all members.

Public Member Functions

 InSocket (ISocketHandler &)
 ~InSocket ()
void OnDelete ()
void OnAccept ()
void OnLine (const std::string &)
void ReadLine ()
void Send (const std::string &)
void Sendf (char *format,...)
void SendPrompt ()
void SetPrompt (const std::string &="", bool colorize=true)
void SetPrompt (Command *, int state, const std::string &, bool colorize=true)
void SetCommandData (const std::string &x)
const std::string & GetCommandData ()
void SetCommandData2 (const std::string &x)
const std::string & GetCommandData2 ()
bool UseMxp ()
Database & GetDatabase ()
void SetAccountName (const std::string &x)
const std::string & GetAccountName ()
void OnEvent (int id)
int AddEvent (long sec, long usec)
int GetMoveEvent ()
void SetMoveEvent (long sec, long usec)
void BufferMove (const std::string &)
void CancelMoves ()
void GlobalEvent (const std::string &)
void Count (const std::string &)

Private Member Functions

void send_mxp_defines ()
void write_to_buffer (const char *)
void write_to_buffer (const unsigned char *)

Private Attributes

std::string m_prompt
Commandm_active_command
int m_command_state
std::string m_command_data
std::string m_command_data2
bool m_colorize_prompt
bool m_b_use_mxp
std::string m_account_name
int m_move_event
std::list< std::string > m_move_buffer
std::string m_line

Detailed Description

Connection.

Definition at line 66 of file InSocket.h.


Constructor & Destructor Documentation

InSocket::InSocket ( ISocketHandler &   ) 

Definition at line 39 of file InSocket.cpp.

00040 :TcpSocket(h)
00041 ,IEventOwner(static_cast<FutureHandler&>(h))
00042 ,m_active_command(NULL)
00043 ,m_command_state(0)
00044 ,m_b_use_mxp(false)
00045 ,m_move_event(0)
00046 {
00047         SetLineProtocol();
00048 }

InSocket::~InSocket (  ) 

Definition at line 51 of file InSocket.cpp.

00052 {
00053         printf("~InSocket()\n");
00054 }


Member Function Documentation

void InSocket::OnDelete (  ) 

Definition at line 57 of file InSocket.cpp.

References Player::Exists(), GetAccountName(), GetDatabase(), and Player::GetDisplayName().

00058 {
00059         Player pl(GetDatabase(), GetAccountName());
00060         static_cast<FutureHandler&>(Handler()).ClearEvents(this);
00061         if (pl.Exists())
00062         {
00063                 static_cast<FutureHandler&>(Handler()).GlobalEvent(this, pl.GetDisplayName() + " dropped.");
00064         }
00065 }

void InSocket::OnAccept (  ) 

Definition at line 68 of file InSocket.cpp.

References Count(), Send(), SetPrompt(), will_mxp_str, and write_to_buffer().

00069 {
00070         static_cast<FutureHandler&>(Handler()).Count("Number of connections");
00071         printf("%s\n", GetRemoteAddress().c_str());
00072         Send("&WWelcome &rto &gFuture&nMud.\n");
00073         write_to_buffer(will_mxp_str);
00074         SetPrompt(static_cast<FutureHandler&>(Handler()).GetLogin(), 0, "Name, please? ");
00075 }

void InSocket::OnLine ( const std::string &   ) 

Definition at line 203 of file InSocket.cpp.

References m_active_command, m_command_state, SendPrompt(), and SetPrompt().

Referenced by ReadLine().

00204 {
00205         if (line[0] == ' ')
00206         {
00207                 size_t i = 0;
00208                 while (line[i] == ' ')
00209                         i++;
00210                 static_cast<FutureHandler&>(Handler()).Talk(this, line.substr(i));
00211                 SendPrompt();
00212                 return;
00213         }
00214         if (m_active_command)
00215         {
00216                 m_active_command -> OnLine(this, line, m_command_state);
00217                 return;
00218         }
00219         if (!line.size())
00220         {
00221                 SetPrompt();
00222                 return;
00223         }
00224         Parse pa(line);
00225         std::string cmd = pa.getword();
00226         static_cast<FutureHandler&>(Handler()).DoCommand(this, cmd, pa.getrest());
00227 }

void InSocket::ReadLine (  ) 

Definition at line 140 of file InSocket.cpp.

References do_mxp_str, dont_mxp_str, m_b_use_mxp, m_line, OnLine(), and send_mxp_defines().

00141 {
00142         if (ibuf.GetLength())
00143         {
00144                 size_t x = 0;
00145                 size_t n = ibuf.GetLength();
00146                 char tmp[TCP_BUFSIZE_READ + 1];
00147 
00148                 n = (n >= TCP_BUFSIZE_READ) ? TCP_BUFSIZE_READ : n;
00149                 ibuf.Read(tmp,n);
00150                 tmp[n] = 0;
00151 
00152                 for (size_t i = 0; i < n; i++)
00153                 {
00154                         unsigned char uc;
00155                         memcpy(&uc, &tmp[i], 1);
00156                         if (uc == IAC)
00157                         {
00158                                 size_t l = strlen((char *)do_mxp_str);
00159                                 if (!memcmp(tmp + i, do_mxp_str, l))
00160                                 {
00161                                         m_b_use_mxp = true;
00162                                         send_mxp_defines();
00163                                         memmove(tmp + i, tmp + i + l, strlen(tmp + i + l) + 1);
00164                                         n -= l;
00165                                 }
00166                                 else
00167                                 if (!memcmp(tmp + i, dont_mxp_str, strlen((char *)dont_mxp_str)))
00168                                 {
00169                                         m_b_use_mxp = false;
00170                                         l = strlen((char *)dont_mxp_str);
00171                                         memmove(tmp + i, tmp + i + l, strlen(tmp + i + l) + 1);
00172                                         n -= l;
00173                                 }
00174                         }
00175                         if (i >= n)
00176                                 break;
00177                         while (tmp[i] == 13 || tmp[i] == 10)
00178                         {
00179                                 char c = tmp[i];
00180                                 tmp[i] = 0;
00181                                 if (tmp[x])
00182                                 {
00183                                         m_line += (tmp + x);
00184                                 }
00185                                 OnLine( m_line );
00186                                 i++;
00187                                 if (i < n && (tmp[i] == 13 || tmp[i] == 10) && tmp[i] != c)
00188                                 {
00189                                         i++;
00190                                 }
00191                                 x = i;
00192                                 m_line = "";
00193                         }
00194                 }
00195                 if (tmp[x])
00196                 {
00197                         m_line += (tmp + x);
00198                 }
00199         }
00200 }

void InSocket::Send ( const std::string &   ) 

Definition at line 78 of file InSocket.cpp.

References cstring::c_str(), and write_to_buffer().

Referenced by BufferMove(), OnAccept(), OnEvent(), Sendf(), and SendPrompt().

00079 {
00080         cstring cs = s;
00081 //      TcpSocket::Send(cs.c_str());
00082         write_to_buffer(cs.c_str());
00083 }

void InSocket::Sendf ( char *  format,
  ... 
)

Definition at line 86 of file InSocket.cpp.

References Send().

00087 {
00088         va_list ap;
00089         va_start(ap, format);
00090         char slask[5000];
00091 #ifdef _WIN32
00092         vsprintf(slask, format, ap);
00093 #else
00094         vsnprintf(slask, 5000, format, ap);
00095 #endif
00096         va_end(ap);
00097         Send( slask );
00098 }

void InSocket::SendPrompt (  ) 

Definition at line 127 of file InSocket.cpp.

References m_colorize_prompt, m_prompt, Send(), and write_to_buffer().

Referenced by OnEvent(), OnLine(), and SetPrompt().

00128 {
00129         if (!m_colorize_prompt)
00130         {
00131                 write_to_buffer(m_prompt.c_str());
00132         }
00133         else
00134         {
00135                 Send(m_prompt);
00136         }
00137 }

void InSocket::SetPrompt ( const std::string &  = "",
bool  colorize = true 
)

Definition at line 101 of file InSocket.cpp.

References m_active_command, m_colorize_prompt, m_prompt, and SendPrompt().

Referenced by BufferMove(), OnAccept(), and OnLine().

00102 {
00103         m_active_command = NULL;
00104         if (!p.size()) // default
00105         {
00106                 m_prompt = "> ";
00107         }
00108         else
00109         {
00110                 m_prompt = p;
00111         }
00112         m_colorize_prompt = colorize;
00113         SendPrompt();
00114 }

void InSocket::SetPrompt ( Command ,
int  state,
const std::string &  ,
bool  colorize = true 
)

Definition at line 117 of file InSocket.cpp.

References m_active_command, m_colorize_prompt, m_command_state, m_prompt, and SendPrompt().

00118 {
00119         m_active_command = cc;
00120         m_command_state = state;
00121         m_prompt = prompt;
00122         m_colorize_prompt = colorize;
00123         SendPrompt();
00124 }

void InSocket::SetCommandData ( const std::string &  x  )  [inline]

Definition at line 83 of file InSocket.h.

References m_command_data.

00083 { m_command_data = x; }

const std::string& InSocket::GetCommandData (  )  [inline]

Definition at line 84 of file InSocket.h.

References m_command_data.

00084 { return m_command_data; }

void InSocket::SetCommandData2 ( const std::string &  x  )  [inline]

Definition at line 85 of file InSocket.h.

References m_command_data2.

00085 { m_command_data2 = x; }

const std::string& InSocket::GetCommandData2 (  )  [inline]

Definition at line 86 of file InSocket.h.

References m_command_data2.

00086 { return m_command_data2; }

bool InSocket::UseMxp (  )  [inline]

Definition at line 88 of file InSocket.h.

References m_b_use_mxp.

00088 { return m_b_use_mxp; }

Database & InSocket::GetDatabase (  ) 

Definition at line 370 of file InSocket.cpp.

Referenced by OnDelete().

00371 {
00372         return static_cast<FutureHandler&>(Handler()).GetDatabase();
00373 }

void InSocket::SetAccountName ( const std::string &  x  )  [inline]

Definition at line 91 of file InSocket.h.

References m_account_name.

00091 { m_account_name = x; }

const std::string& InSocket::GetAccountName (  )  [inline]

Definition at line 92 of file InSocket.h.

References m_account_name.

Referenced by OnDelete().

00092 { return m_account_name; }

void InSocket::OnEvent ( int  id  ) 

Definition at line 376 of file InSocket.cpp.

References m_move_buffer, m_move_event, Send(), and SendPrompt().

00377 {
00378         if (id == m_move_event)
00379         {
00380                 m_move_event = 0;
00381                 if (m_move_buffer.size())
00382                 {
00383                         std::list<std::string>::iterator it = m_move_buffer.begin();
00384                         std::string move = *it;
00385                         m_move_buffer.erase(it);
00386                         Command *cc = NULL;
00387                         if (move == "forward")
00388                                 cc = static_cast<FutureHandler&>(Handler()).GetCommand("forward");
00389                         else
00390                         if (move == "enter")
00391                                 cc = static_cast<FutureHandler&>(Handler()).GetCommand("enter");
00392                         else
00393                         switch (move[0])
00394                         {
00395                         case 'n':
00396                                 cc = static_cast<FutureHandler&>(Handler()).GetCommand("north");
00397                                 break;
00398                         case 's':
00399                                 cc = static_cast<FutureHandler&>(Handler()).GetCommand("south");
00400                                 break;
00401                         case 'e':
00402                                 cc = static_cast<FutureHandler&>(Handler()).GetCommand("east");
00403                                 break;
00404                         case 'w':
00405                                 cc = static_cast<FutureHandler&>(Handler()).GetCommand("west");
00406                                 break;
00407                         case 'u':
00408                                 cc = static_cast<FutureHandler&>(Handler()).GetCommand("up");
00409                                 break;
00410                         case 'd':
00411                                 cc = static_cast<FutureHandler&>(Handler()).GetCommand("down");
00412                                 break;
00413                         }
00414                         if (cc)
00415                         {
00416                                 std::string tmp = "nomap";
00417                                 Parse pa(tmp);
00418                                 if (move == "forward")
00419                                 {
00420                                         Send("\nMoving forward\n");
00421                                 }
00422                                 else
00423                                 if (move == "enter")
00424                                 {
00425                                         Send("\nEntering portal...\n");
00426                                 }
00427                                 else
00428                                 {
00429                                         Send("\nMoving " + cc -> GetCommand() + "\n");
00430                                 }
00431                                 cc -> Execute(this, tmp, pa);
00432                         }
00433                 }
00434         }
00435         else
00436         {
00437                 Send("\nEvent: " + Utility::l2string(id) + "\n");
00438                 SendPrompt();
00439         }
00440 }

int InSocket::AddEvent ( long  sec,
long  usec 
)

Definition at line 443 of file InSocket.cpp.

Referenced by SetMoveEvent().

00444 {
00445         return static_cast<FutureHandler&>(Handler()).AddEvent(this, sec, usec);
00446 }

int InSocket::GetMoveEvent (  )  [inline]

Definition at line 97 of file InSocket.h.

References m_move_event.

00097 { return m_move_event; }

void InSocket::SetMoveEvent ( long  sec,
long  usec 
)

Definition at line 457 of file InSocket.cpp.

References AddEvent(), and m_move_event.

00458 {
00459         m_move_event = AddEvent(sec, usec);
00460 }

void InSocket::BufferMove ( const std::string &   ) 

Definition at line 449 of file InSocket.cpp.

References m_move_buffer, Send(), and SetPrompt().

00450 {
00451         m_move_buffer.push_back(x);
00452         Send("Buffering move.\n");
00453         SetPrompt();
00454 }

void InSocket::CancelMoves (  ) 

Definition at line 463 of file InSocket.cpp.

References m_move_buffer.

00464 {
00465         while (m_move_buffer.size())
00466         {
00467                 std::list<std::string>::iterator it = m_move_buffer.begin();
00468                 m_move_buffer.erase(it);
00469         }
00470 }

void InSocket::GlobalEvent ( const std::string &   ) 

Definition at line 473 of file InSocket.cpp.

00474 {
00475         static_cast<FutureHandler&>(Handler()).GlobalEvent(this, x);
00476 }

void InSocket::Count ( const std::string &   ) 

Definition at line 479 of file InSocket.cpp.

Referenced by OnAccept().

00480 {
00481         static_cast<FutureHandler&>(Handler()).Count(x);
00482 }

void InSocket::send_mxp_defines (  )  [private]

Definition at line 230 of file InSocket.cpp.

References MXPMODE, MXPTAG, start_mxp_str, and write_to_buffer().

Referenced by ReadLine().

00230                                        ://www.gammon.com.au/mushclient/addingservermxp.htm
00231 {
00232         write_to_buffer( start_mxp_str );
00233         write_to_buffer( MXPMODE (6) );  /* permanent secure mode */
00234         write_to_buffer( MXPTAG ("!-- Set up MXP elements --"));
00235         /* Exit tag */
00236         write_to_buffer( MXPTAG ("!ELEMENT Ex '<send>' FLAG=RoomExit"));
00237         /* Room description tag */
00238         write_to_buffer( MXPTAG ("!ELEMENT rdesc '<p>' FLAG=RoomDesc"));
00239         /* Get an item tag (for things on the ground) */
00240         write_to_buffer( MXPTAG 
00241                         ("!ELEMENT Get \"<send href='"
00242                                          "get &#39;&name;&#39;|"
00243                                          "examine &#39;&name;&#39;|"
00244                                          "drink &#39;&name;&#39;"
00245                          "' "
00246                          "hint='RH mouse click to use this object|"
00247                                          "Get &desc;|"
00248                                          "Examine &desc;|"
00249                                          "Drink from &desc;"
00250                          "'>\" ATT='name desc'") );
00251         /* Drop an item tag (for things in the inventory) */
00252         write_to_buffer( MXPTAG 
00253                         ("!ELEMENT Drop \"<send href='"
00254                                          "drop &#39;&name;&#39;|"
00255                                          "examine &#39;&name;&#39;|"
00256                                          "look in &#39;&name;&#39;|"
00257                                          "wear &#39;&name;&#39;|"
00258                                          "eat &#39;&name;&#39;|"
00259                                          "drink &#39;&name;&#39;"
00260                          "' "
00261                          "hint='RH mouse click to use this object|"
00262                                          "Drop &desc;|"
00263                                          "Examine &desc;|"
00264                                          "Look inside &desc;|"
00265                                          "Wear &desc;|"
00266                                          "Eat &desc;|"
00267                                          "Drink &desc;"
00268                          "'>\" ATT='name desc'") );
00269         /* Bid an item tag (for things in the auction) */
00270         write_to_buffer( MXPTAG 
00271                         ("!ELEMENT Bid \"<send href='bid &#39;&name;&#39;' "
00272                          "hint='Bid for &desc;'>\" "
00273                          "ATT='name desc'") );
00274         /* List an item tag (for things in a shop) */
00275         write_to_buffer( MXPTAG 
00276                         ("!ELEMENT List \"<send href='buy &#39;&name;&#39;' "
00277                          "hint='Buy &desc;'>\" "
00278                          "ATT='name desc'") );
00279         /* Player tag (for who lists, tells etc.) */
00280         write_to_buffer( MXPTAG 
00281                         ("!ELEMENT Player \"<send href='tell &#39;&name;&#39; ' "
00282                          "hint='Send a message to &name;' prompt>\" "
00283                          "ATT='name'") );
00284 }

void InSocket::write_to_buffer ( const char *   )  [private]

Definition at line 287 of file InSocket.cpp.

References m_b_use_mxp, MXP_AMPc, MXP_BEGc, and MXP_ENDc.

Referenced by OnAccept(), Send(), send_mxp_defines(), and SendPrompt().

00288 {
00289         std::string tmp;
00290         size_t l = strlen(s);
00291         bool in_mxp = false; // MXP_BEGc ... MXP_ENDc
00292         bool in_ent = false; // MXP_AMPc ... ;
00293         for (size_t i = 0; i < l; i++)
00294         {
00295                 if (in_mxp)
00296                 {
00297                         if (s[i] == MXP_ENDc)
00298                         {
00299                                 if (m_b_use_mxp)
00300                                         tmp += '>';
00301                                 in_mxp = false;
00302                         }
00303                         else
00304                         {
00305                                 if (m_b_use_mxp)
00306                                         tmp += s[i];
00307                         }
00308                 }
00309                 else
00310                 if (in_ent)
00311                 {
00312                         if (m_b_use_mxp)
00313                                 tmp += s[i];
00314                         if (s[i] == ';')
00315                                 in_ent = false;
00316                 }
00317                 else
00318                 switch (s[i])
00319                 {
00320                 case MXP_BEGc:
00321                         if (m_b_use_mxp)
00322                                 tmp += '<';
00323                         in_mxp = true;
00324                         break;
00325                 case MXP_AMPc:
00326                         if (m_b_use_mxp)
00327                                 tmp += '&';
00328                         in_ent = true;
00329                         break;
00330                 case MXP_ENDc: // huh
00331                         break;
00332                 case '<':
00333                         if (m_b_use_mxp)
00334                                 tmp += "&lt;";
00335                         else
00336                                 tmp += s[i];
00337                         break;
00338                 case '>':
00339                         if (m_b_use_mxp)
00340                                 tmp += "&gt;";
00341                         else
00342                                 tmp += s[i];
00343                         break;
00344                 case '&':
00345                         if (m_b_use_mxp)
00346                                 tmp += "&amp;";
00347                         else
00348                                 tmp += s[i];
00349                         break;
00350                 case '"':
00351                         if (m_b_use_mxp)
00352                                 tmp += "&quot;";
00353                         else
00354                                 tmp += s[i];
00355                         break;
00356                 default:
00357                         tmp += s[i];
00358                 }
00359         }
00360         TcpSocket::Send(tmp);
00361 }

void InSocket::write_to_buffer ( const unsigned char *   )  [private]

Definition at line 364 of file InSocket.cpp.

00365 {
00366         SendBuf((const char *)s, strlen((char *)s));
00367 }


Member Data Documentation

std::string InSocket::m_prompt [private]

Definition at line 109 of file InSocket.h.

Referenced by SendPrompt(), and SetPrompt().

Definition at line 110 of file InSocket.h.

Referenced by OnLine(), and SetPrompt().

Definition at line 111 of file InSocket.h.

Referenced by OnLine(), and SetPrompt().

std::string InSocket::m_command_data [private]

Definition at line 112 of file InSocket.h.

Referenced by GetCommandData(), and SetCommandData().

std::string InSocket::m_command_data2 [private]

Definition at line 113 of file InSocket.h.

Referenced by GetCommandData2(), and SetCommandData2().

Definition at line 114 of file InSocket.h.

Referenced by SendPrompt(), and SetPrompt().

bool InSocket::m_b_use_mxp [private]

Definition at line 115 of file InSocket.h.

Referenced by ReadLine(), UseMxp(), and write_to_buffer().

std::string InSocket::m_account_name [private]

Definition at line 116 of file InSocket.h.

Referenced by GetAccountName(), and SetAccountName().

int InSocket::m_move_event [private]

Definition at line 117 of file InSocket.h.

Referenced by GetMoveEvent(), OnEvent(), and SetMoveEvent().

std::list<std::string> InSocket::m_move_buffer [private]

Definition at line 118 of file InSocket.h.

Referenced by BufferMove(), CancelMoves(), and OnEvent().

std::string InSocket::m_line [private]

Definition at line 119 of file InSocket.h.

Referenced by ReadLine().


The documentation for this class was generated from the following files:
Page, code, and content Copyright (C) 2006 by Anders Hedström
Generated on Mon Aug 29 20:21:47 2005 for C++ Sockets by  doxygen 1.4.4