示例#1
0
        /// <summary>
        /// call back handler for UDP calls
        /// </summary>
        /// <param name="obj">response object</param>
        /// <param name="remoteIpAddress">from IP address</param>
        private static void callback(object obj, string remoteIpAddress)
        {
            var responseBytes = obj as byte[];

            if (responseBytes.Length < 3)
            {
                string response = Encoding.UTF8.GetString(responseBytes, 0, responseBytes.Length);
                if (response == "C")
                {
                    if (ConnectionLifeCycle.CurrentConnectionState == ConnectionState.Connected)
                    {
                        // ignore this message
                        // already connected
                        RejectCommand(remoteIpAddress);
                    }
                    else
                    {
                        ConnectionLifeCycle.CurrentConnectionState = ConnectionState.Connected;
                        ConnectionLifeCycle.CurrentConnected       = remoteIpAddress;
                        CallBackForView?.Invoke(ConnectionLifeCycle.CurrentConnectionState);
                        ConnectResponseCommand(remoteIpAddress);
                        audioHandler.StartRecordingAsync(SendAudioCallback);
                    }
                }
                else if (response == "CR")
                {
                    ConnectionLifeCycle.CurrentConnectionState = ConnectionState.Connected;
                    ConnectionLifeCycle.CurrentConnected       = remoteIpAddress;
                    CallBackForView?.Invoke(ConnectionLifeCycle.CurrentConnectionState);
                    // connected successfully
                    audioHandler.StartRecordingAsync(SendAudioCallback);
                }
                else if (response == "D")
                {
                    audioHandler.StopRecording();
                    ConnectionLifeCycle.CurrentConnectionState = ConnectionState.NotConnected;
                    ConnectionLifeCycle.CurrentConnected       = string.Empty;
                    MessagingCenter.Send(new DisconnectMessage(), "Disconnect");
                    //CallBackForView?.Invoke(ConnectionLifeCycle.CurrentConnectionState);
                    // disconnected successfully
                }
                else if (response == "R")
                {
                    ConnectionLifeCycle.CurrentConnectionState = ConnectionState.NotConnected;
                    ConnectionLifeCycle.CurrentConnected       = string.Empty;
                }
                else if (response == "UR")
                {
                    string name = AppStorage.Name;
                    // send this name back to IP Address requested
                    if (name.Length < 3)
                    {
                        name = string.Concat(name, "0000".Substring(0, 3 - name.Length));
                    }
                    NameResponseCommand(remoteIpAddress, name);
                }
            }
            // request name of the client and validating connection
            else if (responseBytes.Length < 20)
            {
                string response = Encoding.UTF8.GetString(responseBytes, 0, responseBytes.Length);
                if (response.StartsWith("NR-"))
                {
                    string remoteName       = response.Split(new char[] { '-' })[1];
                    var    ipaddressService = DependencyService.Get <IIPAddressManager>();
                    string ipaddress        = ipaddressService.GetIPAddress();
                    if (remoteIpAddress != ipaddress)
                    {
                        CallBackForUserDetails?.Invoke(new User()
                        {
                            IPAddress = remoteIpAddress, Name = remoteName
                        });
                    }
                }
            }
            else // voice message
            {
                audioHandler.PlayAudio(responseBytes);
            }
        }