Example #1
0
 private bool OnControllerData(TCPClientState state, byte[] buf)
 {
     if (state.conncode == null)
     {
         int    cmd  = 0;
         byte[] data = m_protocol.ParseCmd(buf, ref cmd);
         if (cmd != (int)Protocol.CMD.C2S_RDCONNECT || data == null || data.Length == 0)
         {
             ;
             return(false);
         }
         string sdata = System.Text.UTF8Encoding.UTF8.GetString(data);
         if (string.IsNullOrEmpty(sdata))
         {
             return(false);
         }
         string[] rdata = sdata.Split(new char[] { '|' }, 3);
         if (rdata == null || rdata.Length != 3)
         {
             return(false);
         }
         state.conncode = rdata[1];
         string         param;
         string         pwd;
         TCPClientState peer = ConnCode.GetState(state.conncode, out param, out pwd);
         if (peer == null)
         {
             Logger.Trace("客户端未连接");
             return(false);
         }
         if (pwd != rdata[2])
         {
             Logger.Trace("密码错误");
             return(false);
         }
         state.PeerTcpClient = peer;
         peer.PeerTcpClient  = state;
         state.WriteData(m_protocol.S2C_RdConnect(param));
         Logger.Trace("Controller Connect code=" + state.conncode);
     }
     else
     {
         if (state.PeerTcpClient != null)
         {
             try
             {
                 state.PeerTcpClient.WriteData(buf);
             }
             catch (Exception e)
             {
                 Logger.Trace(e);
             }
         }
     }
     return(true);
 }
Example #2
0
 private bool OnClientData(TCPClientState state, byte[] buf)
 {
     if (state.conncode == null)
     {
         int    cmd  = 0;
         byte[] data = m_protocol.ParseCmd(buf, ref cmd);
         if (cmd != (int)Protocol.CMD.C2S_HANDSHAKE || data == null || data.Length == 0)
         {
             return(false);
         }
         string xml = System.Text.UTF8Encoding.UTF8.GetString(data);
         try
         {
             XmlDocument doc = new XmlDocument();
             doc.LoadXml(xml);
             XmlNode node    = doc.SelectSingleNode("//param");
             string  param   = "";
             XmlNode tmpnode = node.SelectSingleNode("name");
             if (tmpnode != null)
             {
                 param += tmpnode.InnerText;
             }
             tmpnode = node.SelectSingleNode("mac");
             if (tmpnode != null)
             {
                 param += "|" + tmpnode.InnerText;
             }
             XmlNode pwdnode = node.SelectSingleNode("pwd");
             string  pwd     = "";
             if (pwdnode != null)
             {
                 pwd = pwdnode.InnerText;
             }
             XmlNode codenode = node.SelectSingleNode("code");
             string  code     = "";
             if (codenode != null)
             {
                 code = codenode.InnerText;
             }
             node.ParentNode.RemoveChild(node);
             state.conncode = ConnCode.GetCode(state, param, code, pwd, doc.InnerXml);
             state.WriteData(m_protocol.S2C_Handshake(state.conncode));
             Logger.Trace("Client Connect code=" + state.conncode);
         }
         catch (Exception e)
         {
             Logger.Trace("xml=" + xml);
             Logger.Trace(e);
             return(false);
         }
     }
     else
     {
         if (state.PeerTcpClient != null)
         {
             try
             {
                 state.PeerTcpClient.WriteData(buf);
             }
             catch (Exception e)
             {
                 Logger.Trace(e);
             }
         }
     }
     return(true);
 }