Example #1
0
 /// <summary>
 /// 关闭当前连接
 /// </summary>
 /// <returns></returns>
 public bool CloseConnect()
 {
     if (isInit == false)
     {
         // OnRaiseError("未初始化,请先初始化");
         return(false);
     }
     if (parentSocket != null)
     {
         isReconnection = false;
         parentSocket.Dispose();
     }
     return(true);
 }
Example #2
0
        /// <summary>
        /// 连接服务器
        /// </summary>
        /// <param name="address">地址</param>
        /// <param name="port">端口</param>
        /// <returns></returns>
        public YChannel Connect(string address, uint port)
        {
            if (isInit == false)
            {
                new NullReferenceException("未初始化,请先初始化");
                OnRaiseError("未初始化,请先初始化");
                return(null);
            }
            Socket socket = transfer.ConnectParent(address, port);

            parentSocket = new YChannel(socket, socketPool, ChannelType.Server, "");
            parentSocket.DataPacketManager = this.Analysis;

            parentSocket.RaiseErrored        += parentSocket_RaiseErrored;
            parentSocket.SocketDisconnecting += parentSocket_SocketDisconnecting;
            parentSocket.DataPacketReceived  += parentSocket_DataPacketReceived;
            if (parentSocket != null)
            {
                parentSocket.Start();
                if (CustomDecode == false)
                {
                    Thread.Sleep(100);
                    parentSocket.Send(new SocketRegisterPacket(RegisterType.Add, netTree.Serialize()));
                    if (registerWaite.WaitOne(10000) == false)
                    {
                        parentSocket.Dispose();
                        new Exception("注册失败");
                        //OnRaiseError("注册失败");
                        // return null;
                    }
                }
                IsConnect = true;
                return(parentSocket);
            }
            return(null);
        }
Example #3
0
        private void ConnectCallBack(IAsyncResult e)
        {
            BeginConnectInfo  ret   = e as BeginConnectInfo;
            AsyncConnectState state = e.AsyncState as AsyncConnectState;

            if (state != null)
            {
                if (state.ConnectSocket != null)
                {
                    try
                    {
                        state.ConnectSocket.EndConnect(e);
                    }
                    catch (Exception ex)
                    {
                        if (BeginConnectComplete != null)
                        {
                            BeginConnectComplete(
                                false,
                                ret != null ? ret.ErrorMessage : ex.Message,
                                state != null ? state.State : null
                                );
                        }
                        return;
                    }
                }
            }
            if (e.IsCompleted)
            {
                if (state == null)
                {
                    BeginConnectComplete(false, "连接错误", null);
                    return;
                }
                parentSocket = new YChannel(state.ConnectSocket, socketPool, ChannelType.Client);
                parentSocket.DataPacketManager    = this.Analysis;
                parentSocket.RaiseErrored        += parentSocket_RaiseErrored;
                parentSocket.SocketDisconnecting += parentSocket_SocketDisconnecting;
                parentSocket.DataPacketReceived  += parentSocket_DataPacketReceived;
                if (parentSocket != null)
                {
                    parentSocket.Start();
                    if (CustomDecode == false)
                    {
                        Thread.Sleep(100);
                        parentSocket.Send(new SocketRegisterPacket(RegisterType.Add, netTree.Serialize()));
                        if (registerWaite.WaitOne(10000) == false)
                        {
                            parentSocket.SocketDisconnecting -= parentSocket_SocketDisconnecting;
                            parentSocket.Dispose();
                            OnRaiseError("注册失败");
                            return;
                        }
                    }
                    IsConnect = true;
                    BeginConnectComplete(true, "", state.State);
                }
                else
                {
                    BeginConnectComplete(false, "连接错误", state.State);
                }
            }
            else
            {
                if (BeginConnectComplete != null)
                {
                    BeginConnectComplete(
                        false,
                        ret != null ? ret.ErrorMessage : "连接失败",
                        state != null ? state.State : null
                        );
                }
            }
        }