Example #1
0
    /// <summary>
    /// parse input data
    /// </summary>
    /// <param name="gameAction"></param>
    /// <param name="actionParam"></param>
    /// <param name="formater"></param>
    /// <param name="bShowLoading"></param>
    private void SocketRequest(GameAction gameAction, Google.Protobuf.IMessage pbData, IHeadFormater formater, bool bShowLoading)
    {
        if (mSocket == null)
        {
            string   url = NetWriter.GetUrl();
            string[] arr = url.Split(new char[] { ':' });
            if (arr.Length != 2)
            {
                Debug.LogError("Url is error:" + url);
                return;
            }
            int nPort = int.Parse(arr[1]);
            mSocket = new SocketConnect(arr[0], nPort, formater);
        }
        gameAction.Head.MsgId = NetWriter.MsgId - 1;

        SocketPackage package = new SocketPackage();

        package.MsgId        = gameAction.Head.MsgId;
        package.ActionId     = gameAction.ActionId;
        package.ActionRespId = gameAction.Head.ActionRespId;
        package.Action       = gameAction;
        package.HasLoading   = bShowLoading;
        package.SendTime     = DateTime.Now;
        byte[] data = gameAction.Send(pbData);
        NetWriter.resetData();
        if (bShowLoading)
        {
            RequestDelegate(Status.eStartRequest);
        }
        mSocket.Send(data, package);
    }
Example #2
0
    private IEnumerator HttpGetRequest(GameAction gameAction, ActionParam actionParam, IHeadFormater formater, bool showLoading)
    {
        string url = NetWriter.GetUrl();

        byte[]      postData    = gameAction.Send(actionParam);
        DateTime    start       = DateTime.Now;
        HttpPackage httpPackage = new HttpPackage();

        httpPackage.WwwObject = NetWriter.IsGet ? new WWW(string.Format("{0}?{1}", url, Encoding.UTF8.GetString(postData))) : new WWW(url, postData);
        httpPackage.ActionId  = gameAction.ActionId;
        httpPackage.Action    = gameAction;
        httpPackage.Reader    = new NetReader(formater);

        if (RequestNotify != null && showLoading)
        {
            RequestNotify(Net.Status.eStartRequest);
        }

        yield return(httpPackage.WwwObject);

        if (RequestNotify != null && showLoading)
        {
            RequestNotify(Net.Status.eEndRequest);
        }
        TimeSpan tsStart = new TimeSpan(start.Ticks);
        TimeSpan tsEnd   = new TimeSpan(DateTime.Now.Ticks);
        TimeSpan ts      = tsEnd.Subtract(tsStart).Duration();

        if (ts.Seconds > OVER_TIME)
        {
            httpPackage.IsOverTime = true;
        }
        OnHttpRespond(httpPackage);
    }
Example #3
0
    /// <summary>
    /// parse input data
    /// </summary>
    /// <param name="gameAction"></param>
    /// <param name="actionParam"></param>
    /// <param name="formater"></param>
    /// <param name="bShowLoading"></param>
    private void SocketRequest(GameAction gameAction, ActionParam actionParam, IHeadFormater formater, bool bShowLoading)
    {
        if (mSocket == null)
        {
            string   strUrl = NetWriter.GetUrl();
            string[] arr    = strUrl.Split(new char[] { ':' });
            int      nPort  = int.Parse(arr[1]);
            mSocket = new SocketConnect(arr[0], nPort, formater);
        }
        gameAction.Head.MsgId = NetWriter.MsgId - 1;

        SocketPackage package = new SocketPackage();

        package.MsgId      = gameAction.Head.MsgId;
        package.ActionId   = gameAction.ActionId;
        package.Action     = gameAction;
        package.HasLoading = bShowLoading;
        package.SendTime   = DateTime.Now;
        byte[] data = gameAction.Send(actionParam);
        NetWriter.resetData();
        if (bShowLoading)
        {
            RequestDelegate(Status.eStartRequest);
        }
        mSocket.Send(data, package);
    }
Example #4
0
    /// <summary>
    /// 每一帧被调用多次,enabled=false时禁用
    /// </summary>
    void OnGUI()
    {
        int cwidth  = Screen.width / 2;
        int cheight = Screen.height / 2;
        var boxPos  = new Rect(cwidth - 110, cheight - 160, 240, 120);

        GUI.Box(boxPos, "");
        GUI.Label(new Rect(cwidth - 100, cheight - 150, 100, 22), "User:"******"Password:"******"Regist"))
        {
            //TODO:登录服务器分布式访问
            //NetWriter.SetUrl("http://pass.scutgame.com/", ResponseContentType.Json, true);
            //Debug.Log("Started to access login server, resp-type:" + NetWriter.ResponseContentType.ToString());

            NetWriter.SetUrl("127.0.0.1:9001");
            Net.Instance.Send((int)ActionType.Regist, RegistCallback, null);
        }

        if (GUI.Button(new Rect(cwidth, cheight - 80, 80, 22), "Login"))
        {
            NetWriter.SetUrl("127.0.0.1:9001");

            Debug.Log(NetWriter.GetUrl());
            GameSetting.Instance.Pid      = user;
            GameSetting.Instance.Password = pwd;
            Net.Instance.Send((int)ActionType.Login, LoginCallback, null);
        }
    }
Example #5
0
    /// <summary>
    /// parse input data
    /// </summary>
    /// <param name="url"></param>
    /// <param name="callback"></param>
    /// <param name="ud"></param>
    public void SocketRequest(int actionId, INetCallback callback, object userData, bool bShowLoading)
    {
        if (mSocket == null)
        {
            string strUrl = NetWriter.GetUrl();
            Debug.Log("url" + strUrl);
            string[] arr   = strUrl.Split(new char[] { ':' });
            int      nPort = int.Parse(arr[1]);
            mSocket = new SocketConnect(arr[0], nPort);
        }
        NetWriter writer = NetWriter.Instance;

        writer.writeInt32("actionId", actionId);
        byte[]        data    = NetWriter.Instance.PostData();
        SocketPackage package = new SocketPackage();

        package.FuncCallback = callback;
        package.UserData     = userData;
        package.MsgId        = NetWriter.MsgId - 1;
        package.ActionId     = actionId;
        package.HasLoading   = bShowLoading;
        package.SendTime     = DateTime.Now;
        NetWriter.resetData();

        if (bShowLoading)
        {
            RequestDelegate(Status.eStartRequest);
        }
        mSocket.Request(data, package);
    }
Example #6
0
    static int GetUrl(IntPtr L)
    {
        LuaScriptMgr.CheckArgsCount(L, 0);
        string o = NetWriter.GetUrl();

        LuaScriptMgr.Push(L, o);
        return(1);
    }
Example #7
0
 static int GetUrl(IntPtr L)
 {
     try
     {
         ToLua.CheckArgsCount(L, 0);
         string o = NetWriter.GetUrl();
         LuaDLL.lua_pushstring(L, o);
         return(1);
     }
     catch (Exception e)
     {
         return(LuaDLL.toluaL_exception(L, e));
     }
 }
Example #8
0
 public void ConnectedServer()
 {
     UnityEngine.NetworkReachability state = UnityEngine.Application.internetReachability;
     if (state != UnityEngine.NetworkReachability.NotReachable)
     {
         NetWriter.SetUrl(serverIp + ":" + port);
         //if (mSocket == null)
         {
             string   strUrl = NetWriter.GetUrl();
             string[] arr    = strUrl.Split(new char[] { ':' });
             int      nPort  = int.Parse(arr[1]);
             mSocket = new SocketConnect(arr[0], nPort, HeadFormater, timeOut, hearInterval);
         }
         mSocket.EnsureConnected();
     }
     else
     {
         QueueEvent(EventDef.ServerConnectFailure, "当前网络没有连接");
     }
 }
Example #9
0
    private void SocketRequest(int actionId, int actionRespId, LuaFramework.ByteBuffer body, LuaInterface.LuaFunction callback, IHeadFormater formater, bool bShowLoading)
    {
        if (mSocket == null)
        {
            string   url = NetWriter.GetUrl();
            string[] arr = url.Split(new char[] { ':' });
            if (arr.Length != 2)
            {
                Debug.LogError("Url is error:" + url);
                return;
            }
            int nPort = int.Parse(arr[1]);
            mSocket = new SocketConnect(arr[0], nPort, formater);
        }

        SocketPackage package = new SocketPackage();

        package.MsgId        = NetWriter.MsgId - 1;
        package.ActionId     = actionId;
        package.ActionRespId = actionRespId;
        package.HasLoading   = bShowLoading;
        package.SendTime     = DateTime.Now;
        package.Callback     = callback;

        NetWriter writer = NetWriter.Instance;

        SetActionHead(actionId, body);
        writer.SetBodyData(body.ToBytes());
        byte[] data = writer.PostData();
        NetWriter.resetData();

        if (bShowLoading)
        {
            RequestDelegate(Status.eStartRequest);
        }
        mSocket.Send(data, package);
    }