Esempio n. 1
0
        /// <summary>
        /// Handles the event raised by ClientNetwork when data is received <seealso cref="ClientNetwork.DataReceived"/>
        /// </summary>
        /// <param name="o">Always received as null, ignored</param>
        /// <param name="e">Contains the header and data received from the server by ClientNetwork <seealso cref="DataReceivedEventArgs"/></param>
        public void DataReceivedHandler(object o, DataReceivedEventArgs e)
        {
            switch (e.Header.DataType)
            {
                case Network.DataTypes.InstantMessage:
                    string fromName = "User Unknown";
                    foreach (Network.BuddyListData bud in cmbUser.Items)
                        if (bud.userID == e.Header.FromID)
                            fromName = bud.username;
                    txtHistory.Text += fromName + ": " + System.Text.UnicodeEncoding.Unicode.GetString(e.Data) + "\r\n";
                    break;
                case Network.DataTypes.WhiteBoard:
                    break;
                case Network.DataTypes.BuddyListUpdate:
                    Network.BuddyListData[] bldArray = Network.BytesToBuddyListData(e.Data);
                    foreach(Network.BuddyListData bld in bldArray)
                    {
                        for (int i = cmbUser.Items.Count - 1; i >= 0; i--)
                        {
                            if (((Network.BuddyListData)cmbUser.Items[i]).userID == bld.userID)
                                cmbUser.Items.RemoveAt(i);
                        }

                        if (bld.userState == Network.UserState.Online)
                            cmbUser.Items.Add(bld);
                    }
                    break;
            }
        }
Esempio n. 2
0
        /// <summary>
        /// Message pump for the network
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void ClientNetwork_DataReceived(object sender, DataReceivedEventArgs e)
        {
            if (InvokeRequired)
                this.Invoke(new ClientNetwork_DataReceivedDelegate(ClientNetwork_DataReceived), new object[] {sender, e});
            else
            {
                switch (e.Header.DataType)
                {
                    case Network.DataTypes.BuddyListUpdate:
                        Network.BuddyListData[] bld = Network.BytesToBuddyListData(e.Data);
                        foreach (Network.BuddyListData bd in bld)
                        {
                            for ( int i = 0 ; i < lstBuddyList.Items.Count; i++ )
                            {
                                if (((Network.BuddyListData)lstBuddyList.Items[i]).userID == bd.userID)
                                {
                                    bd.Tag = ((Network.BuddyListData)lstBuddyList.Items[i]).Tag;
                                    lstBuddyList.Items.RemoveAt(i);
                                    i = lstBuddyList.Items.Count;
                                }
                            }

                            switch (bd.userState)
                            {
                                case SDCSCommon.Network.UserState.Online:
                                    lstBuddyList.Items.Add(bd);
                                    break;
                                case SDCSCommon.Network.UserState.Offline:
                                    bd.username = "******" + bd.username;
                                    lstBuddyList.Items.Add(bd);
                                    break;
                            }
                        }
                        break;
                    case Network.DataTypes.InstantMessage:
                        foreach (Network.BuddyListData buddyDat in lstBuddyList.Items)
                            if (buddyDat.userID == e.Header.FromID)
                            {
                                createIMWindow(buddyDat, false);
                                ((IMForm)buddyDat.Tag).recIM(System.Text.UnicodeEncoding.Unicode.GetString(e.Data));
                            }

                        break;
                    case Network.DataTypes.Logout: // ToDo: Clear IM Windows and the buddy list
                        MessageBox.Show("You have been logged out because: " + System.Text.UnicodeEncoding.Unicode.GetString(e.Data));
                        showLogin();
                        break;
                }
            }
        }
Esempio n. 3
0
        /// <summary>
        /// Function for the listening thread to live in
        /// </summary>
        private static void listeningFunc()
        {
            // Records the last time a byte was received so we can check for a read timeout
            System.DateTime lastRead;

            // Loop for the lifetime of the thread
            while (true)
            {
                while (connectionStream.DataAvailable != true)
                {
                    if (connected == false)
                        return;
                    if (System.DateTime.Now - lastActivity > System.TimeSpan.FromMilliseconds(KEEP_ALIVE_TIME))
                        sendKeepAlive();
                    Thread.Sleep(100);
                }

                // Get the header
                byte[] headerBuffer = new byte[Network.HEADER_SIZE];
                lastRead = System.DateTime.Now;
                for (int i = 0; i < Network.HEADER_SIZE; i++)
                {
                    while (connectionStream.DataAvailable != true)
                    {
                        if (connected == false)
                            return;
                        if (System.DateTime.Now - lastRead > System.TimeSpan.FromMilliseconds(MAX_READ_WAIT_TIME))
                            Disconnect(true);
                        Thread.Sleep(100);
                    }
                    headerBuffer[i] = (byte)connectionStream.ReadByte();
                    lastRead = System.DateTime.Now;
                }

                // Get the data
                Network.Header head = Network.bytesToHeader(headerBuffer);
                byte[] data = new byte[head.Length];

                for (int i = 0; i < head.Length; i++)
                {
                    while (connectionStream.DataAvailable != true)
                    {
                        if (connected == false)
                            return;
                        if (System.DateTime.Now - lastRead > System.TimeSpan.FromMilliseconds(MAX_READ_WAIT_TIME))
                            Disconnect(true);
                        Thread.Sleep(100);
                    }
                    data[i] = (byte)connectionStream.ReadByte();
                    lastRead = System.DateTime.Now;
                }

                // Raise the DataReceivedEvent to let the gui know new data has arrived
                DataReceivedEventArgs eventArgs = new DataReceivedEventArgs();
                eventArgs.Header = head;
                eventArgs.Data = data;

                DataReceived(null, eventArgs);

                // Sleep to let other threads do their thing
                Thread.Sleep(100);
                lastActivity = System.DateTime.Now;
            }
        }
Esempio n. 4
0
        /// <summary>
        /// Call to disconnect from the server.
        /// </summary>
        /// <remarks>Always call disconnect before exiting the program. Failure to do so could cause a zombie thread.
        /// Also note that this sets the <see cref="Client.ClientNetwork.Username">Username</see> to blank and makes <see cref="Client.ClientNetwork.Connected">Connected</see> false.</remarks>
        /// <example>Always call this function on exit.
        /// <code>protected override void Dispose( bool disposing )
        ///	{
        ///		// Always call this before exiting the program
        ///		ClientNetwork.Disconnect();
        ///	}</code></example>
        public static void Disconnect(bool forced)
        {
            try
            {
                // Try to close the stream
                connectionStream.Close();
            }
            catch
            {}
            username = "";
            connected = false;

            if (forced)
            {
                Network.Header logoffHeader = new SDCSCommon.Network.Header();
                logoffHeader.DataType = Network.DataTypes.Logout;
                logoffHeader.Encrypted = false;
                logoffHeader.Length = 0;

                DataReceivedEventArgs e = new DataReceivedEventArgs();
                e.Header = logoffHeader;
                e.Data = System.Text.UnicodeEncoding.Unicode.GetBytes("Lost connection to server");

                DataReceived(null, e);
            }

            try
            {
                // Try to kill the thread
                listeningThread.Abort();
            }
            catch
            {}
        }
Esempio n. 5
0
 private async void Agent_OnDataReceived(object sender, DataReceivedEventArgs e)
 {
     await Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.High, () =>
         {
             this.ms.Seek(0, SeekOrigin.Begin);
             this.ms.Write(e.Buffer, 0, e.Buffer.Length);
             this.ms.Seek(0, SeekOrigin.Begin);
             img.SetSource(ms.AsRandomAccessStream());
             pic.Source = img;
             if(web.Visibility == Visibility.Visible)
             {
                 web.Visibility = Visibility.Collapsed;
             }
             pic.Visibility = Visibility.Visible;
         }
     );
 }
Esempio n. 6
0
 // Ошибка
 private static void p_ErrorDataReceived(object sender, DataReceivedEventArgs e)
 {
 }
Esempio n. 7
0
 /// <summary>
 /// Output error from python script
 /// </summary>
 /// <param name="sender"></param>
 /// <param name="e"></param>
 static void proc_ErrorDataReceived(object sender, DataReceivedEventArgs e)
 {
     Console.WriteLine("Error: {0}", e.Data);
 }