Exemple #1
0
        /// <summary>
        /// Gets the name of the peer device using the specified socket.
        /// </summary>
        /// <param name="irdaSocket">A connected IrDA <c>Socket</c>.</param>
        /// <returns>The name of the remote device.</returns>
        /// -
        /// <remarks>
        /// This finds the name of the device to which the socket is connection,
        /// an exception will occur if the socket is not connected.
        /// </remarks>
        /// -
        /// <exception cref="T:System.ArgumentNullException">
        /// <c>s</c> is null (<c>Nothing</c> in Visual Basic).
        /// </exception>
        /// <exception cref="T:System.ArgumentOutOfRangeException">
        /// The remote device is not present in the list of discovered devices.
        /// </exception>
        /// <exception cref="T:System.InvalidOperationException">
        /// The socket is not connected.
        /// </exception>
        public static string GetRemoteMachineName(Socket irdaSocket)
        {
            if (irdaSocket == null)
            {
                throw new ArgumentNullException("irdaSocket", "GetRemoteMachineName requires a valid Socket");
            }
            if (!irdaSocket.Connected)
            {
                throw new InvalidOperationException("The socket must be connected to a device to get the remote machine name.");
            }
            //get remote endpoint
            IrDAEndPoint ep = (IrDAEndPoint)irdaSocket.RemoteEndPoint;
            IrDAAddress  a  = ep.Address;

            //lookup devices and search for match
            IrDADeviceInfo[] idia = DiscoverDevices(10, irdaSocket);

            foreach (IrDADeviceInfo idi in idia)
            {
                if (a == idi.DeviceAddress)
                {
                    return(idi.DeviceName);
                }
            }

            // See unit-test "CheckExceptionThrownByGetRemoteMachineName".
            throw ExceptionFactory.ArgumentOutOfRangeException(null, "No matching device discovered.");
        }
        //--------

        private static void VerifyWriteSpaceRemaining(int requiredLength, byte[] buffer, int offset)
        {
            int spaceRemaining = buffer.Length - offset;

            if (requiredLength > spaceRemaining)
            {
                // I never know what exception to throw in a 'overrun' case.
                // The 'paramName' argument ("buffer") below is of the top-level method 'CreateServiceRecord'.
                throw ExceptionFactory.ArgumentOutOfRangeException(
                          "buffer",
                          "The record bytes are longer that the supplied byte array buffer.");
            }
        }