예제 #1
0
        public async Task <T> Request <T>(string command, params object[] pars)
        {
            ClientConnect client = new ClientConnect(false);
            bool          ret    = await client.Connect <LemonMessage>(IP, Port);

            if (!ret)
            {
                throw new Exception("网络连接失败");
            }
            string       sendParStr = ParameterConverter.PackParameter(command, SerializeObject, pars);
            LemonMessage message    = (LemonMessage)await client.SendAndBack(new LemonMessage()
            {
                Body = sendParStr
            });

            if (message == null)
            {
                return(default(T));
            }
            return(ParameterConverter.UnpackOneParameter <T>(message.Body, SerializeObject));
        }
예제 #2
0
        public void Start(int port, ISerializeObject serializeObject, Assembly assembly = null)
        {
            if (assembly != null)
            {
                RouteManager.RegistAssembly(assembly);
            }
            SerializeObject = serializeObject;

            SocketServer = new SocketServer();
            SocketServer.Start <LemonMessage>(port);
            SocketServer.SetOnReceiveEvent((s, m) =>
            {
                LemonMessage oldmsg = (LemonMessage)m;
                List <string> pars  = oldmsg.Body.Split(new string[] { ParameterConverter.SplitString }, StringSplitOptions.None).ToList();
                string command      = pars[0];
                pars.RemoveAt(0);
                try
                {
                    bool needBack = false;
                    object ret    = RouteManager.ActionStringPars(command, pars.ToArray(), SerializeObject, ref needBack);
                    if (needBack)
                    {
                        LemonMessage msg = new LemonMessage();
                        msg.Body         = ParameterConverter.PackOneParameter(ret, serializeObject);
                        msg.StateCode    = 0;
                        s.SendMessage(msg);
                    }
                }
                catch (Exception ex)
                {
                    LemonMessage msg = new LemonMessage();
                    msg.Body         = ex.Message;
                    msg.StateCode    = -1;
                    s.SendMessage(msg);
                }
            });
        }