Exemplo n.º 1
0
 private static void Client_ConnectFailed(object sender, EventArgs e)
 {
     if (ConnectFailed != null)
     {
         ConnectFailed.Invoke(sender, e);
     }
 }
Exemplo n.º 2
0
 private void OnConnectFailed()
 {
     if (ConnectFailed != null)
     {
         ConnectFailed.Invoke(this, new EventArgs());
     }
     connectExit.Set();
 }
Exemplo n.º 3
0
        void socket_ConnectFailed(object sender, EventArgs e)
        {
            AsynchronousSocket socket = sender as AsynchronousSocket;

            if (ConnectFailed != null)
            {
                ConnectFailed.Invoke(this, new EventArgs());
            }
        }
            public TcpClient(SocketManager socketManager)
            {
                maxBufferSize      = 1024;
                ConnectTimeout     = 5f;
                this.socketManager = socketManager;
                this.hearBeat      = new HearBeat(socketManager, this);

                ConnectSuccess += hearBeat.Start;
                ConnectAbort   += hearBeat.Stop;

                MsgManager.Instance.Regist(MsgID.ConnectSuccess, (data) => ConnectSuccess?.Invoke());
                MsgManager.Instance.Regist(MsgID.ConnectFailed, (data) => ConnectFailed?.Invoke());
                MsgManager.Instance.Regist(MsgID.ConnectAbort, (data) => ConnectAbort?.Invoke());
            }
Exemplo n.º 5
0
 /// <summary>
 /// (re)establish the connection to the specified ip
 /// </summary>
 /// <returns></returns>
 public void Connect()
 {
     Task.Run(() => {
         Thread.CurrentThread.CurrentUICulture = new CultureInfo("en-us");
         Debug.WriteLine($"Trying to connect to {ip}");
         client = new TcpClient();
         try {
             client.Connect(ip, 8888);
             Debug.WriteLine("Connection successfull");
             stream = client.GetStream();
             ConnectionEstablished?.Invoke(this, EventArgs.Empty);
         } catch (SocketException e) {
             ConnectFailed?.Invoke(this, EventArgs.Empty);
             Debug.WriteLine($"Couldn't connect because of {e}");
         }
     });
 }
Exemplo n.º 6
0
        private void EndConnect(IAsyncResult result)
        {
            SocketAsyncState state = (SocketAsyncState)result.AsyncState;

            try
            {
                Socket.EndConnect(result);
            }
            catch (Exception ex)
            {
                //出现异常,连接失败。
                state.Completed = true;
                //判断是否为异步,异步则引发事件
                if (state.IsAsync)
                {
                    ConnectFailed?.Invoke(this, new SocketEventArgs(this, SocketAsyncOperation.Connect, ex.Message));
                }
                return;
            }

            //连接成功。
            //创建Socket网络流
            Stream = new NetworkStream(Socket);
            if (IsUseAuthenticate)
            {
                NegotiateStream negotiate = new NegotiateStream(Stream);
                negotiate.AuthenticateAsClient();
                while (!negotiate.IsMutuallyAuthenticated)
                {
                    Thread.Sleep(10);
                }
            }
            //连接完成
            state.Completed = true;
            if (state.IsAsync)
            {
                ConnectCompleted?.Invoke(this, new SocketEventArgs(this, SocketAsyncOperation.Connect));
            }

            //开始接收数据
            Handler.BeginReceive(Stream, EndReceive, state);
        }
Exemplo n.º 7
0
        private void Listen()
        {
            while (true)
            {
                try
                {
                    byte[]        data    = new byte[512];
                    StringBuilder builder = new StringBuilder();
                    int           bytes   = 0;
                    do
                    {
                        bytes = Stream.Read(data, 0, data.Length);
                        builder.Append(Encoding.UTF8.GetString(data, 0, bytes));
                    }while (Stream.DataAvailable);

                    var message = builder.ToString();
                    if (!string.IsNullOrWhiteSpace(message))
                    {
                        var messages = message.Split('$');
                        foreach (var protocolMessage in messages)
                        {
                            if (string.IsNullOrWhiteSpace(protocolMessage))
                            {
                                continue;
                            }
                            ProcessingConveyor.Process(protocolMessage);
                        }
                    }
                }
                catch (Exception e)
                {
                    ConnectFailed?.Invoke();
                    break;
                }
            }
        }
Exemplo n.º 8
0
        protected virtual void OnConnectFailed()
        {
            var args = new NetConnectFailArgs();

            ConnectFailed?.Invoke(this, args);
        }
Exemplo n.º 9
0
            public void ProcessPackets()
            {
                while (_messageChannel.Reader.TryRead(out var item))
                {
                    switch (item)
                    {
                    case ConnectMessage connect:
                    {
                        DebugTools.Assert(IsServer);

                        var writer = connect.ChannelWriter;

                        var uid       = _genConnectionUid();
                        var sessionId = new NetSessionId($"integration_{uid}");

                        var connectArgs =
                            new NetConnectingArgs(sessionId, new IPEndPoint(IPAddress.IPv6Loopback, 0));
                        Connecting?.Invoke(this, connectArgs);
                        if (connectArgs.Deny)
                        {
                            writer.TryWrite(new DeniedConnectMessage());
                            continue;
                        }

                        writer.TryWrite(new ConfirmConnectMessage(uid, sessionId));
                        var channel = new IntegrationNetChannel(this, connect.ChannelWriter, uid, sessionId, connect.Uid);
                        _channels.Add(uid, channel);
                        Connected?.Invoke(this, new NetChannelArgs(channel));
                        break;
                    }

                    case DataMessage data:
                    {
                        IntegrationNetChannel?channel;
                        if (IsServer)
                        {
                            if (!_channels.TryGetValue(data.Connection, out channel))
                            {
                                continue;
                            }
                        }
                        else
                        {
                            if (ServerChannel == null || data.Connection != ServerChannel.ConnectionUid)
                            {
                                continue;
                            }

                            channel = ServerChannel;
                        }

                        var message = data.Message;
                        message.MsgChannel = channel;
                        if (_callbacks.TryGetValue(message.GetType(), out var callback))
                        {
                            callback(message);
                        }

                        break;
                    }

                    case DisconnectMessage disconnect:
                    {
                        if (IsServer)
                        {
                            if (_channels.TryGetValue(disconnect.Connection, out var channel))
                            {
                                Disconnect?.Invoke(this, new NetDisconnectedArgs(channel, string.Empty));

                                _channels.Remove(disconnect.Connection);
                            }
                        }
                        else
                        {
                            _channels.Clear();
                        }

                        break;
                    }

                    case DeniedConnectMessage _:
                    {
                        DebugTools.Assert(IsClient);

                        ConnectFailed?.Invoke(this, new NetConnectFailArgs("I didn't implement a deny reason!"));
                        break;
                    }

                    case ConfirmConnectMessage confirm:
                    {
                        DebugTools.Assert(IsClient);

                        var channel = new IntegrationNetChannel(this, NextConnectChannel !, _clientConnectingUid,
                                                                confirm.SessionId, confirm.AssignedUid);

                        _channels.Add(channel.ConnectionUid, channel);

                        Connected?.Invoke(this, new NetChannelArgs(channel));
                        break;
                    }

                    default:
                        throw new ArgumentOutOfRangeException();
                    }
                }
            }
Exemplo n.º 10
0
 /// <summary>
 /// Raises the ConnectFailed event.
 /// </summary>
 /// <param name="ex"></param>
 protected void OnConnectFailed(Exception ex)
 {
     ConnectFailed?.Invoke(ex);
 }
Exemplo n.º 11
0
        protected virtual void OnConnectFailed(string reason)
        {
            var args = new NetConnectFailArgs(reason);

            ConnectFailed?.Invoke(this, args);
        }
        protected void OnConnectFailed(Connection conn, ConnectionResult result)
        {
            ConnectionResultArgs args = new ConnectionResultArgs(conn, result);

            ConnectFailed?.Invoke(args);
        }
Exemplo n.º 13
0
 private void RaiseConnectFailed(BLEErrorCode code)
 => ConnectFailed?.Invoke(this, new BLEErrorEventArgs(code));
Exemplo n.º 14
0
 void OnConnectionFail(P2PSessionConnectFail_t ev)
 {
     Debug.Log($"Connect Failed to {ev.m_steamIDRemote}");
     ConnectFailed?.Invoke(ev);
 }
Exemplo n.º 15
0
 protected void OnConnectFailed(SocketResultCode result)
 {
     ConnectFailed?.Invoke(this, result);
 }