예제 #1
0
        private async Task ReceiveLoop()
        {
            Debug.Log("ReceiveLoop Start");

            uint     readPacketSize;
            CSteamID clientSteamID;

            try
            {
                byte[] receiveBuffer;

                while (Connected)
                {
                    while (Receive(out readPacketSize, out clientSteamID, out receiveBuffer))
                    {
                        if (readPacketSize == 0)
                        {
                            continue;
                        }
                        if (clientSteamID != hostSteamID)
                        {
                            Debug.LogError("Received a message from an unknown");
                            continue;
                        }
                        // we received some data,  raise event
                        OnReceivedData?.Invoke(receiveBuffer);
                    }
                    //not got a message - wait a bit more
                    await Task.Delay(TimeSpan.FromSeconds(secondsBetweenPolls));
                }
            }
            catch (ObjectDisposedException) { }

            Debug.Log("ReceiveLoop Stop");
        }
예제 #2
0
        private void StartReadingData()
#endif
        {
            Task.Factory.StartNew(() =>
            {
                try
                {
#if (PORTABLE)
                    socket.ConnectAsync(_ipAddress, _port).Wait();
#else
                    socket.Connect(iPEndPoint);
#endif
#if (PORTABLE)
                    ManualResetEvent ev     = new ManualResetEvent(true);
                    socket.MessageReceived += (s, e) =>
                    {
                        OnReceivedData?.Invoke(e.ByteData);
                    };

                    ev.WaitOne();
#else
                    while (!IsDisposed)
                    {
                        byte[] bytes  = new byte[BufferSize];
                        int readCount = socket.Receive(bytes);
                        OnReceivedData?.Invoke(bytes.ToList().GetRange(0, readCount).ToArray());
                    }
#endif
                }
                catch (Exception ex)
                {
                    AutoLogger.LogError(ex, "UdpConnectorBase StartReadingData");
                }
            });
        }
예제 #3
0
        public void HandleMessages()
        {
            NetIncomingMessage message;

            while ((message = m_Server.ReadMessage()) != null)
            {
                switch (message.MessageType)
                {
                case NetIncomingMessageType.ConnectionApproval:
                    try
                    {
                        var packet = Packet.Read(message.Data);
                        ApproveConnection(message.SenderConnection);
                        OnConnectionApproved?.Invoke(new LidgrenMessage(message));
                    }
                    catch (Exception e)
                    {
                        DenyConnection(message.SenderConnection);
                        OnConnectionDenied?.Invoke(new LidgrenMessage(message));
                    }
                    break;

                case NetIncomingMessageType.StatusChanged:
                    switch (message.SenderConnection.Status)
                    {
                    case NetConnectionStatus.Connected:
                        OnConnected?.Invoke(new LidgrenMessage(message));
                        break;

                    case NetConnectionStatus.Disconnected:
                        OnDisconnecting?.Invoke(new LidgrenMessage(message));         //not firing from lidgren
                        OnDisconnected?.Invoke(new LidgrenMessage(message));
                        break;
                    }
                    break;

                case NetIncomingMessageType.Data:
                    TriggerCallback(new LidgrenConnection(message.SenderConnection), message.Data);
                    OnReceivedData?.Invoke(new LidgrenMessage(message));
                    break;

                case NetIncomingMessageType.DebugMessage:
                    // handle debug messages
                    // (only received when compiled in DEBUG mode)
                    Console.WriteLine("DEBUG: " + message.ReadString());
                    break;

                case NetIncomingMessageType.WarningMessage:
                    Console.WriteLine("WARNING: " + message.ReadString());
                    break;

                /* .. */
                default:
                    Console.WriteLine("unhandled message with type: "
                                      + message.MessageType);
                    break;
                }
            }
        }
예제 #4
0
        public ReadViewModel()
        {
            ImageTapCommand = new Command(async() => {
                try
                {
                    if (_isTapped)
                    {
                        return;
                    }

                    _isTapped = true;

                    if (Device.RuntimePlatform != Device.iOS)
                    {
                        MessagingCenter.Send <App, List <string> >((App)Xamarin.Forms.Application.Current, "Tag", new List <string> {
                            "Test"
                        });
                    }
                    else
                    {
                        DependencyService.Get <INfc>().StartSession();
                    }
                    _isTapped = false;
                }
                catch (Exception ex)
                {
                    _isTapped = false;
                    await Application.Current.MainPage.DisplayAlert(AppResources.Attenzione, ex.Message, AppResources.Ok);
                }
            });

            PopupCommand = new Command(async() =>
            {
                // If there is a popup, I close it
                if (Rg.Plugins.Popup.Services.PopupNavigation.Instance.PopupStack.Count > 0)
                {
                    await Rg.Plugins.Popup.Services.PopupNavigation.Instance.PopAllAsync();
                }

                await Rg.Plugins.Popup.Services.PopupNavigation.Instance.PushAsync(new ResultPopupPage(_arg));
            });

            OnAppearingCommand = new Command(() => {
                MessagingCenter.Subscribe <App, List <string> >(this, "Tag", (sender, arg) =>
                {
                    TagId             = arg[0]; // Pos 0 = TagID
                    _arg              = arg;
                    DateTime dateTime = DateTime.Now;
                    ReceivedAt        = dateTime.ToLongDateString() + "\n" + dateTime.ToLongTimeString();
                    OnReceivedData?.Invoke(); //Now run the Action which, if it is not null, your ContentPage should have set to do the scrolling
                });
            });

            OnDisappearingCommand = new Command(() => {
                MessagingCenter.Unsubscribe <App, List <string> >(this, "Tag");
            });
        }
예제 #5
0
        protected override void OnReceiveData(byte[] data, CSteamID clientSteamID, int channel)
        {
            if (clientSteamID != hostSteamID)
            {
                Debug.LogError("Received a message from an unknown");
                return;
            }

            OnReceivedData.Invoke(data, channel);
        }
예제 #6
0
        protected override void OnReceiveData(byte[] data, ProductUserId clientUserId, int channel)
        {
            if (clientUserId != hostProductId)
            {
                Debug.LogError("Received a message from an unknown");
                return;
            }

            OnReceivedData.Invoke(data, channel);
        }
예제 #7
0
 private void AllDown(CoordinationStatus status)
 {
     // 此处没有取消和超时状态,直接完成
     if (status == CoordinationStatus.AllDone)
     {
         Interlocked.Exchange(ref ConnectStatus, 0);
         LogNet?.WriteDebug("All bytes read complete.");
         OnReceivedData?.Invoke(BytesResult.ToArray());
     }
 }
예제 #8
0
 protected override void OnReceiveData(byte[] data, CSteamID clientSteamID, int channel)
 {
     if (steamToMirrorIds.TryGetValue(clientSteamID, out int connectionId))
     {
         OnReceivedData.Invoke(connectionId, data, channel);
     }
     else
     {
         CloseP2PSessionWithUser(clientSteamID);
         Debug.LogError("Data received from steam client thats not known " + clientSteamID);
         OnReceivedError.Invoke(-1, new Exception("ERROR Unknown SteamID"));
     }
 }
예제 #9
0
        protected override void OnReceiveData(byte[] data, ProductUserId clientUserId, int channel) {
            if (epicToMirrorIds.TryGetValue(clientUserId, out int connectionId)) {
                OnReceivedData.Invoke(connectionId, data, channel);
            } else {
                CloseP2PSessionWithUser(clientUserId);

                string productId;
                clientUserId.ToString(out productId);

                Debug.LogError("Data received from epic client thats not known " + productId);
                OnReceivedError.Invoke(-1, new Exception("ERROR Unknown product ID"));
            }
        }
예제 #10
0
 protected override void OnReceiveData(byte[] data, SteamId clientSteamID, int channel)
 {
     if (steamToMirrorIds.Contains(clientSteamID))
     {
         int connectionId = steamToMirrorIds[clientSteamID];
         OnReceivedData?.Invoke(connectionId, data, channel);
     }
     else
     {
         CloseP2PSessionWithUser(clientSteamID);
         Debug.LogError("Data received from steam client thats not known " + clientSteamID);
         OnReceivedError?.Invoke(-1, new Exception("ERROR Unknown SteamID"));
     }
 }
예제 #11
0
        public async void Listen()
        {
            while (true)
            {
                try
                {
                    TcpClient client = await _serverSocket.AcceptTcpClientAsync();

                    _timer.Stop();
                    _timer.Start();

                    string ip = client.Client.RemoteEndPoint.AddressFamily.ToString();

                    IPEndPoint remoteIpEndPoint = client.Client.RemoteEndPoint as IPEndPoint;
                    IPEndPoint localIpEndPoint  = client.Client.LocalEndPoint as IPEndPoint;


                    NetworkStream stream = client.GetStream();

                    byte[] bufferSend = StreamHelper.GetBytes(stream);

                    byte[] bufferReply = OnReceivedData?.Invoke(remoteIpEndPoint?.Address.ToString(), bufferSend);

                    if (bufferReply != null)
                    {
                        StreamHelper.SendBytes(stream, bufferReply);
                    }
                    stream.Close();
                }
                catch (SocketException socketException)
                {
                    Console.WriteLine(socketException);
                    return;
                }
                catch (InvalidOperationException invalidOperationException)
                {
                    Console.WriteLine(invalidOperationException);
                    return;
                }
                catch (Exception e)
                {
                    Console.WriteLine(e);
                    return;
                }
            }
        }
 //start to reading data from server
 void StartReadingData()
 {
     Task.Factory.StartNew(() =>
     {
         try
         {
             socket.Connect(iPEndPoint);
             while (!IsDisposed)
             {
                 byte[] bytes  = new byte[BufferSize];
                 var readCount = socket.Receive(bytes);
                 OnReceivedData?.Invoke(bytes.ToList().GetRange(0, readCount).ToArray());
             }
         }
         catch (Exception ex)
         {
             AutoLogger.LogError(ex, "UdpConnectorBase StartReadingData");
         }
     });
 }
예제 #13
0
        private async Task ReceiveLoop()
        {
            Debug.Log("ReceiveLoop Start");

            uint    readPacketSize;
            SteamId clientSteamID;

            try
            {
                byte[] receiveBuffer;
                while (!Offline)
                {
                    for (int i = 0; i < channels.Length; i++)
                    {
                        while (Receive(out readPacketSize, out clientSteamID, out receiveBuffer, i))
                        {
                            if (readPacketSize == 0)
                            {
                                continue;
                            }

                            try {
                                int connectionId = steamConnectionMap.fromSteamID[clientSteamID].connectionID;
                                // we received some data,  raise event
                                OnReceivedData?.Invoke(connectionId, receiveBuffer, i);
                            } catch (KeyNotFoundException) {
                                CloseP2PSessionWithUser(clientSteamID);
                                //we have no idea who this connection is
                                Debug.LogError("Data received from steam client thats not known " + clientSteamID);
                                OnReceivedError?.Invoke(-1, new Exception("ERROR Unknown SteamID"));
                            }
                        }
                    }
                    //not got a message - wait a bit more
                    await Task.Delay(TimeSpan.FromSeconds(secondsBetweenPolls));
                }
            }
            catch (ObjectDisposedException) { }

            Debug.Log("ReceiveLoop Stop");
        }