} // BasicProp /// <summary>Соединение с сервером</summary> public bool ConnectWithServer() { BinaryClientFormatterSinkProvider bcfs = new BinaryClientFormatterSinkProvider(); BinaryServerFormatterSinkProvider bsfs = new BinaryServerFormatterSinkProvider { TypeFilterLevel = TypeFilterLevel.Full }; IDictionary id = new Hashtable { ["port"] = 0, ["typeFilterLevel"] = TypeFilterLevel.Full, ["name"] = Guid.NewGuid().ToString() }; TcpChannel tcc = new TcpChannel(id, bcfs, bsfs); ChannelServices.RegisterChannel(tcc, false); string url = $@"tcp://{LoginData.HostName}:{LoginData.Port}/RemoteMsgObj"; object RemoteObj = Activator.GetObject(typeof(MyMessageObject), url); Proxy = (MyMessageObject)RemoteObj; try { Client = new CallBackClient(); Proxy.SetClients(Client); Path = $@"D:\\Storage\{LoginData.UserName}"; Proxy.CreateDir(Path); ShowFilesAndDirs(Path); return true; } catch (SocketException) { return false; } // try-catch } // ConnectWithServer
private void m_RemoveTimedOutCallBacks() { try { ArrayList timedOutCalledBack = null; lock (UploadList.SyncRoot) { try { foreach (CClient Client in UploadList) { if ((Client.UploadState == Protocol.UploadState.WaitCallBack) && (Client.CallBackTimedOut)) { if (timedOutCalledBack == null) { timedOutCalledBack = new ArrayList(); } timedOutCalledBack.Add(Client); } } } catch (Exception e) { Debug.WriteLine(e.ToString()); } } if (timedOutCalledBack != null) { foreach (CClient CallBackClient in timedOutCalledBack) { CallBackClient.OnDisconnect((byte)Protocol.ConnectionReason.CannotConnect); } } } catch (Exception e) { Debug.WriteLine(e.ToString()); } }
} // MainWindow private void StartServer() { BinaryServerFormatterSinkProvider serverFormatter = new BinaryServerFormatterSinkProvider { TypeFilterLevel = TypeFilterLevel.Full }; IDictionary ht = new Hashtable { ["name"] = "ServerFileStorage", ["port"] = 32469 }; TcpChannel channel = new TcpChannel(ht, null, serverFormatter); ChannelServices.RegisterChannel(channel, false); RemotingConfiguration.RegisterWellKnownServiceType(typeof(MyMessageObject), "RemoteMsgObj", WellKnownObjectMode.Singleton); CallBackClient client = new CallBackClient(); client.OnSendClientsList += new ClientsListHandler(Msg_OnSendClientsList); client.userName = "******"; MyMessageObject proxy = GetProxy(); proxy.SetClients(client); Status.Text = "Сервер запущен. Ожидание подключений..."; } // StartServer
public void Process() { //Search more sources in the connected server // if (((DateTime.Now-m_LastTCPSearch)>new TimeSpan(0,Protocol.ReaskClient,0))&& // (CKernel.Preferences.GetInt("MaxSourcesPerFile")>m_ArrayList.Count-5)) // { // if ((CKernel.ServersList.ActiveServer!=null)&&(m_Element!=null)&&(m_Element.File!=null)) // { // CKernel.ServersList.ActiveServer.RequestSources(m_Element.File.FileHash); // m_LastTCPSearch=DateTime.Now; // Debug.Write("Asking for file "+ m_Element.File.GetFileName() +"\n"); // } // } float aux_VelocidadDescarga = 0; ushort aux_nDownloadingClients = 0; ushort aux_nUsableClients = 0; float factor; ArrayList timedOutCalledBack = null; lock (m_ArrayList.SyncRoot) { try { foreach (CClient Client in m_ArrayList) { factor = 1.0F; if (Client.DownloadState == Protocol.DownloadState.NoNeededParts) { factor = 2.0F; } else if ((Client.SoftwareID == (uint)Protocol.Client.mlDonkey) && (Client.DownloadTries > 0)) { factor = 0.55F; } else if ((Client.QRDownload == 0) && (Client.DownloadTries > 0)) { factor = 0.75F; } if ((DateTime.Now - Client.LastDownloadRequest > new TimeSpan(0, 0, (int)((float)Protocol.MinClientReask * factor) - 1, 0)) && (Client.DownloadState != Protocol.DownloadState.Connecting) && (Client.DownloadState != Protocol.DownloadState.Downloading) && (Client.DownloadState != Protocol.DownloadState.Connected) && (Client.DownloadState != Protocol.DownloadState.WaitCallBack)) { if (!Client.AskingUDP) { Client.TryUDPDownload(); } else { if (DateTime.Now - Client.LastDownloadRequest > new TimeSpan(0, 0, (int)((float)Protocol.MinClientReask * factor), 0)) { Client.TryDownload(); } } } if ((Client.DownloadState == Protocol.DownloadState.Downloading)) { aux_nDownloadingClients++; Client.UpdateDownloadSpeed(); aux_VelocidadDescarga += Client.DownloadSpeed; } if ((Client.DownloadState == Protocol.DownloadState.WaitCallBack) //||(Client.UploadState==Protocol.UploadState.WaitCallBack)) && (Client.CallBackTimedOut)) { if (timedOutCalledBack == null) { timedOutCalledBack = new ArrayList(); } timedOutCalledBack.Add(Client); } if ((Client.DownloadState != Protocol.DownloadState.None) && (Client.DownloadState != Protocol.DownloadState.NoNeededParts)) { aux_nUsableClients++; } } } catch (InvalidOperationException e) { //in some vary rare case the enumerating the sources collection can fail //possible errors a can not create a socket or similar //we catch this execption and continue since we will process sources again in 1 second //if entering here is not rare then we must delete the sources with this error outside the loop #if DEBUG CLog.Log(Types.Constants.Log.Verbose, "Error enumerating sources collection in SourcesList.Process " + e.ToString()); #endif Debug.WriteLine(e.ToString()); } } // Disconnect may modify the sources list collection, so we disconnect outside the loop if (timedOutCalledBack != null) { foreach (CClient CallBackClient in timedOutCalledBack) { CallBackClient.OnDisconnect((byte)Protocol.ConnectionReason.CannotConnect); } } m_PrevDownloadSpeed = m_DownloadSpeed; m_DownloadSpeed = aux_VelocidadDescarga; m_nDownloadingClients = aux_nDownloadingClients; m_nUsableClients = aux_nUsableClients; }