简体   繁体   English

单插槽多客户端架构

[英]Single socket multiple clients architecture

I have to maintain a single persistent socket connection to a payment gateway and use it to send financial messages and receive confirmation for the same. 我必须保持与支付网关的单个持久套接字连接,并使用它来发送财务消息并接收相同的确认。 My application will be then used by various clients and so I need to devise a way to handle them concurrently and handle issues such as timeouts and retries etc. 我的应用程序将被各种客户端使用,因此我需要设计一种方法来同时处理它们并处理诸如超时和重试等问题。

Right now, my main issue is with accessing the socket... should I just lock the send and recv per message request and response or set up a queuing system and match them? 现在,我的主要问题是访问套接字...我应该只锁定每个消息的发送和recv请求和响应,还是设置一个排队系统并匹配它们? I'll also be sending periodic echo messages on another thread. 我还将在另一个线程上发送定期回显消息。

Oh, and I am planning to do it in C#. 哦,我打算用C#做。 I would appreciate some general advice on this issue. 对于这个问题,我将不胜感激。

You need a persistent socket to the payment gateway, ok. 你需要一个持久的套接字到支付网关,好的。 By that I assume you mean it must stay connected. 我认为你的意思是它必须保持联系。

Then you need to create a listener socket to listen for connections from your clients. 然后,您需要创建一个侦听器套接字来侦听来自客户端的连接。 Then act as a translator between the two. 然后充当两者之间的翻译。

I'm not sure I understand what you mean by "lock the socket". 我不确定我是通过“锁定插座”理解你的意思。 Lock it how? 锁定它怎么样?

Unless the protocol for the payment gateway is intended for multiple concurrent operations, you probably don't want to send more than one request at a time. 除非支付网关的协议用于多个并发操作,否则您可能不希望一次发送多个请求。 This would mean a queue of some sort, or a thread for each request using some kind of mutex or semaphore to control access. 这将意味着某种类型的队列,或者使用某种互斥或信号量来控制访问的每个请求的线程。 A queue is more efficient in most cases. 在大多数情况下,队列更有效。

I did this exact thing (if I understand you correctly). 我做了这件事(如果我理解正确的话)。 I have a server that connects to some target devices over sockets and then clients hook up to the server to talk to different target systems. 我有一台通过套接字连接到某些目标设备的服务器,然后客户端连接到服务器以与不同的目标系统通信。 Is this (kind of) what you want? 这是(有点)你想要的吗? I have multiple clients talking to the same socket through the server. 我有多个客户端通过服务器与同一个套接字进行通信。

In my server I keep a list of connected clients and a list of connected targets. 在我的服务器中,我保留了已连接客户端列表和已连接目标列表。 When a client requests a target I add it to a matrix that is essentially a dictionary of connections because several clients can talk to one target at the same time. 当客户端请求目标时,我将其添加到基本上是连接字典的矩阵中,因为多个客户端可以同时与一个目标进行通信。 The server then pumps messages between clients and targets entirely asynchrounously and I use transaction IDs to keep track of the messages. 然后,服务器完全异步地在客户端和目标之间泵送消息,并使用事务ID来跟踪消息。 So that when a target answers a request the server knows to which client to send the answer. 因此,当目标回答请求时,服务器知道向哪个客户端发送答案。

I'm not sure this is what you want but maybe what I did will help you a little on your way anyway. 我不确定这是你想要的,但也许我所做的将会帮助你一点点。 If I'm on the right track I can elaborate further. 如果我走在正确的轨道上,我可以进一步阐述。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM