Exemple #1
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
                        );
                }
            }
        }
Exemple #2
0
        private void ReConnectCallBack(IAsyncResult e)
        {
            AsyncConnectState state = e.AsyncState as AsyncConnectState;

            if (state != null)
            {
                if (state.ConnectSocket != null)
                {
                    try
                    {
                        state.ConnectSocket.EndConnect(e);
                    }
                    catch (Exception ex)
                    {
                        if (isReconnection == true)
                        {
                            ReConnect(this.parentSocket.Address, (uint)this.parentSocket.Port);
                        }
                        return;
                    }
                }
            }
            if (e.IsCompleted)
            {
                if (parentSocket == null)
                {
                    parentSocket = new YChannel(state.ConnectSocket, socketPool, ChannelType.Server);
                    parentSocket.DataPacketManager    = this.Analysis;
                    parentSocket.RaiseErrored        += parentSocket_RaiseErrored;
                    parentSocket.SocketDisconnecting += parentSocket_SocketDisconnecting;
                    parentSocket.DataPacketReceived  += parentSocket_DataPacketReceived;
                }
                else
                {
                    parentSocket.SetSocket(state.ConnectSocket, socketPool);
                }
                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("注册失败");
                            if (isReconnection == true)
                            {
                                ReConnect(this.parentSocket.Address, (uint)this.parentSocket.Port);
                            }
                            return;
                        }
                    }
                    IsConnect = true;
                    BeginConnectComplete(true, "", state.State);
                }
                else
                {
                    if (isReconnection == true)
                    {
                        ReConnect(this.parentSocket.Address, (uint)this.parentSocket.Port);
                    }
                }
            }
            else
            {
                if (isReconnection == true)
                {
                    ReConnect(this.parentSocket.Address, (uint)this.parentSocket.Port);
                }
            }
        }