Exemple #1
0
            public static void __Dispatch(SprotoRpc.RpcInfo info)
            {
                Sproto.SprotoTypeBase ret = null;
                switch (info.tag)
                {
                case Protocol.foobar.Tag:
                    ret = S2C.foobar((SprotoType.foobar.request)info.requestObj);
                    break;

                case Protocol.foo.Tag:
                    ret = S2C.foo();
                    break;

                case Protocol.blackhole.Tag:
                    S2C.blackhole((SprotoType.blackhole.request)info.requestObj);
                    break;

                case Protocol.bar.Tag:
                    S2C.bar();
                    break;

                default:
                    break;
                }
                if (null != info.Response)
                {
                    SendData(info.Response(ret));
                }
            }
Exemple #2
0
 private void OnRecvive(byte[] data, int start, int length)
 {
     if (length <= 0)
     {
         return;
     }
     if (_handshake)
     {
         byte[] buffer = new byte[length];
         Array.Copy(data, start, buffer, 0, length);
         if (_step == 1)
         {
             string str  = Encoding.ASCII.GetString(buffer);
             int    code = Int32.Parse(str.Substring(0, 3));
             string msg  = str.Substring(4);
             if (code == 200)
             {
                 _tcpflag   = true;
                 _step      = 0;
                 _handshake = false;
                 //_tcp.SetEnabledPing(true);
                 //_tcp.SendPing();
                 UnityEngine.Debug.Log(string.Format("{0},{1}", code, msg));
                 if (OnAuthed != null)
                 {
                     OnAuthed(code);
                 }
             }
             else if (code == 403)
             {
                 _step      = 0;
                 _handshake = false;
                 if (OnAuthed != null)
                 {
                     OnAuthed(code);
                 }
             }
             else
             {
                 UnityEngine.Debug.Assert(false);
                 _step = 0;
                 DoAuth();
                 UnityEngine.Debug.LogError(string.Format("error code : {0}, {1}", code, msg));
                 if (OnAuthed != null)
                 {
                     OnAuthed(code);
                 }
             }
         }
     }
     else
     {
         byte[] buffer = new byte[length];
         Array.Copy(data, start, buffer, 0, length);
         if (_clientSockScriptEnable)
         {
             if (_clientSockScript.recv(Encoding.ASCII.GetString(buffer)))
             {
                 return;
             }
         }
         SprotoRpc.RpcInfo sinfo = _host.Dispatch(buffer);
         if (sinfo.type == SprotoRpc.RpcType.REQUEST)
         {
             int tag = (int)sinfo.tag;
             try {
                 var cb = _req[tag];
                 if (sinfo.session != null && sinfo.session > 0)
                 {
                     SprotoTypeBase rsp = cb((uint)sinfo.session, sinfo.requestObj);
                     byte[]         d   = sinfo.Response(rsp);
                     _tcp.Send(d, 0, d.Length);
                 }
                 else
                 {
                     cb(0, sinfo.requestObj);
                 }
             } catch (Exception ex) {
                 UnityEngine.Debug.LogException(ex);
             }
         }
         else if (sinfo.type == SprotoRpc.RpcType.RESPONSE)
         {
             UnityEngine.Debug.Assert(sinfo.session != null);
             long   session = (long)sinfo.session;
             string key     = idToHex(session);
             try {
                 RspPg pg = _rspPg[key];
                 var   cb = _rsp[pg.Tag];
                 cb((uint)session, sinfo.responseObj, pg.Ud);
                 _rspPg.Remove(key);
             } catch (Exception ex) {
                 UnityEngine.Debug.LogException(ex);
             }
         }
     }
 }
Exemple #3
0
        public override void run()
        {
            SprotoRpc client  = new SprotoRpc();
            SprotoRpc service = new SprotoRpc(Protocol.Instance);

            SprotoRpc.RpcRequest clientRequest = client.Attach(Protocol.Instance);

            // ===============foobar=====================
            // request

            SprotoType.foobar.request obj = new SprotoType.foobar.request();
            obj.what = "foo";
            byte[] req = clientRequest.Invoke <Protocol.foobar> (obj, 1);
            assert(req, new byte[] { 0X55, 0X02, 0X04, 0X04, 0X01, 0Xc4, 0X03, 0X66, 0X6f, 0X01, 0X6f });

            // dispatch
            SprotoRpc.RpcInfo sinfo = service.Dispatch(req);
            assert(sinfo.type == SprotoRpc.RpcType.REQUEST);
            assert(sinfo.requestObj.GetType() == typeof(SprotoType.foobar.request));
            assert(sinfo.Response != null);
            SprotoType.foobar.request req_obj = (SprotoType.foobar.request)sinfo.requestObj;
            assert(req_obj.what == "foo");

            // response
            SprotoType.foobar.response obj2 = new SprotoType.foobar.response();
            obj2.ok = true;
            byte[] resp = sinfo.Response(obj2);
            assert(resp, new byte[] { 0X55, 0X02, 0X01, 0X04, 0X01, 0X01, 0X04 });

            // dispatch
            sinfo = client.Dispatch(resp);
            assert(sinfo.type == SprotoRpc.RpcType.RESPONSE);
            assert(sinfo.session == 1);
            assert(((SprotoType.foobar.response)sinfo.responseObj).ok == true);

            // ================foo====================
            // request
            req = clientRequest.Invoke <Protocol.foo> (null, 2);
            assert(req, new byte[] { 0X15, 0X02, 0X06, 0X06 });

            // dispatch
            sinfo = service.Dispatch(req);
            assert(sinfo.type == SprotoRpc.RpcType.REQUEST);
            assert(sinfo.tag == Protocol.foo.Tag);
            assert(sinfo.requestObj == null);

            // response
            SprotoType.foo.response obj3 = new SprotoType.foo.response();
            obj3.ok = false;
            resp    = sinfo.Response(obj3);
            assert(resp, new byte[] { 0X55, 0X02, 0X01, 0X06, 0X01, 0X01, 0X02 });

            // dispatch
            sinfo = client.Dispatch(resp);
            assert(sinfo.type == SprotoRpc.RpcType.RESPONSE);
            assert(sinfo.session == 2);
            assert(((SprotoType.foo.response)sinfo.responseObj).ok == false);

            // ================bar====================
            // request
            req = clientRequest.Invoke <Protocol.bar> ();
            assert(req, new byte[] { 0X05, 0X01, 0X08, });

            // dispatch
            sinfo = service.Dispatch(req);
            assert(sinfo.type == SprotoRpc.RpcType.REQUEST);
            assert(sinfo.requestObj == null);
            assert(sinfo.tag == Protocol.bar.Tag);
            assert(sinfo.Response == null);

            // ================blackhole====================
            // request
            req = clientRequest.Invoke <Protocol.blackhole> ();
            assert(req, new byte[] { 0X05, 0X01, 0X0a });
        }