Пример #1
0
        /// <summary>
        /// 当连接服务器之后的回调函数
        /// </summary>
        /// <param name="ar">TcpClient</param>
        private void AcceptCallback(IAsyncResult ar)
        {
            Socket socket = (Socket)ar.AsyncState;

            stateOne = new TcpTransmitBox(socket, BufferSize);
            try
            {
                socket.EndConnect(ar);
                socket.BeginReceive(stateOne.Buffer, 0, stateOne.Buffer.Length, 0, new AsyncCallback(ReadCallback), stateOne);
            }
            catch (Exception Ex)
            {
                if (outtimebool == true)
                {
                    return;               //说明已经超时了,已经触发登录失败了
                }
                loginFailure(Ex.Message); //登录失败触发
            }
        }
Пример #2
0
        /// <summary>
        /// 登录以及心跳代码进行处理;由基类在操作
        /// </summary>
        /// <param name="stateOne">TcpState</param>
        /// <param name="haveDate">代码</param>
        override internal void VerificationCodeManage(TcpTransmitBox stateOne, byte haveDate)
        {
            if (stateOne == null)
            {
                return;
            }
            switch (haveDate)
            {
            case 0:    //不是需要的数据
                break;

            case CipherCode._heartbeatCode:    //是心跳信息
                                               //stateOne.HeartTime = DateTime.Now;
                break;

            case CipherCode._clientToServer:    //客户端和服务端暗号正确;可以登录;
                loginSuccess(stateOne);
                break;
            }
        }
Пример #3
0
 /// <summary>
 /// 重连模块
 /// </summary>
 private void reconnect()
 {
     if (_reconnectMax == 0)
     {
         return;    //不重连直接返回
     }
     reconnectCi++; //每重连一次重连的次数加1
     if (stateOne != null)
     {
         stateOne.WorkSocket.Close();
         stateOne = null;
     }
     if (reconnectOn == false)
     {
         reconnectOn = true;
         CommonMethod.eventInvoket(() => { ReconnectionStart(); });
     }
     _engineStart = false;
     StartEngine();//调用启动引擎
 }
Пример #4
0
 /// <summary>
 /// 向客户端发送数据,最基础的发送
 /// </summary>
 /// <param name="stateBase">TcpState</param>
 /// <param name="data">发送的数据</param>
 override internal void Send(TransmitData stateBase, byte[] data) //实现服务端虚方法
 {
     if (stateBase == null)
     {
         return;
     }
     StickPackage.EncryptionPackage(ref data);
     //MessageBox.Show(data.Length.ToString()+"你好"+data[9].ToString());
     try
     {
         stateBase.WorkSocket.BeginSend(data, 0, data.Length, 0, new AsyncCallback(SendCallback), stateBase);
     }
     catch (Exception Ex)
     {
         int i = Ex.Message.IndexOf("远程主机强迫关闭了一个现有的连接");
         if (i != -1)
         {
             TcpTransmitBox stateOne = IPEndPointToState(stateBase.IpEndPoint);
             socketRemove(stateOne, Ex.Message);
         }
     }
 }
Пример #5
0
 /// <summary>
 /// 要被TCP子类重写的;关于验证暗号怎么处理的类;(客户端与服务端不相同)
 /// </summary>
 /// <param name="stateOne">TcpState</param>
 /// <param name="haveDate">字节</param>
 virtual internal void VerificationCodeManage(TcpTransmitBox stateOne, byte haveDate)
 {
 }
Пример #6
0
        /// <summary>
        /// 服务器向客户端发送图片数据
        /// </summary>
        /// <param name="ipEndPoint">IPEndPoint</param>
        /// <param name="data">图片的数据</param>
        public void sendMessage(IPEndPoint ipEndPoint, byte[] data)
        {
            TcpTransmitBox stateOne = IPEndPointToState(ipEndPoint);

            sendMessage(stateOne, data);
        }
Пример #7
0
        /// <summary>
        /// 服务器强制关闭一个客户端
        /// </summary>
        /// <param name="ipEndPoint">IPEndPoint</param>
        public void clientClose(IPEndPoint ipEndPoint)
        {
            TcpTransmitBox stateOne = IPEndPointToState(ipEndPoint);

            clientClose(stateOne);
        }