码迷,mamicode.com
首页 > Web开发 > 详细

.NET Core初览

时间:2019-12-14 13:45:49      阅读:130      评论:0      收藏:0      [点我收藏+]

标签:async   ++   back   code   work   content   游戏   ons   rap   

.NET Core

 

初览的应用场景为游戏服务器开发。所以测试在侧重点上更强于IO和密集型计算

 

网络IO:

NetCore:60%CPU QPS 56W

C++:31%CPU QPS 58W

C++:68%CPU QPS 124W

 

C#测试代码

using System;
using System.IO;
using System.Net;
using System.Net.Sockets;
using System.Threading;
using System.Collections;
using System.Collections.Generic;


namespace ConsoleApp2
{
    class Session
    {
        public TcpClient client = null;
        public void Go()
        {
            client.GetStream().BeginRead(bytes, pos, 32 - pos, AsyncReadCallback, this);
        }
        void AsyncReadCallback(IAsyncResult ar)
        {
            if (ar.IsCompleted)
            {
                var sess = ar.AsyncState as Session;

                sess.client.GetStream().BeginWrite(sess.bytes, sess.pos, 32, AsyncWriteCallback, sess);
            }
        }
        void AsyncWriteCallback(IAsyncResult ar)
        {
            if (ar.IsCompleted)
            {
                var sess = ar.AsyncState as Session;
                sess.client.GetStream().BeginRead(sess.bytes, sess.pos, 32, AsyncReadCallback, sess);
            }
        }
        int pos = 0;
        byte[] bytes = new byte[32];
    }
    class Program
    {
        static void worker(object obj)
        {
         while(true)
            {
                Thread.Sleep(1);

                lock(obj)
                {
                    var que = obj as Queue<Session>;
                    if(que.Count>0)
                    {
                        que.Dequeue().Go();
                    }
                }
            }
        }

        static void Main(string[] args)
        {
            var ss = new TcpListener(12125);
            ss.Start();
            var bytes = new byte[32];

            var ths = new Thread[50];
            var que = new Queue<Session>[50];

            int i = 0;
            foreach(var p in ths)
            {
                que[i] = new Queue<Session>();

                ths[i] = new Thread(   (worker));

                ths[i].Start(que[i]);
                ++i;
            }

            int balance = 0;
            while (true)
            {
                balance = ++balance %  que.Length;
                var sess = new Session();

                var task = ss.AcceptTcpClientAsync();
                while (task.IsCompleted == false)
                {
                    var cli = task.Result;
                    sess.client = cli;
                    lock (que[balance])
                    {
                        que[balance].Enqueue(sess);
                    }    
                }


            }

            ss.Stop();

            Console.WriteLine("Hello World!");
        }
    }
}

 

 

TODO

 来源:http://www.1994july.club/seo/

 

.NET Core初览

标签:async   ++   back   code   work   content   游戏   ons   rap   

原文地址:https://www.cnblogs.com/1994jinnan/p/12038712.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!