创建对象的新实例,还是修改现有对象?

发布于 2024-08-10 10:28:25 字数 963 浏览 7 评论 0原文

我正在尝试用 C# 创建一个 BitTorrent 库作为一个业余项目,为了好玩。然而,我遇到了一个设计问题,如果我现在不解决它,以后可能会产生问题。

我目前有一个 PeerGreeter 类,它将 Socket 置于监听状态,以便任何尝试连接到我的对等点,为我提供 torrent 中的文件。当对等方连接时,迎宾者会交换握手,确保一切有效,然后触发 PeerConnected 事件,并将关联的 Socket 和握手信息作为处理程序参数。

我的 Torrent 类代表单个 torrent 及其所有职责,它有两个列表,其中包含群中的所有对等点(封装在 Peer 对象中),已连接和已断开连接。当欢迎程序触发 PeerConnected 事件时,Torrent 实例会在断开连接的列表中找到相应的 Peer。如果找到,则将其移动到连接列表中,并将其实例中 Socket 类型的 Connection 属性设置为由迎宾员。该属性是一个自动属性,其访问修饰符为: { get;内部设置; 。

我遇到的问题是,据我所知,这不是线程安全的 如果一个线程正在使用 Peer 的连接,然后另一个线程以某种方式修改该连接对象或处置它,则可能会产生问题。我考虑过将 Connection 属性的 setter 访问修饰符设置为 private,并在构造函数中设置它,但问题是我需要创建一个新对象替换断开连接列表中的占位符 Peer 以将其添加到连接列表中。

我的问题是,我应该坚持将 setter 设置为 internal,还是将其设置为 private 并用全新的实例替换占位符也是一个好方法?

I'm attempting to create a BitTorrent library in C# as a side project, for fun. However, I've run into a design issue that could create problems later on if I don't address it now.

I currently have a PeerGreeter class, which puts a Socket in a listening state for any peers that try to connect to me, to serve me files in the torrent. When a peer connects, the greeter exchanges handshakes, makes sure everything is valid, and then fires a PeerConnected event with the associated Socket and handshake information as handler arguments.

My Torrent class, which is a representation of a single torrent and all its duties, has two lists of all peers in the swarm (encapsulated in a Peer object), connected and disconnected. When the greeter fires the PeerConnected event, the Torrent instance finds the corresponding Peer in the disconnected list. If it finds one, it moves it to the connected list, and sets a Connection property of type Socket in its instance to the Socket created by the greeter. The property is an auto-property with the access modifiers as: { get; internal set; }

The problem I'm having is that this, as far as I know, isn't thread-safe. If one thread is working with a connection of a Peer, and then another thread modifies that connection object somehow, or disposes it, it could create issues. I've considered setting the Connection property's setter's access modifier to private, and having it set in the constructor, but the problem with that is that I need to create a new object to replace the placeholder Peer in the disconnected list to add it to the connected list.

My question is should I stick with having the setter as internal, or is making it private and replacing the placeholder with an entirely new instance a good approach as well?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

小伙你站住 2024-08-17 10:28:25

出于线程安全的目的,使类型尽可能不可变将为您省去很多很多麻烦。将访问器设置为私有(或者更好的是,将字段标记为只读)并在需要进行更改时生成新实例。

For thread-safety purposes, making the type as immutable as possible will save you many, many headaches. Make the accessor private (or better yet, mark the field as readonly) and generate new instances when changes need to be made.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文