您的位置:首页 > 其它

智能预判 (四:命名管道连接逻辑和通信)

2016-05-13 12:30 267 查看
在测试中发现。单进程中,批量处理数据,会导致通信停滞。多线程并不能解决,所以采取 逻辑处理 和 通信 分开两个项目的方法。

连接这两个 服务器的 桥梁,选择了 命名管道 通信。

是的,他 比 socket 走tcp 的方式,更快 更安全,尤其是 大数据并发的时候,100倍socket也 不为过。

当然,他没有 socekt流行,原因 不能 外网 访问。不过在 服务器之间的 传输 数据 中,却能发挥 最大的优势。

我的智能预判 分 4步骤

后台 数据通信架构

前台 数据通信架构

前台 模拟架构

后台 模拟架构

每块架构 都是 需要 慢慢 完善的,没有 优先级 之分。

最后列出 命名管道 通信 加 线程 的 demo。

using LitJson;
using System;
using System.Collections.Generic;
using System.IO;
using System.IO.Pipes;
using System.Linq;
using System.Security.Principal;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace ConsoleApplication3
{
class Program
{
static void Main(string[] args)
{
Thread threads = new Thread(new ParameterizedThreadStart(ThreadMethod));//开启线程
threads.IsBackground = true;
threads.Start(ClientPipes());//命名管道在线程中 轮询
Console.ReadKey();
}

public static void ThreadMethod(object data)
{
StreamReader rdr = data as StreamReader;
string temp;
while ((temp = rdr.ReadLine()) != null)
{
JsonData json = JsonMapper.ToObject(temp);

Console.WriteLine(json["task"].ToString()
+ json["id"]
+ json["time"]
+ json["attack"]
+ json["beaten"]
+ json["soldier"]
+ json["isover"]
+ json["attackname"]
+ json["beatenname"]
);
}
}

//命名管道服务器开启
public static StreamReader ClientPipes()
{
NamedPipeClientStream pipeStream = new NamedPipeClientStream("mypipe");
pipeStream.Connect();
//在client读取server端写的数据
StreamReader rdr = new StreamReader(pipeStream);
return rdr;
}
}
}


using LitJson;
using System;
using System.Collections.Generic;
using System.IO;
using System.IO.Pipes;
using System.Linq;
using System.Security.Principal;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace ConsoleApplication4
{

class Program
{
static void Main(string[] args)
{
feiServer();
//    Server();
}
static void feiServer()
{
using (NamedPipeServerStream pipeStream = new NamedPipeServerStream("testpipe"))
{
pipeStream.WaitForConnection();

using (StreamWriter writer = new StreamWriter(pipeStream))
{
writer.AutoFlush = true;
string temp;

while ((temp = Console.ReadLine()) != "stop")
{
//   writer.WriteLine(temp);
JsonData message = new JsonData();
message["type"] = "颠三倒四";
message["Mode"] = "AppointChat";
writer.WriteLine(message.ToJson());
}
}
}
}

}
}


以上demo,我已经整合到通信项目中了,逻辑这方面就不发给大家了。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: