示例#1
0
        public bool Init()
        {
            try
            {
                //初始化Sokect
                listenSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
                //初始化IP地址
                ipAddress  = IPAddress.Parse(ipString);
                ipEndPoint = new IPEndPoint(ipAddress, ipPort);

                //初始化缓存池
                connecters = new APMConnectState[maxConnectNum];

                for (int i = 0; i < connecters.Length; i++)
                {
                    connecters[i] = new APMConnectState(log, timeOut, i, readSize);
                    connecters[i].ReadComplete    += APMServer_ReadComplete;
                    Connecters[i].SendComplete    += APMServer_SendComplete;
                    connecters[i].DisconnectEvent += APMServer_DisconnectEvent;
                    //connecters[i].Init();
                }
                return(true);
            }
            catch (Exception e)
            {
                string errorInfor = string.Format("Server init error: {0}", e.Message);
                log.ErrorLog(errorInfor);
                return(false);
            }
        }
示例#2
0
        private void acceptCallBack(IAsyncResult result)
        {
            var server = result.AsyncState as Socket;
            int index  = newIndex();

            if (index == -1)
            {
                string errorInfor = string.Format("Server connect error: {0}", "Over the max connect number.");
                log.ErrorLog(errorInfor);
                server.EndAccept(result);
                server.BeginAccept(acceptCallBack, listenSocket);
                return;
            }
            APMConnectState connecter = connecters[index];

            connecter.Init();
            connecter.IsUsed        = true;
            connecter.CurrentSocket = server.EndAccept(result);
            log.NormalLog(string.Format("connect information,ID:{0} , IPAdderss:{1}", connecter.ID, connecter.CurrentSocket.RemoteEndPoint));
            //first Receive data
            connecter.ReceiveAsync(readSize);
            //继续监听新的连接
            server.BeginAccept(acceptCallBack, listenSocket);
        }
示例#3
0
 private void APMServer_ReadComplete(APMConnectState arg1, int arg2)
 {
     ReadComplete?.Invoke(arg1);
 }
示例#4
0
 private void APMServer_DisconnectEvent(APMConnectState obj)
 {
     DisconnectEvent?.Invoke(obj);
 }