示例#1
0
        private bool handleFileIO(byte[] buffer, int length, string filepath, int chunk, string ip, string fullname)
        {
            string fileData = StringCompressor.ToHexString(buffer, length);
            string m        = "<OBJECT>" + "FILE*" + chunk.ToString() + "*" + Path.GetFileName(filepath).Replace("?", "_") + "*" + fileData;
            string chk      = Sock.Checksum(Sock.Checksum(m)); // do it twice, trust me

            _e = null;
            AckDone.Reset();
            Sock.SendToBuddy(userName, false, ip, fullname, m, null);

            // wait for confirmations
            AckDone.WaitOne(5000);

            if (!isExit && (_e == null || !_e.Valid || _e.Checksum != chk))
            {
                // try to repeat once
                _e = null;
                AckDone.Reset();
                Sock.SendToBuddy(userName, false, ip, fullname, m, null);
                // wait for confirmations
                AckDone.WaitOne(5000);

                if (_e == null || !_e.Valid || _e.Checksum != chk)
                {
                    return(false);
                }
            }

            return(true);
        }
示例#2
0
        private void Sock_NewDataReceivedEventHandler(MessageEventArgs e)
        {
            this.Invoke((MethodInvoker) delegate
            {
                try
                {
                    if (this.comboBoxUsers.Items.Contains(e.FriendName))
                    {
                        //    int index = this.comboBoxUsers.Items.IndexOf(e.FriendName);
                        //    if (index > -1)
                        //        this.comboBoxUsers.SelectedIndex = index;
                    }
                    else
                    {
                        this.comboBoxUsers.Items.Add(e.FriendName);
                        this.buddyList[e.FriendName] = e.FriendIP;

                        // auto select combo only if first buddy found
                        if (this.comboBoxUsers.Items.Count <= 2)
                        {
                            int index = this.comboBoxUsers.Items.IndexOf(e.FriendName);
                            if (index > -1)
                            {
                                this.comboBoxUsers.SelectedIndex = index;
                            }
                        }
                    }

                    if (e.IsFile)
                    {
                        if (e.ChunkId == -1)
                        {
                            if (currentChunkId[e.FriendIP] != -1)
                            {
                                byte[] data = StringCompressor.ToHexBytes(currentChunk[e.FriendIP]);
                                currentChunk[e.FriendIP] = "";
                                if (!File.Exists(currentTempFile[e.FriendIP]))
                                {
                                    File.WriteAllBytes(currentTempFile[e.FriendIP], data);
                                }
                                else
                                {
                                    appendAllBytes(currentTempFile[e.FriendIP], data);
                                }
                            }

                            appendText(richTextBoxChatOut, e.FriendName + ":\tSent a file->File_Waiting: ", Color.LightGreen);
                            richTextBoxChatOut.InsertLink(e.FileName + "   ");
                            if (linkList.ContainsKey(e.FileName + "   "))
                            {
                                linkList[e.FileName + "   "] = currentTempFile[e.FriendIP];
                            }
                            else
                            {
                                linkList.Add(e.FileName + "   ", currentTempFile[e.FriendIP]);
                            }
                            richTextBoxChatOut.AppendText("      " + Environment.NewLine);
                            richTextBoxChatOut.ScrollToCaret();
                        }
                        else
                        {
                            if (e.ChunkId == 0)
                            {
                                currentTempFile[e.FriendIP] = Path.GetTempFileName();
                                currentChunk[e.FriendIP]    = e.FileData;
                                currentChunkId[e.FriendIP]  = 0;
                            }
                            else
                            {
                                // if this is a different chunk than before write it out
                                if (currentChunkId[e.FriendIP] != e.ChunkId)
                                {
                                    byte[] data = StringCompressor.ToHexBytes(currentChunk[e.FriendIP]);
                                    currentChunk[e.FriendIP] = "";
                                    if (!File.Exists(currentTempFile[e.FriendIP]))
                                    {
                                        File.WriteAllBytes(currentTempFile[e.FriendIP], data);
                                    }
                                    else
                                    {
                                        appendAllBytes(currentTempFile[e.FriendIP], data);
                                    }
                                }
                                currentChunk[e.FriendIP]   = e.FileData;
                                currentChunkId[e.FriendIP] = e.ChunkId;
                            }
                        }
                    }
                    else if (e.TextFromFriend.StartsWith("<REMOTE>"))
                    {
                        processTypedMessage(e.TextFromFriend.Substring(8));
                    }
                    else
                    {
                        appendText(richTextBoxChatOut, e.FriendName + ":\t", Color.LightGreen);
                        appendText(richTextBoxChatOut, e.TextFromFriend + Environment.NewLine, Color.LightBlue);
                        // scroll it automatically
                        richTextBoxChatOut.ScrollToCaret();
                    }

                    // make the form blink on taskbar if not already active
                    User32.FlashWindow(this.Handle, false);
                }
                catch { }
            });
        }