예제 #1
0
        public void SendFile(string host, int port, string fileName, int bufferSize)
        {
            //            IPEndPoint ipEndPoint = new IPEndPoint(Dns.GetHostEntry(host).AddressList[0], port);
            IPEndPoint ipEndPoint = new IPEndPoint(Dns.Resolve(host).AddressList[0], port);

            try {
                sender.Connect(ipEndPoint);
                NetworkStream networkStream = new NetworkStream(sender);

                FileInfo fileInfo = new FileInfo(fileName);
                FileInformation information = new FileInformation(
                    fileInfo.Name,
                    fileInfo.FullName,
                    fileInfo.Extension,
                    fileInfo.DirectoryName,
                    fileInfo.CreationTime,
                    fileInfo.LastAccessTime,
                    fileInfo.LastWriteTime,
                    Settings.LOCAL_IP.ToString()
                );

                BinaryFormatter formatter = new BinaryFormatter();
                MemoryStream memoryStream = new MemoryStream();

                formatter.Serialize(memoryStream, information);
                memoryStream.Position = 0;
                byte[] infoBuffer = new byte[memoryStream.Length];
                memoryStream.Read(infoBuffer, 0, infoBuffer.Length);

                byte[] headerLength = BitConverter.GetBytes(infoBuffer.Length);
                networkStream.Write(headerLength, 0, headerLength.Length);
                networkStream.Write(infoBuffer, 0, infoBuffer.Length);

                FileStream fileStream = new FileStream(fileName, FileMode.Open, FileAccess.Read);
                byte[] buffer = new byte[bufferSize];
                int bytesRead = 0;
                while ((bytesRead = fileStream.Read(buffer, 0, bufferSize)) != 0) {
                    networkStream.Write(buffer, 0, bytesRead);
                }

                fileStream.Close();
                memoryStream.Close();
                networkStream.Close();
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
            }
            finally
            {
                sender.Close();
            }
        }
예제 #2
0
        private void SendResultToOthers(FileInformation file, string localHost)
        {
            var Hosts = actualHosts;
            foreach (var host in Hosts) {
                try {
                    if (host.Equals(localHost))
                        continue;

                    var url = "tcp://" + host + ":" + Settings.REMOTING_SERVER_PORT + "/RemoteAddResult";
                    var remote = (RemoteAddResult) Activator.GetObject(typeof (RemoteAddResult), url);
                    remote.AddResult(file, localHost);

                    Log.WriteInfo("Передаем файл " + file.name + " на хост " + host);
                }
                catch (SocketException e) {
                    //We just remove bad host from actualHosts
                    //From hosts it will be removed
            //                    DateTime time;
            //                    hosts.TryRemove(host, out time);
            //                    new Thread(() => UpdateHostsEvent(hosts.Keys)).Start();
                    string data;
                    actualHosts.TryTake(out data);
                    Log.WriteInfo(data + " отключился от поиска");
                }
                //TODO: Concretize Remoting exception
                catch (RemotingException e) {
            //                    DateTime time;
            //                    hosts.TryRemove(host, out time);
            //                    new Thread(() => UpdateHostsEvent(hosts.Keys)).Start();
                    string data;
                    actualHosts.TryTake(out data);
                    Log.WriteInfo(data + " отключился от поиска");
                }
            }
        }
예제 #3
0
        public void AddResultByRemote(FileInformation file, string host)
        {
            if (searchResult.ContainsKey(file.name)) {
                if (searchResult[file.name].LastModifyTime.CompareTo(file.LastModifyTime) < 0) {
                    Log.WriteInfo("Файл " + file.name + " [" + searchResult[file.name].directory + "] с хоста " + searchResult[file.name].hostIp + " заменен версией с хоста " + file.hostIp + " из папки " + file.directory);

                    searchResult[file.name] = file;
                }
            } else {
                searchResult.TryAdd(file.name, file);
                if (UpdateSearchResultEvent != null)
                    UpdateSearchResultEvent(file.name);
                Log.WriteInfo("Файл " + file.name + " добавлен, хост [" + host + "]");
            }
        }