Exemplo n.º 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);
        }
Exemplo n.º 2
0
        public MessageEventArgs(string data)
        {
            if (data != null && data.Contains("|"))
            {
                data = data.Trim(); // important trim
                int loc, loc1, loc2, loc3;

                if (find4backwards(data, out loc, out loc1, out loc2, out loc3))
                {
                    id             = data.Substring(loc + 1, loc1 - loc - 1);
                    friendName     = data.Substring(loc1 + 1, loc2 - loc1 - 1);
                    friendIP       = data.Substring(loc2 + 1, loc3 - loc2 - 1);
                    textFromFriend = data.Substring(loc3 + 1, data.Length - loc3 - 1);
                    checksum       = Sock.Checksum(textFromFriend.Replace("<EOF>", ""));

                    // sanity checks
                    if (friendIP.Split('.').Count() != 4 || id.Length > 4 || id.Length == 0)
                    {
                        valid = false;
                    }
                    else if (textFromFriend.EndsWith("<EOF>"))
                    {
                        if (textFromFriend.StartsWith("<OBJECT>FILE"))
                        {
                            isFile = true;
                            try
                            {
                                string[] splitObj = textFromFriend.Split('*');
                                this.chunkId  = Convert.ToInt32(splitObj[1]);
                                this.fileName = splitObj[2];
                                this.fileData = (splitObj[3].Substring(0, splitObj[3].Length - 4 - 1));
                                valid         = true;
                            }
                            catch
                            {
                                valid = false;
                            }
                            textFromFriend = textFromFriend.Replace("<EOF>", "");
                            return;
                        }

                        textFromFriend = textFromFriend.Replace("<EOF>", "");
                        valid          = true;
                    }
                    else
                    {
                        textFromFriend = "";
                    }
                }
            }
        }
Exemplo n.º 3
0
        private MessageEventArgs processMea()
        {
            string localMsg;
            int    myIndex;

            lock (_buildMsg)
            {
                localMsg = _buildMsg.ToString();
                myIndex  = localMsg.IndexOf("<EOF>");
                if (myIndex >= 0)
                {
                    localMsg = localMsg.Substring(0, myIndex + 1 + 4).Trim();
                    _buildMsg.Remove(0, myIndex + 1 + 4);
                }
            }

            if (myIndex >= 0)
            {
                MessageEventArgs mea = new MessageEventArgs(localMsg);

                if (mea.Valid)
                {
                    // for safety
                    _buddyIp = mea.FriendIP;

                    if (this._state == MsgState.WaitingResponse || this._state == MsgState.SendAck)
                    {
                        Sock.debug(_name + ":got ACK:" + this._state.ToString() + ":Id=" + mea.Id);
                        mea.IsAck = true;
                        if (this._state == MsgState.WaitingResponse)
                        {
                            this._state = MsgState.Done;
                        }
                        return(mea);
                    }

                    if (this._imSendingAck || mea.FriendIP == myip)
                    {
                        if (this._state == MsgState.WaitingResponse)
                        {
                            this._state = MsgState.Done;
                        }
                        return(null);
                    }
                    else if (this._state == MsgState.Idle || this._state == MsgState.ReadyForRemote)
                    {
                        this._imSendingAck = true;
                        _lastAckMsg        = generate(mea.Id, mea.FriendName, mea.FriendIP, Sock.Checksum(mea.TextFromFriend)); // no need to send entire msg payload
                        _lastAckIP         = mea.FriendIP;

                        Sock.debug(_name + ":Display(sendAck)" + this._state.ToString() + ":Id=" + mea.Id);
                        this._state = MsgState.ReadyForAck;
                    }
                    else
                    {
                        return(mea);
                    }
                }
                else
                {
                    Sock.debug(_name + ": oops, invalid message received");
                }

                return(mea);
            }
            else
            {
                return(null);
            }
        }