//接收到网络消息
 private void OnReceive(byte[] bytes)
 {
     try
     {
         SprotoRpc.RpcInfo sinfo = _sprotoRpc.Dispatch(bytes);
         _onEvent.Invoke(sinfo);
     }
     catch (Exception e)
     {
         Debug.LogError(e);
     }
 }
Exemple #2
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 });
		}
Exemple #3
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 #4
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 });
        }