/// <summary> /// Synchronously reads a packet from a Rendezvous proxy connection /// </summary> private RendezvousProxyPacket ReadProxyPacket() { int bytesreceived = 0; byte[] header = new byte[12]; while (bytesreceived < header.Length) { bytesreceived += socket.Read(header, bytesreceived, header.Length - bytesreceived); } Logging.DumpFLAP(header, "Rendezvous proxy read packet header"); RendezvousProxyPacket retval = new RendezvousProxyPacket(); using (ByteStream bstream = new ByteStream(header)) { retval.Data = new byte[bstream.ReadUshort() - 10]; bstream.AdvanceToPosition(4); retval.Command = (RendezvousProxyCommand)bstream.ReadUshort(); } bytesreceived = 0; while (bytesreceived < retval.Data.Length) { bytesreceived += socket.Read(retval.Data, bytesreceived, retval.Data.Length - bytesreceived); } Logging.DumpFLAP(retval.Data, "Rendezvous proxy read packet data"); return(retval); }
/// <summary> /// Synchronously reads a packet from a Rendezvous proxy connection /// </summary> private RendezvousProxyPacket ReadProxyPacket() { int bytesreceived = 0; byte[] header = new byte[12]; while (bytesreceived < header.Length) { bytesreceived += socket.Read(header, bytesreceived, header.Length - bytesreceived); } Logging.DumpFLAP(header, "Rendezvous proxy read packet header"); RendezvousProxyPacket retval = new RendezvousProxyPacket(); using (ByteStream bstream = new ByteStream(header)) { retval.Data = new byte[bstream.ReadUshort() - 10]; bstream.AdvanceToPosition(4); retval.Command = (RendezvousProxyCommand)bstream.ReadUshort(); } bytesreceived = 0; while (bytesreceived < retval.Data.Length) { bytesreceived += socket.Read(retval.Data, bytesreceived, retval.Data.Length - bytesreceived); } Logging.DumpFLAP(retval.Data, "Rendezvous proxy read packet data"); return retval; }
/// <summary> /// Receives the PROMPT message and responds with an ACK message, then prepares the transfer socket to receive data /// </summary> private void BeginReceiveFile() { byte[] filetransferheader = null; int index = 0; // Read in 256 bytes, PROMPT type and blank cookie Logging.WriteString("In BeginReceiveFile()"); filetransferheader = new byte[256]; try { while (index < filetransferheader.Length) { index += socket.Read(filetransferheader, index, filetransferheader.Length - index); } } catch (Exception ex) { string message = "Error negotiating file transfer:" + Environ.NewLine + ex.Message; CancelFileTransfer(message); } Logging.WriteString("Got file transfer header"); using (ByteStream bstream = new ByteStream(filetransferheader)) { bstream.AdvanceToPosition(8); bstream.ReadFileTransferInformation(this); } // Respond with the same header, but with the ACK type and the ICBM cookie set index = 6; Marshal.InsertUshort(filetransferheader, 0x0202, ref index); Marshal.CopyArray(Cookie.ToByteArray(), filetransferheader, 0, ref index); Logging.WriteString("Rewrote file transfer header"); index = 0; try { while (index < filetransferheader.Length) { index += socket.Write(filetransferheader, index, filetransferheader.Length - index); } } catch (Exception ex) { string message = "Error negotiating file transfer:" + Environ.NewLine + ex.Message; CancelFileTransfer(message); } // Open the file for writing try { _datachunk = new byte[8192]; _datastream = (new StreamWriter(LocalFileName, false)).BaseStream; _streamposition = 0; } catch (Exception) { throw new Exception("Can't open target file for writing"); } Logging.WriteString("File opened for writing"); // Signal the parent session that the file transfer has started parent.OnFileTransferProgress(Cookie, 0, FileHeader.Size); // Start receiving data socket.BeginRead(_datachunk, 0, _datachunk.Length, new AsyncCallback(SendFileTransferReceive), null); //socket.BeginReceive(_datachunk, 0, _datachunk.Length, SocketFlags.None, new AsyncCallback(SendFileTransferReceive), null); }
/// <summary> /// Verifies that a JPEG File Interchange Format image falls inside the /// AOL-required dimensions: between 48x48 and 50x50 /// </summary> /// <param name="fs"> /// An opened <see cref="FileStream"/> two bytes into the JFIF. /// </param> /// <returns> /// <c>true</c> if the JFIF fits in the required dimensions, /// <c>false</c> otherwise. /// </returns> private static bool VerifyJFIF(FileStream fs) { // Scroll to the Start Frame 0 position in the image bool foundframe0 = false; while (!foundframe0 && fs.Position < fs.Length) { if (fs.ReadByte() == 0xFF && fs.ReadByte() == 0xC0) break; } if (fs.Position == fs.Length) return false; // Three skip bytes, then the next four are the width and height (in NBO format) byte[] size = new byte[7]; if (fs.Read(size, 0, size.Length) != size.Length) return false; ushort height = 0, width = 0; using (ByteStream bstream = new ByteStream(size)) { bstream.AdvanceToPosition(3); height = bstream.ReadUshort(); width = bstream.ReadUshort(); } return (width >= 48 && width <= 50) && (height >= 48 && height <= 50); }
/// <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(); }