Esempio n. 1
0
        public void SendReq <T>(int tag, SprotoTypeBase obj, long session, object ud)
        {
            //UnityEngine.Debug.Assert(_tcpflag == true);
            if (_tcpflag)
            {
                if (session == 0)
                {
                    byte[] d = _sendRequest.Invoke <T>(obj, null);
                    UnityEngine.Debug.Assert(d != null);
                    _tcp.Send(d, 0, d.Length);
                }
                else
                {
                    byte[] d = _sendRequest.Invoke <T>(obj, session);
                    UnityEngine.Debug.Assert(d != null);

                    RspPg pg = new RspPg();
                    pg.Tag     = tag;
                    pg.Session = session;
                    pg.Index   = _index;
                    pg.Version = _version;
                    pg.Ud      = ud;

                    string key = idToHex(session);
                    _rspPg[key] = pg;
                    _tcp.Send(d, 0, d.Length);
                }
            }
        }
Esempio n. 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);
             }
         }
     }
 }