コード例 #1
0
ファイル: ServiceBase.cs プロジェクト: ymgeneral/PopLarCloud
        void transfer_Connected(object sender, System.Net.Sockets.Socket e)
        {
            YChannel client = new YChannel(e, socketPool, ChannelType.Client);

            if (socketPool.Count < 2)
            {
                return;
            }
            client.RaiseErrored        += sm_RaiseErrored;
            client.SocketDisconnecting += sm_SocketDisconnecting;
            client.DataPacketReceived  += sm_DataPacketReceived;
            client.DataPacketManager    = this.Analysis;
            client.Start();
            if (CustomDecode == true)
            {
                NetNode node = new NetNode(client.Id, this.Id);
                this.NetTree.Add(node);
                OnConnected(client);
            }
        }
コード例 #2
0
ファイル: DeviceBase.cs プロジェクト: ymgeneral/PopLarCloud
        /// <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);
        }
コード例 #3
0
ファイル: DeviceBase.cs プロジェクト: ymgeneral/PopLarCloud
        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
                        );
                }
            }
        }
コード例 #4
0
ファイル: DeviceBase.cs プロジェクト: ymgeneral/PopLarCloud
        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);
                }
            }
        }