⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 phone.cs

📁 Gibphone is CSharp Program, it can tell you how to design p2p chat.
💻 CS
字号:
using System;
using System.Collections.Generic;
using System.Text;
using System.Net;
using System.Drawing;
using GPCore.Protocols;
using GPCore.Vocoders;
using GPCore.NetworkProtocols;
using GPCore.Args;
using System.Reflection;

namespace GPCore.Phones
{

    /// <summary>
    /// An interface that extends IProtocol to allow for voice
    /// conversations
    /// </summary>
    public abstract class Phone : Protocol, IPhone
    {
        /// <summary>
        /// The vocoder for this phone.
        /// </summary>
        protected IVocoder vox;

        /// <summary>
        /// The network protocol for this phone.
        /// </summary>
        protected INetworkProtocol nox; 

        /// <summary>
        /// Starts a new phone with the given vocoder and network protocol
        /// </summary>
        virtual public void Initialize(IVocoder vox, INetworkProtocol nox)
        {
            this.nox = nox;
            this.vox = vox;
            nox.DataReceived += new NetworkDelegate(nox_DataReceived);
            vox.DataEncoded += new ByteDelegate(vox_DataEncoded);
            vox.DataDecoded += new ByteDelegate(vox_DataDecoded);
            Sound.GetEvent("SoundCaptured").AddEventHandler(this, new ByteDelegate(sound_DataReceived));
        }


        
        protected void StartSound(int samplesize, int samplerate, int channels)
        {
            Sound.GetMethod("Initialize").Invoke(null, new object[] { samplesize, samplerate, channels });
        }

        protected void StopSound()
        {
            Sound.GetMethod("Dispose").Invoke(null, null);
        }

        /* 
         * Vocoder has two parts: Encode and Decode
         * The data flows as follows:
         * sound_DataReceived(IMPL) -> Encode -> vox_DataEncoded -> Send
         * nox_DataReceived -> Decode -> vox_DataDecoded(IMPL) -> Play
         * These four methods are listed below.
         * IMPL means implemented already, due to simplicity.
         */

        /// <summary>
        /// This is called every time the network protocol recieves data that needs to be processed.
        /// </summary>
        protected abstract void nox_DataReceived(byte[] packetBytes, IPAddress From);

        /// <summary>
        /// This is called every time the microphone recieves data.
        /// </summary>
        /// <remarks>
        /// The default implementation just queues the data for encoding.
        /// </remarks>
        protected virtual void sound_DataReceived(byte[] a)
        {
            vox.Encode(a);
        }

        /// <summary>
        /// Called every time the vocoder encodes data.
        /// </summary>
        protected abstract void vox_DataEncoded(byte[] a);

        /// <summary>
        /// Called when the vocoder has data that is ready to be sent to the speakers.
        /// </summary>
        protected virtual void vox_DataDecoded(byte[] b)
        {
            SendToSpeakers(b);
        }

        internal static Type Sound = typeof(Sound);

        /// <summary>
        /// Send data to the speakers.
        /// </summary>
        protected void SendToSpeakers(byte[] a)
        {
            Sound.GetMethod("SoundPlayback").Invoke(null, new object[] { a });
        }

        /// <summary>
        /// Start (request) a voice conversation with someone
        /// </summary>
        /// <param name="Username">The Username of the person you want to talk with</param>
        public abstract void StartTalking(SqlAccount Username);

        /// <summary>
        /// Stop a voice conversation with someone
        /// </summary>
        /// <param name="Username">The Username of the person you want to end the conversation with</param>
        public abstract void StopTalking(SqlAccount Username);

        /// <summary>
        /// Returns the state of the given Account to check whether or not
        /// the account is currently talking to you.
        /// </summary>
        /// <param name="Username">The Account to check the state of.</param>
        /// <returns>Either Call or Not In Call depending on that accounts state.</returns>
        public abstract string IsInCall(SqlAccount Username);

        /// <summary>
        /// Accept a chat request
        /// </summary>
        /// <param name="Username">The person whom you are accepting</param>
        public abstract void Accept(String Username);

        /// <summary>
        /// Deny a chat request
        /// </summary>
        /// <param name="Username">The person whom you are denying</param>
        public abstract void Deny(String Username);
        
        /// <summary>
        /// Fired when a person requests to speak with you
        /// </summary>
        public static Event<IPhone, UsernameEventArgs> Requested = new Event<IPhone, UsernameEventArgs>("Requested");

        /// <summary>
        /// Fired when a person accepts your request
        /// </summary>
        public static Event<IPhone, UsernameEventArgs> Accepted = new Event<IPhone, UsernameEventArgs>("Accepted");

        /// <summary>
        /// Fired when a person denies your request
        /// </summary>
        public static Event<IPhone, UsernameEventArgs> Denied = new Event<IPhone, UsernameEventArgs>("Denied");

        /// <summary>
        /// Fired when a person closes the connection with you
        /// </summary>
        public static Event<IPhone, UsernameEventArgs> Closed = new Event<IPhone, UsernameEventArgs>("Closed");

        public override abstract SqlProtocolAccountList Accounts
        {
            get;
        }

        public override abstract string Username
        {
            get;
            set;
        }

        public override abstract string Version
        {
            get;
        }

        public override abstract bool Connected
        {
            get;
        }

        public override abstract Image DefaultBuddyIcon
        {
            get;
        }

        #region IDisposable Members
        /// <summary>
        /// Disposes of this objects resources.
        /// </summary>
        public virtual void Dispose()
        {
            MethodInfo mi = Sound.GetMethod("Dispose");
                mi.Invoke(null, null);
            nox.Dispose();
            vox.Dispose();
        }

        #endregion

        
    }
     /// <summary>
    /// An interface that extends IProtocol to allow for voice
    /// conversations
    /// </summary>
    public interface IPhone : IProtocol, IDisposable
    {
        /// <summary>
        /// Initializes an IPhone with the given Vocoder and NetworkProtocol
        /// </summary>
        /// <param name="vox"></param>
        /// <param name="nox"></param>
        void Initialize(IVocoder vox, INetworkProtocol nox);

        /// <summary>
        /// Start (request) a voice conversation with someone
        /// </summary>
        /// <param name="Username">The Username of the person you want to talk with</param>
        [GPStateMethod("Start Call", typeof(SqlAccount), "In Call", false,"IsInCall")]
        void StartTalking(SqlAccount Username);

        /// <summary>
        /// Stop a voice conversation with someone
        /// </summary>
        /// <param name="Username">The Username of the person you want to end the conversation with</param>
        [GPStateMethod("End Call", typeof(SqlAccount), "In Call", true, "IsInCall")]
        void StopTalking(SqlAccount Username);

        /// <summary>
        /// Returns the state of the given Account to check whether or not
        /// the account is currently talking to you.
        /// </summary>
        /// <param name="Username">The Account to check the state of.</param>
        /// <returns>Either Call or Not In Call depending on that accounts state.</returns>
        string IsInCall(SqlAccount Username);

        /// <summary>
        /// Accept a chat request
        /// </summary>
        /// <param name="Username">The person whom you are accepting</param>
        void Accept(String Username);

        /// <summary>
        /// Deny a chat request
        /// </summary>
        /// <param name="Username">The person whom you are denying</param>
        void Deny(String Username);

    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -