示例#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);
 }
示例#2
0
        /// <summary>
        /// 关闭
        /// </summary>
        public void Close()
        {
            //关闭数据的接受和发送
            try
            {
                if (this.TcpClient == null)
                {
                    return;
                }

                if (ClientType == CLIENTTYPE.CLIENT)
                {
                    Logger.Trace("Client Close code=" + conncode);
                    if (conncode != null && conncode != "")
                    {
                        ConnCode.DelState(conncode);
                    }
                }
                else if (ClientType == CLIENTTYPE.CONTROLLER)
                {
                    Logger.Trace("Controller Close code=" + conncode);
                }
                TcpClient      myclient = this.TcpClient;
                TCPClientState peer     = this.PeerTcpClient;
                this.TcpClient     = null;
                this.PeerTcpClient = null;

                if (peer != null)
                {
                    peer.PeerTcpClient = null;
                    peer.Close();
                }
                if (myclient != null)
                {
                    if (myclient.Connected)
                    {
                        myclient.Close();
                    }
                }
            }
            catch (Exception e)
            {
                Logger.Trace(e);
            }
            Buffer = null;
        }
示例#3
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);
 }