示例#1
0
        /// <summary>
        /// Creates a channel instance
        /// </summary>
        /// <param name="channelId">The ID of the channel</param>
        /// <param name="sendEventArgs">The args used to send args</param>
        public DefaultChannel(int channelId, AsyncServerSocket serverSocket, SocketAsyncEventArgs sendEventArgs)
        {
            this.Id           = channelId;
            this.ServerSocket = serverSocket;
            this.SendArgs     = sendEventArgs;

            this.WriterQueue = new ConcurrentQueue <IBuffer>();
        }
        private void dealCallExe(Socket socket, CallExeCo co)
        {
            CallExeServer exe = new CallExeServer();

            if (co.Async)
            {
                CallExeCro exeCro = new CallExeCro();

                string uuid = logReqMsg(socket, "exePath:" + co.ExePath + ", Args:" + string.Join(",", co.Args));
                // CallExeOutputHandler outputHandler = new CallExeOutputHandler();
                // async
                exe.asyncCallExe(co.ExePath, co.Args,
                                 delegate(string output)
                {
                    Console.WriteLine("output:" + output);
                    // 每次只输出一行,因此需要动态添加"\n"
                    exeCro.Output += output + "\n";
                },
                                 delegate(int exitCode)
                {
                    Console.WriteLine("exitCode:" + exitCode);
                    exeCro.ExitCode = exitCode;

                    if (exeCro.Output != null && exeCro.Output.Length >= 1)
                    {
                        // 去除最后一个"\n"
                        exeCro.Output = exeCro.Output.Substring(0, exeCro.Output.Length - 1);
                    }

                    logResMsg(socket, "exitCode:" + exeCro.ExitCode + ", output:" + exeCro.Output, uuid);
                    // 异步调用结束
                    AsyncServerSocket.getInstance().Send(socket, exeCro);
                },
                                 delegate(string error)
                {
                    Console.WriteLine("error:" + error);
                    exeCro.Error += error;
                });
                //string[] ret = exe.asyncCallExe(co.ExePath, co.Args,null);
            }
            else
            {
                // sync
                string[] ret = exe.syncCallExe(co.ExePath, co.Args);
                Console.WriteLine("exitCode:{0}, ouput:{1}, error:{2}", ret[0], ret[1], ret[2]);


                CallExeCro exeCro = new CallExeCro();
                exeCro.ExitCode = int.Parse(ret[0]);
                exeCro.Output   = ret[1];
                exeCro.Error    = ret[2];
                AsyncServerSocket.getInstance().Send(socket, exeCro);
            }
        }
        private void startServerSocket()
        {
            // AppServer.initApp();
            ServerReceiveHandler receiveHandler = new ServerReceiveHandler(Receive);
            AcceptSocketHandler  acceptHandler  = new AcceptSocketHandler(accept);

            // 初始化Server Socket
            AsyncServerSocket.initSocketServer("127.0.0.1", 0,
                                               receiveHandler,
                                               delegate(int port) {
                this.txtPort.Text = port + "";
            },
                                               acceptHandler);
        }
        private void dealDownloadFile(Socket socket, DownloadFileCo co)
        {
            DownloadFileServer server = new DownloadFileServer();

            string uuid = logReqMsg(socket, "URL:" + co.Url + ", savePath:" + co.SavePath);

            server.downloadFile(co.Url, co.SavePath,
                                delegate(DownloadFileCro cro)
            {
                logResMsg(socket, "isSuccess:" + cro.IsSuccess + ", savePath:" + cro.LocalPath + ", Message:" + cro.Message, uuid);
                // 异步调用结束
                AsyncServerSocket.getInstance().Send(socket, cro);
                server = null;
            });
        }
        public void Start(string hostName, int port, Action <ServiceInitialisedContext> onServiceInitialised)
        {
            this._hostName = hostName;
            this._port     = port;

            if (!IPAddress.TryParse(this._hostName, out IPAddress ipAddress))
            {
                throw new InvalidOperationException("Invalid host name detected, a valid IP address is required");
            }

            this._serverSocket = new AsyncServerSocket(new IPEndPoint(ipAddress, port), this._channelInitialiser);

            this._serverSocket.Listen();

            onServiceInitialised.Invoke(new ServiceInitialisedContext(this));
        }
示例#6
0
 /// <summary>
 /// Construtor
 /// </summary>
 /// <param name="server"></param>
 internal TcpAcceptModule(AsyncServerSocket server)
 {
     this.server = server;
 }