Пример #1
0
 /// <summary>
 /// Queues a <see cref="DirectIM"/> for sending
 /// </summary>
 public void SendMessage(DirectIM message)
 {
     lock (_messagequeue)
     {
         _messagequeue.Enqueue(message);
     }
 }
Пример #2
0
 /// <summary>
 /// Dequeues a single message and sends it immediately
 /// </summary>
 private void MessageSendCallback(object state)
 {
     lock (_messagequeue)
     {
         if (_messagequeue.Count > 0)
         {
             DirectIM message = _messagequeue.Dequeue();
             // Turn off the timer while a message is being sent, it...could take a while
             _messagetimer.Change(Timeout.Infinite, Timeout.Infinite);
             SendPacket(message.ToByteArray(), new AsyncCallback(
                            delegate
             {
                 // If the message has attachments, start a writer to spool out the data, reset the timer when it's done
                 if (message.Attachments.Count > 0)
                 {
                     DirectIMDataWriter writer =
                         new DirectIMDataWriter(this,
                                                message);
                     writer.DataWriterComplete +=
                         new DataWriterCompleteHandler(
                             delegate
                     {
                         _messagetimer.Change
                             (500, 500);
                     });
                     writer.Write();
                 }
                 else
                 {
                     // Reset the timer right away
                     _messagetimer.Change(500, 500);
                 }
             }));
         }
     }
 }
Пример #3
0
 /// <summary>
 /// Initializes a new DirectIMDataReader
 /// </summary>
 public DirectIMDataWriter(DirectIMConnection connection, DirectIM message)
 {
     _conn = connection;
     _fullbuffer = message.AttachmentsToByteArray();
 }
Пример #4
0
 /// <summary>
 /// Initializes a new DirectIMDataReader
 /// </summary>
 public DirectIMDataReader(DirectIMConnection connection, DirectIM message)
 {
     _conn = connection;
     _conn.ReadPacketAsyncComplete += new ConnectionReadPacketHandler(_conn_ReadPacketAsyncComplete);
     _msg = message;
 }
Пример #5
0
        /// <summary>
        /// Processes the received message
        /// </summary>
        private void EndReadHeader(IAsyncResult res)
        {
            byte[] odcheader = null;
            try
            {
                odcheader = (byte[])res.AsyncState;
                socket.EndRead(res);
            }
            catch (Exception ex)
            {
                Logging.WriteString(String.Format("Exception in DirectIMConnection.EndReadHeader: {0}", ex));
                DisconnectFromServer(true);
            }

            // Verify that this is an ODC header
            if (Encoding.ASCII.GetString(odcheader, 0, 4) != "ODC2")
            {
                // Huh...
                return;
            }

            ushort datalen = (ushort)((odcheader[4] << 8) | odcheader[5]);
            if (datalen < 6)
            {
                // Oh
                return;
            }

            ByteStream messageheader = ReadPacket(datalen - 6);
            if (messageheader == null)
            {
                // Tum ta tiddily tumpa turr
                return;
            }

            // Extract various members from the message header
            messageheader.AdvanceToPosition(6);
            byte[] cookie = messageheader.ReadByteArray(8);
            messageheader.AdvanceToPosition(22);
            uint datalength = messageheader.ReadUint();
            ushort charset = messageheader.ReadUshort();
            ushort subcharset = messageheader.ReadUshort();
            DirectIMFlags flags = (DirectIMFlags)messageheader.ReadUint();
            messageheader.AdvanceToPosition(38);
            string screenname = messageheader.ReadString(16, Encoding.ASCII);

            if ((flags & DirectIMFlags.TypingPacket) != 0)
            {
                // Determine the type of typing packet this is
                if ((flags & DirectIMFlags.UserTyping) != 0)
                {
                    //_parent.OnTypingNotification(screenname, TypingNotification.TypingStarted);
                }
                else if ((flags & DirectIMFlags.UserTyped) != 0)
                {
                    //_parent.OnTypingNotification(screenname, TypingNotification.TextTyped);
                }
                else
                {
                    // TODO:  restore these
                    // _parent.OnTypingNotification(screenname, TypingNotification.TypingFinished);
                }

                // Probably no data, but read it in anyway to make sure we're not missing anything
                ReadPacket((int)datalength);
            }
            else if ((flags & DirectIMFlags.ConfirmationPacket) != 0 && datalength == 0)
            {
                // Do we really do anything here?  I don't think so.
            }
            else
            {
                // Create a new instant message
                DirectIM dim = new DirectIM(Other.ScreenName, this);
                dim.Cookie = csammisrun.OscarLib.Cookie.GetReceivedCookie(cookie);
                dim.IsAutoResponse = ((flags & DirectIMFlags.AutoResponse) != 0);
                dim.Encoding = IM.GetEncodingFromCharset(charset, subcharset);

                // Create a spooler to incrementally read in a DirectIM packet,
                // then restart the read sequence when it's done
                DirectIMDataReader reader = new DirectIMDataReader(this, dim);
                reader.DataReaderComplete += new DataReaderCompleteHandler(delegate
                                                                               {
                                                                                   parent.OnDirectIMReceived(
                                                                                       reader.Message);
                                                                                   reader.Dispose();
                                                                                   ReadHeader();
                                                                               });
                reader.Read(datalength);
                return;
            }

            // Restart the read sequence
            ReadHeader();
        }
Пример #6
0
 /// <summary>
 /// Queues a <see cref="DirectIM"/> for sending
 /// </summary>
 public void SendMessage(DirectIM message)
 {
     lock (_messagequeue)
     {
         _messagequeue.Enqueue(message);
     }
 }
Пример #7
0
 private void sess_DirectIMReceived(Session sess, DirectIM message)
 {
     Console.WriteLine("DirectIM Message from {1}: {0}", message.Message, message.ScreenName);
     sess.Messages.SendMessage(message.ScreenName, "Your momma!", OutgoingMessageFlags.None);
 }
Пример #8
0
 /// <summary>
 /// Initializes a new DirectIMDataReader
 /// </summary>
 public DirectIMDataWriter(DirectIMConnection connection, DirectIM message)
 {
     _conn       = connection;
     _fullbuffer = message.AttachmentsToByteArray();
 }
Пример #9
0
 /// <summary>
 /// Initializes a new DirectIMDataReader
 /// </summary>
 public DirectIMDataReader(DirectIMConnection connection, DirectIM message)
 {
     _conn = connection;
     _conn.ReadPacketAsyncComplete += new ConnectionReadPacketHandler(_conn_ReadPacketAsyncComplete);
     _msg = message;
 }
Пример #10
0
        /// <summary>
        /// Processes the received message
        /// </summary>
        private void EndReadHeader(IAsyncResult res)
        {
            byte[] odcheader = null;
            try
            {
                odcheader = (byte[])res.AsyncState;
                socket.EndRead(res);
            }
            catch (Exception ex)
            {
                Logging.WriteString(String.Format("Exception in DirectIMConnection.EndReadHeader: {0}", ex));
                DisconnectFromServer(true);
            }

            // Verify that this is an ODC header
            if (Encoding.ASCII.GetString(odcheader, 0, 4) != "ODC2")
            {
                // Huh...
                return;
            }

            ushort datalen = (ushort)((odcheader[4] << 8) | odcheader[5]);

            if (datalen < 6)
            {
                // Oh
                return;
            }

            ByteStream messageheader = ReadPacket(datalen - 6);

            if (messageheader == null)
            {
                // Tum ta tiddily tumpa turr
                return;
            }

            // Extract various members from the message header
            messageheader.AdvanceToPosition(6);
            byte[] cookie = messageheader.ReadByteArray(8);
            messageheader.AdvanceToPosition(22);
            uint          datalength = messageheader.ReadUint();
            ushort        charset    = messageheader.ReadUshort();
            ushort        subcharset = messageheader.ReadUshort();
            DirectIMFlags flags      = (DirectIMFlags)messageheader.ReadUint();

            messageheader.AdvanceToPosition(38);
            string screenname = messageheader.ReadString(16, Encoding.ASCII);

            if ((flags & DirectIMFlags.TypingPacket) != 0)
            {
                // Determine the type of typing packet this is
                if ((flags & DirectIMFlags.UserTyping) != 0)
                {
                    //_parent.OnTypingNotification(screenname, TypingNotification.TypingStarted);
                }
                else if ((flags & DirectIMFlags.UserTyped) != 0)
                {
                    //_parent.OnTypingNotification(screenname, TypingNotification.TextTyped);
                }
                else
                {
                    // TODO:  restore these
                    // _parent.OnTypingNotification(screenname, TypingNotification.TypingFinished);
                }

                // Probably no data, but read it in anyway to make sure we're not missing anything
                ReadPacket((int)datalength);
            }
            else if ((flags & DirectIMFlags.ConfirmationPacket) != 0 && datalength == 0)
            {
                // Do we really do anything here?  I don't think so.
            }
            else
            {
                // Create a new instant message
                DirectIM dim = new DirectIM(Other.ScreenName, this);
                dim.Cookie         = csammisrun.OscarLib.Cookie.GetReceivedCookie(cookie);
                dim.IsAutoResponse = ((flags & DirectIMFlags.AutoResponse) != 0);
                dim.Encoding       = IM.GetEncodingFromCharset(charset, subcharset);

                // Create a spooler to incrementally read in a DirectIM packet,
                // then restart the read sequence when it's done
                DirectIMDataReader reader = new DirectIMDataReader(this, dim);
                reader.DataReaderComplete += new DataReaderCompleteHandler(delegate
                {
                    parent.OnDirectIMReceived(
                        reader.Message);
                    reader.Dispose();
                    ReadHeader();
                });
                reader.Read(datalength);
                return;
            }

            // Restart the read sequence
            ReadHeader();
        }