Ejemplo n.º 1
0
        public void Poll()
        {
            lock (_commandQueue)
            {
                while (_commandQueue.Count > 0)
                {
                    UdpCommand command = _commandQueue.Dequeue();
                    switch (command)
                    {
                    case UdpCommand.Connect:
                        byte[] connectionData = _dataQueue.Dequeue();
                        OnConnectionAccepted?.Invoke(connectionData);
                        break;

                    case UdpCommand.Disconnect:
                        OnDisconnect?.Invoke();
                        break;

                    case UdpCommand.Data:
                        byte[] data = _dataQueue.Dequeue();
                        OnDataReceived?.Invoke(data);
                        break;

                    default:
                        throw new ArgumentOutOfRangeException();
                    }
                }
            }
        }
Ejemplo n.º 2
0
        private void Loop()
        {
            Logger.Debug("Starting loop");

            while (_socket.IsBound)
            {
                Socket newSocket;

                OnNewStatus?.Invoke(Status.Listening);

                try
                {
                    newSocket = _socket.Accept();
                }
                catch (Exception)
                {
                    break;
                }

                OnNewStatus?.Invoke(Status.Accepting);

                var remoteAddress = newSocket.RemoteEndPoint.ToString();
                Logger.Info($"New socket connection from {remoteAddress} accepted");

                Logger.Debug("Preparing ClientSocket instance");
                OnConnectionAccepted?.Invoke(newSocket);
            }
        }
        public async Task <bool> Listen()
        {
            while (IsRunning)
            {
                if (Socket.Poll(100000, SelectMode.SelectRead))
                {
                    var newConnection = Socket.Accept();
                    if (newConnection != null)
                    {
                        var client  = new SimpleClient();
                        var newGuid = await client.CreateGuid(newConnection);

                        await client.SendMessage(newGuid);

                        Connections.Add(client);
                        var e = BuildEvent(client, null, String.Empty);
                        OnConnectionAccepted?.Invoke(this, e);
                    }
                }
            }
            return(true);
        }
Ejemplo n.º 4
0
 private void OnInternalData(ulong steamID, byte[] data)
 {
     if (data.SequenceEqual(RequestConnectPacket))
     {
         Debug.Log($"Request Connect Received from {steamID}");
         OnRequestConnect?.Invoke(steamID);
     }
     else if (data.SequenceEqual(AcceptedConnectionPacket))
     {
         Debug.Log($"Accepted Connection Received from {steamID}");
         OnConnectionAccepted?.Invoke(steamID);
     }
     else if (data.SequenceEqual(DisconnectPacket))
     {
         Debug.Log($"Received Disconnect request from {steamID}");
         OnRequestDisconnect?.Invoke(steamID);
     }
     else if (data.SequenceEqual(AcceptedDisconnectPacket))
     {
         Debug.Log($"Accepted Disconnect Received from {steamID}");
         OnDisconnectAccepted?.Invoke(steamID);
     }
 }
Ejemplo n.º 5
0
        /// <summary>
        /// Listening methode calls itself when a new client was accepted
        /// </summary>
        protected virtual async void Listen()
        {
            try
            {
                TcpClient newClient = await listener.AcceptTcpClientAsync();

                if (active)
                {
                    Listen();
                }

                OnConnectionAccepted?.Invoke(this, new NewConnectionArgs(newClient));
            }
            catch (Exception ex)
            {
                OnException?.Invoke(this, new ConnectionErrorArgs(ex, false));

                if (active)
                {
                    Listen();
                }
            }
        }
Ejemplo n.º 6
0
        public async Task StartAsync(CancellationToken?token = null)
        {
            _tokenSource = CancellationTokenSource.CreateLinkedTokenSource(token ?? new CancellationToken());
            _token       = _tokenSource.Token;
            _listener.Start();
            Listening = true;

            try
            {
                await Task.Run(async() =>
                {
                    while (!_token.IsCancellationRequested)
                    {
                        var client = await _listener.AcceptTcpClientAsync();
                        OnConnectionAccepted?.Invoke(this, new ConnectionAcceptedEventArgs(client, _token));
                    }
                }, _token);
            }
            finally
            {
                _listener.Stop();
                Listening = false;
            }
        }
Ejemplo n.º 7
0
 private void Listener_OnNewConnection(NetworkCore.Socket obj)
 {
     OnConnectionAccepted?.Invoke(this, new SocketArg(obj));
 }
Ejemplo n.º 8
0
 protected virtual void InvokeOnConnectionAccepted(NewConnectionArgs connectionArgs)
 {
     OnConnectionAccepted?.Invoke(this, connectionArgs);
 }