从Netty Handler内部访问Netty服务器的实例(Accessing an instance of Netty server from inside a Netty Handler)

我正在为多人游戏编写Netty服务器,我不确定是否需要以某种方式同步服务器中但由ChannelHandler访问的变量。

在服务器级别,我使用ArrayList来存储服务器将要服务的不同匹配。

每个匹配将引用2个通道(我为每个匹配存储ChannelHandlerContetx)。

当我创建从SimpleChannelInboundHandler扩展的ChannelHandler时,我将服务器的实例传递给构造函数,并将服务器作为实例变量存储在处理程序中。

当触发channelActive时,ChannerlHandler将在“等待”状态下搜索ArrayList(它位于服务器实例中)中的匹配项。 如果找到一个绑定它并改变匹配状态。 如果不是,则创建新的匹配,然后通道绑定到它,使其处于等待状态。

我知道频道是线程安全的。 但是这里不同的通道正在访问同一服务器的ArrayList实例。

在这种情况下,我应该注意同步对ArrayList的访问吗?

注意如果它添加到我的问题:因为我将在后端有一个数据库,我在创建处理程序时在.addLast()方法中传递一个DefaultEventExecutor。


I am writting a Netty Server for a multiplayer game and I am not sure if I need to synchronize in some way a variable that lives in the server but is accessed by the ChannelHandler.

At the server level I am using an ArrayList to store the different matches the server will be serving.

Each match will be referencing 2 channels (I store for the match the ChannelHandlerContetx for each one).

When I create the ChannelHandler that extends from SimpleChannelInboundHandler I pass an instance of the server to the constructor and I store the server as an instance variable in the handler.

When channelActive is fired, the ChannerlHandler will search for a match in the ArrayList (that lives in the server instance) in "Waiting" state. If it finds one it bounds to it and changes the match status. If not a new Match is created and then the channel bounds to it leaving it in Waiting status.

I know that channels are Thread Safe. But here the different channels are accessing the same server's ArrayList instance.

In this case should I take care of synchronizing the access to the ArrayList?

Note in case it adds to my question: As I am going to have a database on the backend, I am passing a DefaultEventExecutor in the .addLast() method when the handler is being created.


原文:https://stackoverflow.com/questions/23840097
2022-03-21 06:03

满意答案

我在你的问题中看不到任何问题,但总的来说是表达方式

hp == w

如果向量具有相同的长度,则返回该长度的0s和1s的向量,表示两个向量匹配( 1 )或不匹配( 0 )的情况。 请注意,浮点数相等的比较充满了“问题”,您可能会更好地评估:

abs(hp-w) < 10^-6

用你喜欢的公差替换10^-6

根据你对w的定义,你应该能够写出来

hp == 1/(x.^2+1)

注意使用元素平方运算符.^ ,它返回一个与x长度相同的向量,每个元素都是x中相应元素的平方。 当然,表达

hp - 1./(x.^2+1)

将返回差异的向量,这可能是你想要的。


I see no w in your question, but in general the expression

hp == w

will, if the vectors have the same length, return a vector of 0s and 1s, of that length, representing the cases where the two vectors match (1) or don't match (0). In passing, note that comparison for equality of floating-point numbers is fraught with 'issues' and you might be better evaluating:

abs(hp-w) < 10^-6

replacing 10^-6 by your preferred tolerance.

Given your definition for w you should be able to write

hp == 1/(x.^2+1)

Note the use of the elementwise squaring operator .^, which returns a vector of the same length as x with each element the square of the corresponding element in x. Of course, the expression

hp - 1./(x.^2+1)

will return a vector of the differences, which might be what you want.

相关问答

更多

如何在MATLAB中使用哈希表(字典)?(How to use Hash Tables (dictionaries) in MATLAB?)

在MATLAB的最新版本中,有containers.Map数据结构。 查看更多的MATLAB Map容器 。 这在使用STRUCTs时删除了一些限制。 例如 c = containers.Map c('foo') = 1 c(' not a var name ') = 2 keys(c) values(c) In recent versions of MATLAB, there's the containers.Map data structure. See MATLAB Map contain...

更快的Matlab表创建(Faster Matlab Table Creation)

根据MathWorks员工的回复 ,如果不改变核心Matlab文件,就无法做到这一点。 Based on this reply from a MathWorks employee, you can't do it without altering the core Matlab files.

如何在matlab中使用以下值构建表?(How I can build a table in matlab with the following values?)

我在你的问题中看不到任何问题,但总的来说是表达方式 hp == w 如果向量具有相同的长度,则返回该长度的0s和1s的向量,表示两个向量匹配( 1 )或不匹配( 0 )的情况。 请注意,浮点数相等的比较充满了“问题”,您可能会更好地评估: abs(hp-w) < 10^-6 用你喜欢的公差替换10^-6 。 根据你对w的定义,你应该能够写出来 hp == 1/(x.^2+1) 注意使用元素平方运算符.^ ,它返回一个与x长度相同的向量,每个元素都是x中相应元素的平方。 当然,表达 hp - ...

重塑Matlab表(Reshape Matlab table)

这可以使用自定义函数使用accumarray完成。 第一步是将T的name列转换为数字向量; 然后可以应用accumarray 。 这种方法要求T按照第1列进行排序,因为只有在这种情况下accumarray确保accumarray保持顺序(如文档中所示)。 因此,如果T可能没有排序(尽管在您的示例中),请首先使用sortrows对其进行sortrows 。 T = sortrows(T, 1); %// you can remove this line if T is guaranteed to ...

在Matlab表中将字符串值更改为数字(Change string values to number in Matlab table)

我的Matlab给你的代码提供了不同的错误信息。 >> rows_attack = strcmp(T(:,2),'attack') rows_attack = 0 >> T(rows_attack,2) = 1 Right hand side of an assignment into a table must be another table or a cell array. >> T(rows_attack,2) ans = empty 0-by-1 tabl...

MATLAB:增长哈希表(MATLAB: Growing hash table)

我假设你的意思是你使用内建的containers.Map对象作为散列表。 尽管没有直接的方法来预先分配这样一个对象,但我建议你使用一个简单的两列单元数组或一个java.util.Hashtable对象,它们通常比containers.Map快得多containers.Map 。 参考: https : //undocumentedmatlab.com/blog/using-java-collections-in-matlab I assume you mean that you're using ...

如何在matlab中生成具有下一个值的矩阵?(How to generate a matrix in matlab with the next values?)

spdiags是要走的路, A = sparse(Nx); A = spdiags(b*ones(Nx-1,1), -1, A); A = spdiags(a*ones(Nx,1), 0, A); A = spdiags(b*ones(Nx-1,1), 1, A); A(1, 1:2) = [1,1]; A(N, N-1:N) = [1,1]; spdiags is the way to go, A = sparse(Nx); A = spdiags(b*ones(Nx-1,1), -1, A)...

如何在MATLAB中使用查找表(How to use a look up table in MATLAB)

我同意这个问题不是很清楚,显示一些代码会有所帮助。 无论如何我会尝试。 为了使LUT有意义,必须将由tx获得的值集限制为例如整数。 假设指数可以是-1000到1000之间的任何整数,你可以像这样创建一个LUT: LUT = exp(-1000:1000); 然后创建索引(假设t是一维数组,x是二维数组) indexArray = bsxfun(@minus,reshape(t,[1,1,3]), x) + 1001; %# -1000 turns into 1 最后,您创建结果 output ...

Matlab在列中查找x min值的索引(Matlab find indexes of x min values in column)

如果你不介意全部完成,那么[~, ans] = sort(a)就可以了。 然后,您可以获取实际需要的ans的前几个元素。 sort的构建非常快,尽管找到了所有的分钟而不仅仅是你需要的分钟,但这应该足够高效。 If you don't mind doing them all, then [~, ans] = sort(a) will do the trick. You can then take the first few elements of ans that you actually need...

Matlab的。(Matlab. Replace missed values with an avg)

以下解决方案解决了以下问题: 将最后一个表列转换为单元格数组(保持不同长度的字符串需要单元格数组)。 从单元格数组中删除所有NaN元素 (NaN元素会破坏下一节)。 查找单元格数组中最重复的字符串 。 在stringColumn中查找NaN元素的所有indeces(我使用cellfun基于上一节的cellfun )。 找到的indeces中的Rplace元素,最常见的字符串。 由于您是Matlab的新手,我的解决方案对您来说看起来非常复杂(对我来说这看起来很复杂)。 可能有一个更简单的解决方案,我...

相关文章

更多

[Netty 1] 初识Netty

1. 简介 最早接触netty是在阅读Zookeeper源码的时候,后来看到Storm的消息传输层也由 ...

Netty源码分析

Netty提供异步的、事件驱动的网络应用程序框架和工具,用以快速开发高性能、高可靠性的网络服务器和客户 ...

Netty入门实例-时间服务器

Netty中服务器和客户端之间最大的和唯一的区别是使用了不同的Bootstrap和Channel实现

Netty入门实例-编写服务器端程序

channelRead()处理程序方法实现如下

Netty基于流的传输处理

​在TCP/IP的基于流的传输中,接收的数据被存储到套接字接收缓冲器中。不幸的是,基于流的传输的缓冲器 ...

Netty开发环境配置

最新版本的Netty 4.x和JDK 1.6及更高版本

Netty环境配置

netty是一个java事件驱动的网络通信框架,也就是一个jar包,只要在项目里引用即可。

storm学习之Netty代替ZMQ

整理自 http://www.csdn.net/article/2014-08-04/2821018/ ...

在Twitter,Netty 4 GC开销降为五分之一

原文:http://www.infoq.com/cn/news/2013/11/netty4-twit ...

Netty入门实例-使用POJO代替ByteBuf

使用TIME协议的客户端和服务器示例,让它们使用POJO来代替原来的ByteBuf。

最新问答

更多

获取MVC 4使用的DisplayMode后缀(Get the DisplayMode Suffix being used by MVC 4)

我用Google搜索了一个解决方案。 “EnumDisplayModeProvider”是我自己设置网站的各种模式的枚举。 public EnumDisplayModeProvider GetDisplayModeId() { foreach (var mode in DisplayModeProvider.Instance.Modes) if (mode.CanHandleContext(HttpContext)) {

如何通过引用返回对象?(How is returning an object by reference possible?)

这相对简单:在类的构造函数中,您可以分配内存,例如使用new 。 如果你制作一个对象的副本,你不是每次都分配新的内存,而是只复制指向原始内存块的指针,同时递增一个也存储在内存中的引用计数器,使得每个副本都是对象可以访问它。 如果引用计数降至零,则销毁对象将减少引用计数并仅释放分配的内存。 您只需要一个自定义复制构造函数和赋值运算符。 这基本上是共享指针的工作方式。 This is relatively easy: In the class' constructor, you allocate m

矩阵如何存储在内存中?(How are matrices stored in memory?)

正如它在“熵编码”中所说的那样,使用Z字形图案,与RLE一起使用,在许多情况下,RLE已经减小了尺寸。 但是,据我所知,DCT本身并没有给出稀疏矩阵。 但它通常会增强矩阵的熵。 这是compressen变得有损的点:输入矩阵用DCT传输,然后量化量化然后使用霍夫曼编码。 As it says in "Entropy coding" a zig-zag pattern is used, together with RLE which will already reduce size for man

每个请求的Java新会话?(Java New Session For Each Request?)

你是如何进行重定向的? 您是否事先调用了HttpServletResponse.encodeRedirectURL()? 在这里阅读javadoc 您可以使用它像response.sendRedirect(response.encodeRedirectURL(path)); The issue was with the path in the JSESSIONID cookie. I still can't figure out why it was being set to the tomca

css:浮动div中重叠的标题h1(css: overlapping headlines h1 in floated divs)

我认为word-break ,如果你想在一个单词中打破行,你可以指定它,这样做可以解决问题: .column { word-break:break-all; } jsFiddle演示。 您可以在此处阅读有关word-break属性的更多信息。 I think word-break, with which you can specify if you want to break line within a word, will do the trick: .column { word-break

无论图像如何,Caffe预测同一类(Caffe predicts same class regardless of image)

我认为您忘记在分类时间内缩放输入图像,如train_test.prototxt文件的第11行所示。 您可能应该在C ++代码中的某个位置乘以该因子,或者使用Caffe图层来缩放输入(请查看ELTWISE或POWER图层)。 编辑: 在评论中进行了一次对话之后,结果发现在classification.cpp文件中错误地删除了图像均值,而在原始训练/测试管道中没有减去图像均值。 I think you have forgotten to scale the input image during cl

xcode语法颜色编码解释?(xcode syntax color coding explained?)

转到: Xcode => Preferences => Fonts & Colors 您将看到每个语法高亮颜色旁边都有一个简短的解释。 Go to: Xcode => Preferences => Fonts & Colors You'll see that each syntax highlighting colour has a brief explanation next to it.

在Access 2010 Runtime中使用Office 2000校对工具(Use Office 2000 proofing tools in Access 2010 Runtime)

你考虑过第三方拼写检查吗? 您可以将在C#中开发的自定义WinForms控件插入访问数据库吗? VB6控件怎么样? 如果你能找到一个使用第三方库进行拼写检查的控件,那可能会有效。 Have you considered a third party spell checker? Can you insert a custom WinForms controls developed in C# into an access database? What about a VB6 control? If

从单独的Web主机将图像传输到服务器上(Getting images onto server from separate web host)

我有同样的问题,因为我在远程服务器上有两个图像,我需要在每天的预定义时间复制到我的本地服务器,这是我能够提出的代码... try { if(@copy('url/to/source/image.ext', 'local/absolute/path/on/server/' . date("d-m-Y") . ".gif")) { } else { $errors = error_get_last(); throw new Exception($err

从旧版本复制文件并保留它们(旧/新版本)(Copy a file from old revision and keep both of them (old / new revision))

我不确定我完全明白你在说什么。 你能编辑你的帖子并包含你正在做的Subversion命令/操作的特定顺序吗? 最好使用命令行svn客户端,以便容易为其他人重现问题。 如果您只是想获取文件的旧副本(即使该文件不再存在),您可以使用如下命令: svn copy ${repo}/trunk/moduleA/file1@${rev} ${repo}/trunk/moduleB/file1 其中${repo}是您的存储库的URL, ${rev}是您想要的文件的版本。 这将恢复该文件的旧版本,包括最高版本