public Hub(HubSetting infav, IBaseUpdater gui) : base(infav.Address, infav.Port) { fav = infav; // Sets Favorites. me = new User(fav.DisplayName); me.UserInfo.Description = fav.UserDescription; me.Tag.Version = "FlowLib"; UpdateRegMode(); UpdateShare(); #region Event(s) UnknownProtocolId = new FmdcEventHandler(OnUnknownProtocolId); RegModeUpdated += new FmdcEventHandler(Hub_RegModeUpdated); ProtocolChange += new FmdcEventHandler(Hub_ProtocolChange); #endregion #region Event(s) if (gui != null) { gui.UpdateBase += new FmdcEventHandler(OnUpdateBase); baseUpdater = gui; } #endregion //FireUpdate(Actions.Name, new HubName(HubSetting.Address + ":" + HubSetting.Port.ToString())); #region Keep Alive // For Connection Keepalive TimerCallback timerDelegate = new TimerCallback(OnkeepAliveTimer_Elapsed); keepAliveTimer = new System.Threading.Timer(timerDelegate, socket, interval * 1000, interval * 1000); #endregion }
public void GetFileList(User usr, string func) { ContentInfo info = new ContentInfo(ContentInfo.FILELIST, BaseFilelist.UNKNOWN); string id = FlowLib.Utils.Convert.Base32.Encode(System.Text.Encoding.UTF8.GetBytes(usr.StoreID.ToLower().Trim())); info.Set(ContentInfo.STORAGEPATH, "filelists" + System.IO.Path.DirectorySeparatorChar + id + ".filelist"); info.Set("USR", usr.ID); info.Set("FUNC", func); downloadManager.AddDownload(new DownloadItem(info), new Source(hubConnection.HubSetting.Address + hubConnection.HubSetting.Port, usr.StoreID)); UpdateBase(this, new FmdcEventArgs(Actions.StartTransfer, usr)); }
protected void OnProtocolUpdate(object sender, FmdcEventArgs e) { switch (e.Action) { case Actions.StatusChange: switch (((HubStatus)e.Data).Code) { case HubStatus.Codes.Disconnected: if (socket != null && socket.Connected) { try { socket.Shutdown(SocketShutdown.Both); //socket.Disconnect(true); socket.Close(); } catch (System.Net.Sockets.SocketException) { } catch (System.ObjectDisposedException) { } } break; } break; case Actions.RegMode: RegMode = (int)e.Data; break; case Actions.UserOnline: UserInfo ui = (UserInfo)e.Data; User u = new User(ui.DisplayName); u.UserInfo = ui; lock (userlist) { userlist.Add(ui.ID, u); } break; case Actions.UserOffline: UserInfo userInfo = (UserInfo)e.Data; if (userInfo == null) return; lock (userlist) { userlist.Remove(userInfo.ID); } break; case Actions.Name: HubName hn = (HubName)e.Data; if(this.hubname == null) { this.hubname = hn; } break; } }
/// <summary> /// Returns user with the specified id from userlist. /// In ADC this means SID. /// In NMDC this means Nickname. /// </summary> /// <param name="id">Id for the user we want to get</param> /// <returns>User that have the specified id or null if not found</returns> public User GetUserById(string id) { User usr = new User(id); lock (userlist) { if (id != null && userlist.TryGetValue(id, out usr)) { return usr; } } return null; }
public override void Dispose() { if (!disposed) { if (baseUpdater != null) baseUpdater.UpdateBase -= OnUpdateBase; base.Dispose(); Hub.RegModeUpdated -= Hub_RegModeUpdated; UnknownProtocolId -= OnUnknownProtocolId; ProtocolChange -= Hub_ProtocolChange; if (updateInfoTimer != null) { updateInfoTimer.Dispose(); updateInfoTimer = null; } if (keepAliveTimer != null) { keepAliveTimer.Dispose(); keepAliveTimer = null; } if (worker != null) { worker.Abort(); worker = null; } lock (userlist) { if (userlist != null) { userlist.Clear(); userlist = null; } } this.me = null; if (share != null) { share.LastModifiedChanged -= share_LastModifiedChanged; this.share = null; } this.fav = null; } }
public void Hub_Update(object sender, FlowLib.Events.FmdcEventArgs e) { Hub hub = (Hub)sender; this.hub = hub; string str = string.Empty; switch (e.Action) { case Actions.TransferStarted: if (e.Data is ITransfer) { ITransfer trans = (ITransfer)e.Data; transferManager.StartTransfer(trans); } break; case Actions.TransferRequest: if (e.Data is TransferRequest) { TransferRequest req = (TransferRequest)e.Data; transferManager.AddTransferReq(req); } break; #region MainMessage case Actions.MainMessage: if (e.Data is MainMessage) { MainMessage mainchat = (MainMessage)e.Data; if (mainchat.From == null) str = mainchat.Content; else { User u = hub.GetUserById(mainchat.From); if (u == null) str = "<" + mainchat.From + "> " + mainchat.Content; else str = "<" + u.DisplayName + "> " + mainchat.Content; } // Test : Start user = hub.GetUserById("DC++0.699"); //User user = hub.GetUserById("DCDM++0.0495"); if (user != null && hub.Share != null) { ContentInfo ci = new ContentInfo(ContentInfo.FILELIST, FlowLib.Utils.FileLists.BaseFilelist.UNKNOWN); ci.Set(ContentInfo.STORAGEPATH, @"C:\Private\FMDC\PiP\FlowLibDemo\ConsoleDemo\bin\Debug\FileLists\" + user.ID + ".xml.bz2"); DownloadItem di = new DownloadItem(ci); downloadManager.AddDownload(di, new Source(null, user.ID)); downloadManager.DownloadCompleted += new FmdcEventHandler(DownloadManager_DownloadCompleted); this.Hub_Update(null, new FmdcEventArgs(Actions.TransferRequest, new TransferRequest(user.ID, hub, user.UserInfo))); //TransferManager.AddTransferReq(user.ID, hub, user.UserInfo); hub.Send(new FlowLib.Protocols.HubNmdc.ConnectToMe(user.ID, hub.Share.Port, hub)); } // Test : End } else str = e.Data.ToString(); Console.WriteLine(str); break; #endregion #region PrivateMessage case Actions.PrivateMessage: PrivateMessage to = (PrivateMessage)e.Data; Console.WriteLine("*** PM From: " + to.From + ", To: " + to.To + " " + to.Content); break; #endregion #region RegMode case Actions.RegMode: break; #endregion #region Status case Actions.StatusChange: HubStatus status = (HubStatus)e.Data; switch (status.Code) { case HubStatus.Codes.Disconnected: Console.WriteLine("*** Hub Disconnected"); if (status.Exception != null) { #if DEBUG if (status.Exception is System.Net.Sockets.SocketException) { System.Net.Sockets.SocketException se = (System.Net.Sockets.SocketException)status.Exception; StringBuilder sb = new StringBuilder(); sb.Append("\r\n"); sb.Append("\tErrorCode: " + se.ErrorCode + "\r\n"); sb.Append("\tMessage: " + se.Message + "\r\n"); sb.Append("\tStackTrace: " + se.StackTrace); Console.WriteLine(sb.ToString()); } #else Console.WriteLine(" : " + status.Exception.Message); //richTextBox1.AppendText(status.Exception.StackTrace); #endif } Console.WriteLine("\r\n"); break; case HubStatus.Codes.Connected: Console.WriteLine("*** Hub Connected"); break; case HubStatus.Codes.Connecting: Console.WriteLine("*** Hub Connecting"); break; } break; #endregion default: Console.WriteLine(e.Data); break; } }