コード例 #1
0
        private void AcceptCallback(IAsyncResult AR)
        {
            lock (lockAccept)
            {
                Socket socket;

                try
                {
                    socket = ServerSocket.EndAccept(AR);
                    var session = new SocketSession(socket, BufferSize);

                    ClientSockets.Add(session);

                    socket.BeginReceive(session.SessionStorage, 0, BufferSize, SocketFlags.None, ReceiveCallback, socket);
                    ServerSocket.BeginAccept(AcceptCallback, null);
                }
                catch (Exception ex) // I cannot seem to avoid this (on exit when properly closing sockets)
                {
                    LogController.WriteLog(new ServerLog("*** ERROR ***: \n" + ex.Message, ServerLogType.ERROR));
                }
            }
        }
コード例 #2
0
        public RequestPreProcess(IAsyncResult AR)
        {
            ClientSocket = (Socket)AR.AsyncState;
            int received;

            for (int i = 0; i < 3; i++)
            {
                session = GetSession(ClientSocket);
                if (session != null)
                {
                    break;
                }
                else
                {
                    Thread.Sleep(100);
                }
            }

            if (session == null)
            {
                Dispose();
                return;
            }

            try
            {
                received = ClientSocket.EndReceive(AR);
            }
            catch (Exception ex)
            {
                Dispose();
                return;
            }

            byte[] recBuf = new byte[received];
            Array.Copy(session.SessionStorage, recBuf, received);
            string uriRequest = Server.GlobalInstance.ServerEncoding.GetString(recBuf);
            string resultText = string.Empty;

            var srv = Server.GlobalInstance;

            if (srv.IsBasicServerControllerMode)
            {
                BasicControllerRequestProcess requestProcess = new BasicControllerRequestProcess(this);
                if (srv.IsSingleThreaded)
                {
                    requestProcess.DoInBackGround(uriRequest);
                }
                else
                {
                    WaitPendingRequestsCompletations();
                    requestProcess.Execute(uriRequest);
                }
                return;
            }

            if (string.IsNullOrEmpty(uriRequest))
            {
                Dispose();
                return;
            }

            RequestProccess process = new RequestProccess(uriRequest, ClientSocket);

            process.OnCompleted += Process_OnCompleted;

            if (Server.GlobalInstance.IsSingleThreaded)
            {
                process.DoInBackGround(0);
                Dispose();
            }
            else
            {
                WaitPendingRequestsCompletations();
                process.Execute(0);
            }
        }