Example #1
0
        public bool listen(int port, int backlog, AcceptCallback accept_cb)
        {
            is_server      = true;
            this.accept_cb = accept_cb;

            sock = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
            setNonBlocking();
            sock.Bind(new IPEndPoint(IPAddress.Any, port));
            server_port = ((IPEndPoint)sock.LocalEndPoint).Port;
            sock.Listen(backlog);
            if (!initializeSocket())
            {
                return(false);
            }
            if ((flags & (int)Flags.SYNCHRONOUS) == 0)
            {
                enableRead();
            }
            return(true);
        }
 internal static extern void QuestManager_Accept(
     HandleRef self,
  /* from(Quest_t) */IntPtr quest,
  /* from(QuestManager_AcceptCallback_t) */AcceptCallback callback,
  /* from(void *) */IntPtr callback_arg);
Example #3
0
        public bool listen(int port, int backlog, AcceptCallback accept_cb)
        {
            is_server = true;
            this.accept_cb = accept_cb;

            sock = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
            setNonBlocking();
            sock.Bind(new IPEndPoint(IPAddress.Any, port));
            server_port = ((IPEndPoint) sock.LocalEndPoint).Port;
            sock.Listen(backlog);
            if (!initializeSocket())
                return false;
            if ((flags & (int)Flags.SYNCHRONOUS) == 0)
                enableRead();
            return true;
        }
 internal static extern void QuestManager_Accept(HandleRef self, IntPtr quest, AcceptCallback callback, IntPtr callback_arg);
Example #5
0
 private void ProcessAccept(SocketAsyncEventArgs saea)
 {
     AcceptCallback?.Invoke(this, saea);
     StartAccept(saea);
 }
Example #6
0
 private void ProcessAccept(SocketAsyncEventArgs saea)
 {
     Console.WriteLine($"Listener.ProcessAccept, saea={saea?.PrintInfo()}");
     AcceptCallback?.Invoke(this, saea);
     StartAccept(saea);
 }
Example #7
0
        private int epoll_fd = -1;//epoolid


        /// <summary> /// 启动epoll服务
        /// </summary>
        /// <param name="ports">端口</param>
        /// <param name="ipaddrs">ip</param>
        /// <param name="recvSizes">每次接收大小</param>
        /// <param name="RecvCalls">收到消息后的回调用函数</param>
        /// <param name="SocketCloseCalls">连接断开后的回掉函数</param>
        /// <param name="listenSizes"> socket监听的队列长度</param>
        /// <param name="epollSizes"> Epoll监听的最多数量</param>
        /// <param name="AcceptCalls">有新的连接连入时候的回掉</param>
        /// <param name="readCounts">处理到某一个连接事件通知后,每次最多读取的次数</param>
        /// <param name="workQueueCounts">指定接收消息的队列数量,最大是8</param>
        /// <param name="istickpackage">受否选择粘包</param>
        /// <returns></returns>
        public bool Start(ushort ports, string ipaddrs, int recvSizes, RecvCallback RecvCalls, SocketCloseCallback SocketCloseCalls, int listenSizes, int epollSizes, AcceptCallback AcceptCalls, int readCounts, int workQueueCounts)
        {
            //开启队列接受粘包数据
            revMesQue1.UserWork += UserWorkRrevMesQue;
            if (revMesQue1.StartUserWork())
            {
                Console.WriteLine("UserWorkRrevMesQue1队列开启成功");
            }
            revMesQue2.UserWork += UserWorkRrevMesQue;
            if (revMesQue2.StartUserWork())
            {
                Console.WriteLine("UserWorkRrevMesQue2队列开启成功");
            }
            revMesQue3.UserWork += UserWorkRrevMesQue;
            if (revMesQue3.StartUserWork())
            {
                Console.WriteLine("UserWorkRrevMesQue3队列开启成功");
            }
            revMesQue4.UserWork += UserWorkRrevMesQue;
            if (revMesQue4.StartUserWork())
            {
                Console.WriteLine("UserWorkRrevMesQue4队列开启成功");
            }
            revMesQue5.UserWork += UserWorkRrevMesQue;
            if (revMesQue5.StartUserWork())
            {
                Console.WriteLine("UserWorkRrevMesQue5队列开启成功");
            }
            revMesQue6.UserWork += UserWorkRrevMesQue;
            if (revMesQue6.StartUserWork())
            {
                Console.WriteLine("UserWorkRrevMesQue6队列开启成功");
            }
            revMesQue7.UserWork += UserWorkRrevMesQue;
            if (revMesQue7.StartUserWork())
            {
                Console.WriteLine("UserWorkRrevMesQue7队列开启成功");
            }
            revMesQue8.UserWork += UserWorkRrevMesQue;
            if (revMesQue8.StartUserWork())
            {
                Console.WriteLine("UserWorkRrevMesQue8队列开启成功");
            }



            port            = ports;
            ipaddr          = ipaddrs;
            recvSize        = recvSizes;
            RecvCall        = RecvCalls;
            SocketCloseCall = SocketCloseCalls;
            listenSize      = listenSizes;
            epollSize       = epollSizes;
            AcceptCall      = AcceptCalls;

            readCount      = readCounts;
            workQueueCount = workQueueCounts;

            //回掉
            callback = CSCallbackFunction;
            var honssin_port = LSocket.htons(port);//端口

            Console.WriteLine("端口输出: {0}", honssin_port.ToString());

            var htonlip = LSocket.inet_addr(ipaddr); //htonl(0x00000000);

            Console.WriteLine("IP输出: {0}", htonlip.ToString());

            //创建socket
            sockfd = LSocket.socket(DomainType.AF_INET, SocketTypes.SOCK_STREAM, Protocol.IPPROTO_TCP);

            Console.WriteLine("创建socket输出: {0}", sockfd.ToString());

            //创建参数结构
            sockaddr_in addr = new sockaddr_in()
            {
                sin_family = DomainType.AF_INET,
                sin_port   = honssin_port,
                //sin_zero =new byte[] ,// System.Text.Encoding.UTF8.GetBytes(""),
                sin_addr = new in_addr {
                    s_addr = htonlip
                }
            };


            //绑定
            int bindretrun = LSocket.bind(sockfd, ref addr, 16);

            Console.WriteLine("绑定输出: {0}", bindretrun.ToString());

            if (bindretrun == 0)
            {
                Console.WriteLine("bind成功");
            }
            else
            {
                return(false);
            }

            //监听
            int lien = LSocket.listen(sockfd, listenSize);

            if (lien == 0)
            {
                Console.WriteLine("listen成功");
            }
            else
            {
                return(false);
            }

            epoll_fd = LSocket.epoll_create(epollSize);

            Console.WriteLine("epoll_create输出: {0}", epoll_fd.ToString());

            if (epoll_fd == -1)
            {
                return(false);
            }



            var epollctlret = LSocket.epoll_ctl_ADD(epoll_fd, EPOLL_CTL.EPOLL_CTL_ADD, sockfd, (int)EpollEvents.EPOLLIN);


            Console.WriteLine("epoll_ctl注册返回值: {0}", epollctlret.ToString());
            if (epollctlret == -1)
            {
                return(false);
            }

            //开启Epoll
            Thread th = new Thread(() =>
            {
                LSocket.PollWait(epoll_fd, epollSize, callback);//开启等待模式
            });

            th.Start();



            //开启线程读取消息-------1个线程池
            #region 开启线程读取消息
            Thread thr = new Thread(() =>
            {
                while (true)
                {
                    var tepm = workQ1.DequeueItem();
                    if (tepm != null && tepm != 0)
                    {
                        try
                        {
                            Redmes(tepm);
                        }
                        catch (Exception expWork)
                        {
                            Console.WriteLine("读取都列异常: {0}", expWork.Message.ToString());
                        }
                    }
                    else
                    {
                        Thread.Sleep(20);
                    }
                }
            });
            thr.Start();


            Thread thr2 = new Thread(() =>
            {
                while (true)
                {
                    var tepm = workQ2.DequeueItem();
                    if (tepm != null && tepm != 0)
                    {
                        try
                        {
                            Redmes(tepm);
                        }
                        catch (Exception expWork)
                        {
                            Console.WriteLine("读取都列异常: {0}", expWork.Message.ToString());
                        }
                    }
                    else
                    {
                        Thread.Sleep(20);
                    }
                }
            });
            thr2.Start();

            Thread thr3 = new Thread(() =>
            {
                while (true)
                {
                    var tepm = workQ3.DequeueItem();
                    if (tepm != null && tepm != 0)
                    {
                        try
                        {
                            Redmes(tepm);
                        }
                        catch (Exception expWork)
                        {
                            Console.WriteLine("读取都列异常: {0}", expWork.Message.ToString());
                        }
                    }
                    else
                    {
                        Thread.Sleep(20);
                    }
                }
            });
            thr3.Start();


            Thread thr4 = new Thread(() =>
            {
                while (true)
                {
                    var tepm = workQ4.DequeueItem();
                    if (tepm != null && tepm != 0)
                    {
                        try
                        {
                            Redmes(tepm);
                        }
                        catch (Exception expWork)
                        {
                            Console.WriteLine("读取都列异常: {0}", expWork.Message.ToString());
                        }
                    }
                    else
                    {
                        Thread.Sleep(20);
                    }
                }
            });
            thr4.Start();
            #endregion

            #region 开启线程读取消息2
            Thread thr5 = new Thread(() =>
            {
                while (true)
                {
                    var tepm = workQ5.DequeueItem();
                    if (tepm != null && tepm != 0)
                    {
                        try
                        {
                            Redmes(tepm);
                        }
                        catch (Exception expWork)
                        {
                            Console.WriteLine("读取都列异常: {0}", expWork.Message.ToString());
                        }
                    }
                    else
                    {
                        Thread.Sleep(20);
                    }
                }
            });
            thr5.Start();


            Thread thr6 = new Thread(() =>
            {
                while (true)
                {
                    var tepm = workQ6.DequeueItem();
                    if (tepm != null && tepm != 0)
                    {
                        try
                        {
                            Redmes(tepm);
                        }
                        catch (Exception expWork)
                        {
                            Console.WriteLine("读取都列异常: {0}", expWork.Message.ToString());
                        }
                    }
                    else
                    {
                        Thread.Sleep(20);
                    }
                }
            });
            thr6.Start();

            Thread thr7 = new Thread(() =>
            {
                while (true)
                {
                    var tepm = workQ7.DequeueItem();
                    if (tepm != null && tepm != 0)
                    {
                        try
                        {
                            Redmes(tepm);
                        }
                        catch (Exception expWork)
                        {
                            Console.WriteLine("读取都列异常: {0}", expWork.Message.ToString());
                        }
                    }
                    else
                    {
                        Thread.Sleep(20);
                    }
                }
            });
            thr7.Start();


            Thread thr8 = new Thread(() =>
            {
                while (true)
                {
                    var tepm = workQ8.DequeueItem();
                    if (tepm != null && tepm != 0)
                    {
                        try
                        {
                            Redmes(tepm);
                        }
                        catch (Exception expWork)
                        {
                            Console.WriteLine("读取都列异常: {0}", expWork.Message.ToString());
                        }
                    }
                    else
                    {
                        Thread.Sleep(20);
                    }
                }
            });
            thr8.Start();
            #endregion



            return(true);
        }
Example #8
0
 internal static extern void QuestManager_Accept(
     HandleRef self,
     /* from(Quest_t) */ IntPtr quest,
     /* from(QuestManager_AcceptCallback_t) */ AcceptCallback callback,
     /* from(void *) */ IntPtr callback_arg);
Example #9
0
 public void SetAcceptCallback(AcceptCallback cb)
 {
     mAcceptCb = cb;
 }