示例#1
0
        public void open(string parameters)
        {
            string socketKey = JsonHelper.Deserialize <string[]>(parameters)[0];
            string host      = JsonHelper.Deserialize <string[]>(parameters)[1];
            int    port      = int.Parse(JsonHelper.Deserialize <string[]>(parameters)[2]);

            ISocketAdapter socketAdapter = new SocketAdapter();

            socketAdapter.CloseEventHandler = (hasError) => this.CloseEventHandler(socketKey, hasError);
            socketAdapter.DataConsumer      = (data) => this.DataConsumer(socketKey, data);
            socketAdapter.ErrorHandler      = (ex) => this.ErrorHandler(socketKey, ex);

            try
            {
                socketAdapter.Connect(host, port).Wait();

                this.socketStorage.Add(socketKey, socketAdapter);

                this.DispatchCommandResult(new PluginResult(PluginResult.Status.OK));
            }
            catch (SocketException ex)
            {
                this.DispatchCommandResult(new PluginResult(PluginResult.Status.IO_EXCEPTION, ex.Message));
            }
            catch (AggregateException ex)
            {
                this.DispatchCommandResult(new PluginResult(PluginResult.Status.IO_EXCEPTION, ex.InnerException.Message));
            }
        }
示例#2
0
        public void open(string parameters)
        {
            string socketKey = JsonHelper.Deserialize<string[]>(parameters)[0];
            string host = JsonHelper.Deserialize<string[]>(parameters)[1];
            int port = int.Parse(JsonHelper.Deserialize<string[]>(parameters)[2]);

            ISocketAdapter socketAdapter = new SocketAdapter();
            socketAdapter.CloseEventHandler = (hasError) => this.CloseEventHandler(socketKey, hasError);
            socketAdapter.DataConsumer = (data) => this.DataConsumer(socketKey, data);
            socketAdapter.ErrorHandler = (ex) => this.ErrorHandler(socketKey, ex);

            try
            {
                socketAdapter.Connect(host, port).Wait();

                this.socketStorage.Add(socketKey, socketAdapter);

                this.DispatchCommandResult(new PluginResult(PluginResult.Status.OK));
            }
            catch (SocketException ex)
            {
                this.DispatchCommandResult(new PluginResult(PluginResult.Status.IO_EXCEPTION, ex.Message));
            }
            catch (AggregateException ex)
            {
                this.DispatchCommandResult(new PluginResult(PluginResult.Status.IO_EXCEPTION, ex.InnerException.Message));
            }
        }
        private async Task <string> OpenTask(string socketKey, string host, int port)
        {
            PluginResult result = new PluginResult();
            await Task.Run(async() =>
            {
                ISocketAdapter socketAdapter    = new SocketAdapter();
                socketAdapter.CloseEventHandler = async(hasError) => await closeEventDelegate(socketKey, hasError);
                socketAdapter.DataConsumer      = async(data) => await dataConsumeDelegate(socketKey, data.ToArray());
                socketAdapter.ErrorHandler      = async(ex) => await errorDelegate(socketKey, ex);

                try
                {
                    await socketAdapter.Connect(host, port);
                    socketStorage.Add(socketKey, socketAdapter);
                    result.Result = PluginResult.Status.OK;
                }
                catch (AggregateException ex)
                {
                    result.Result  = PluginResult.Status.IO_EXCEPTION;
                    result.Message = ex.InnerException.Message;
                }
                catch (Exception ex)
                {
                    result.Result  = PluginResult.Status.IO_EXCEPTION;
                    result.Message = ex.Message;
                }
            });

            JsonObject jsonObject = new JsonObject();

            jsonObject.SetNamedValue("Result", JsonValue.CreateNumberValue((int)result.Result));
            if (result.Message != null)
            {
                jsonObject.SetNamedValue("Message", JsonValue.CreateStringValue(result.Message));
            }

            return(jsonObject.ToString());
        }