コード例 #1
0
    private void ConnectResult(IAsyncResult rec_socket)
    {
        try
        {
            this.socket = (Socket)rec_socket.AsyncState;
            this.socket.EndConnect(rec_socket);
            this.socket.NoDelay = true;

            this.socket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.SendTimeout, 2000);
            this.socket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.KeepAlive, true);

            Debug.Log("与服务器" + this.socket.RemoteEndPoint + "连接成功");
            NetworkManagerV2.AddEvent(Protocal.Connect, new ByteBuffer());

            this.thd = new Thread(new ThreadStart(this.Work));
            this.thd.Start();
        }
        catch (Exception exc)
        {
            this.CloseConnect();
            //netUnit.AddEvent(NotiConst.ConnectError, this.netUnit.netType);
            Debug.Log(" 2 开始与服务器进行连接异常" + exc.ToString());

            NetworkManagerV2.AddEvent(Protocal.ConnectError, new ByteBuffer());
        }
    }
コード例 #2
0
    static int SendMessage(IntPtr L)
    {
        try
        {
            int count = LuaDLL.lua_gettop(L);

            if (count == 2 && TypeChecker.CheckTypes(L, 1, typeof(NetworkManagerV2), typeof(string)))
            {
                NetworkManagerV2 obj  = (NetworkManagerV2)ToLua.ToObject(L, 1);
                string           arg0 = ToLua.ToString(L, 2);
                obj.SendMessage(arg0);
                return(0);
            }
            else if (count == 2 && TypeChecker.CheckTypes(L, 1, typeof(NetworkManagerV2), typeof(LuaFramework.ByteBuffer)))
            {
                NetworkManagerV2        obj  = (NetworkManagerV2)ToLua.ToObject(L, 1);
                LuaFramework.ByteBuffer arg0 = (LuaFramework.ByteBuffer)ToLua.ToObject(L, 2);
                obj.SendMessage(arg0);
                return(0);
            }
            else if (count == 3 && TypeChecker.CheckTypes(L, 1, typeof(NetworkManagerV2), typeof(string), typeof(UnityEngine.SendMessageOptions)))
            {
                NetworkManagerV2 obj  = (NetworkManagerV2)ToLua.ToObject(L, 1);
                string           arg0 = ToLua.ToString(L, 2);
                UnityEngine.SendMessageOptions arg1 = (UnityEngine.SendMessageOptions)ToLua.ToObject(L, 3);
                obj.SendMessage(arg0, arg1);
                return(0);
            }
            else if (count == 3 && TypeChecker.CheckTypes(L, 1, typeof(NetworkManagerV2), typeof(string), typeof(object)))
            {
                NetworkManagerV2 obj  = (NetworkManagerV2)ToLua.ToObject(L, 1);
                string           arg0 = ToLua.ToString(L, 2);
                object           arg1 = ToLua.ToVarObject(L, 3);
                obj.SendMessage(arg0, arg1);
                return(0);
            }
            else if (count == 4 && TypeChecker.CheckTypes(L, 1, typeof(NetworkManagerV2), typeof(string), typeof(object), typeof(UnityEngine.SendMessageOptions)))
            {
                NetworkManagerV2 obj  = (NetworkManagerV2)ToLua.ToObject(L, 1);
                string           arg0 = ToLua.ToString(L, 2);
                object           arg1 = ToLua.ToVarObject(L, 3);
                UnityEngine.SendMessageOptions arg2 = (UnityEngine.SendMessageOptions)ToLua.ToObject(L, 4);
                obj.SendMessage(arg0, arg1, arg2);
                return(0);
            }
            else
            {
                return(LuaDLL.luaL_throw(L, "invalid arguments to method: NetworkManagerV2.SendMessage"));
            }
        }
        catch (Exception e)
        {
            return(LuaDLL.toluaL_exception(L, e));
        }
    }
コード例 #3
0
ファイル: NetworkManagerV2.cs プロジェクト: madswab/GDV4
 private void Awake()
 {
     if (single != null)
     {
         Destroy(this);
     }
     else
     {
         single = this;
     }
 }
コード例 #4
0
 static int GetNetManager(IntPtr L)
 {
     try
     {
         ToLua.CheckArgsCount(L, 0);
         NetworkManagerV2 o = LuaFramework.LuaHelper.GetNetManager();
         ToLua.Push(L, o);
         return(1);
     }
     catch (Exception e)
     {
         return(LuaDLL.toluaL_exception(L, e));
     }
 }
コード例 #5
0
 static int SendConnect(IntPtr L)
 {
     try
     {
         ToLua.CheckArgsCount(L, 1);
         NetworkManagerV2 obj = (NetworkManagerV2)ToLua.CheckObject(L, 1, typeof(NetworkManagerV2));
         obj.SendConnect();
         return(0);
     }
     catch (Exception e)
     {
         return(LuaDLL.toluaL_exception(L, e));
     }
 }
コード例 #6
0
 static int AddEvent(IntPtr L)
 {
     try
     {
         ToLua.CheckArgsCount(L, 2);
         int arg0 = (int)LuaDLL.luaL_checknumber(L, 1);
         LuaFramework.ByteBuffer arg1 = (LuaFramework.ByteBuffer)ToLua.CheckObject(L, 2, typeof(LuaFramework.ByteBuffer));
         NetworkManagerV2.AddEvent(arg0, arg1);
         return(0);
     }
     catch (Exception e)
     {
         return(LuaDLL.toluaL_exception(L, e));
     }
 }
コード例 #7
0
    static int get_ip(IntPtr L)
    {
        object o = null;

        try
        {
            o = ToLua.ToObject(L, 1);
            NetworkManagerV2 obj = (NetworkManagerV2)o;
            string           ret = obj.ip;
            LuaDLL.lua_pushstring(L, ret);
            return(1);
        }
        catch (Exception e)
        {
            return(LuaDLL.toluaL_exception(L, e, o == null ? "attempt to index ip on a nil value" : e.Message));
        }
    }
コード例 #8
0
    static int get_port(IntPtr L)
    {
        object o = null;

        try
        {
            o = ToLua.ToObject(L, 1);
            NetworkManagerV2 obj = (NetworkManagerV2)o;
            int ret = obj.port;
            LuaDLL.lua_pushinteger(L, ret);
            return(1);
        }
        catch (Exception e)
        {
            return(LuaDLL.toluaL_exception(L, e, o == null ? "attempt to index port on a nil value" : e.Message));
        }
    }
コード例 #9
0
    static int set_headTimer(IntPtr L)
    {
        object o = null;

        try
        {
            o = ToLua.ToObject(L, 1);
            NetworkManagerV2 obj  = (NetworkManagerV2)o;
            float            arg0 = (float)LuaDLL.luaL_checknumber(L, 2);
            obj.headTimer = arg0;
            return(0);
        }
        catch (Exception e)
        {
            return(LuaDLL.toluaL_exception(L, e, o == null ? "attempt to index headTimer on a nil value" : e.Message));
        }
    }
コード例 #10
0
    static int set_newWork(IntPtr L)
    {
        object o = null;

        try
        {
            o = ToLua.ToObject(L, 1);
            NetworkManagerV2 obj  = (NetworkManagerV2)o;
            SelfNetwork      arg0 = (SelfNetwork)ToLua.CheckObject(L, 2, typeof(SelfNetwork));
            obj.newWork = arg0;
            return(0);
        }
        catch (Exception e)
        {
            return(LuaDLL.toluaL_exception(L, e, o == null ? "attempt to index newWork on a nil value" : e.Message));
        }
    }
コード例 #11
0
    static int set_ip(IntPtr L)
    {
        object o = null;

        try
        {
            o = ToLua.ToObject(L, 1);
            NetworkManagerV2 obj  = (NetworkManagerV2)o;
            string           arg0 = ToLua.CheckString(L, 2);
            obj.ip = arg0;
            return(0);
        }
        catch (Exception e)
        {
            return(LuaDLL.toluaL_exception(L, e, o == null ? "attempt to index ip on a nil value" : e.Message));
        }
    }
コード例 #12
0
    static int get_headTimer(IntPtr L)
    {
        object o = null;

        try
        {
            o = ToLua.ToObject(L, 1);
            NetworkManagerV2 obj = (NetworkManagerV2)o;
            float            ret = obj.headTimer;
            LuaDLL.lua_pushnumber(L, ret);
            return(1);
        }
        catch (Exception e)
        {
            return(LuaDLL.toluaL_exception(L, e, o == null ? "attempt to index headTimer on a nil value" : e.Message));
        }
    }
コード例 #13
0
    static int get_newWork(IntPtr L)
    {
        object o = null;

        try
        {
            o = ToLua.ToObject(L, 1);
            NetworkManagerV2 obj = (NetworkManagerV2)o;
            SelfNetwork      ret = obj.newWork;
            ToLua.PushObject(L, ret);
            return(1);
        }
        catch (Exception e)
        {
            return(LuaDLL.toluaL_exception(L, e, o == null ? "attempt to index newWork on a nil value" : e.Message));
        }
    }
コード例 #14
0
    public void SendMessage(ByteBuffer buffer)
    {
        try
        {
            if (this.IsSocketConnected())
            {
                if (this.socket.Poll(0, SelectMode.SelectWrite))
                {
                    //Serializer des = new Serializer();
                    //des.SendData(pkt, pkt.pktType);
                    ////SendData(des.sendBuff, des.nowWriteIndex);

                    byte[] bt     = buffer.ToBytes();
                    int    length = bt.Length;

                    byte[] bts = new byte[length + 4];
                    ByteCode.copyByte(bt, 0, bts, 4, length);
                    ByteCode.putInt(bts, length, 0);
                    this.socket.Send(bts);

                    buffer.Close();
                }
                else
                {
                    NetworkManagerV2.AddEvent(Protocal.Exception, new ByteBuffer());
                    //this.netUnit.AddEvent(NotiConst.NetError, this.netUnit.netType);
                    Debug.Log("this.IsSocketConnected() falase");
                }
            }
            else
            {
                NetworkManagerV2.AddEvent(Protocal.Exception, new ByteBuffer());
                //this.netUnit.AddEvent(NotiConst.NetError, this.netUnit.netType);
                Debug.Log("this.IsSocketConnected() falase");
            }
        }
        catch (Exception e)
        {
            NetworkManagerV2.AddEvent(Protocal.Exception, new ByteBuffer());
            Debug.Log(" net error " + e.ToString());
        }
    }
コード例 #15
0
    private void DecodeData()
    {
        while (true)
        {
            if (nowPktLength == -1)
            {
                //Debug.Log(" parse nowPktLength " + nowPktLength);
                if (nowWriteIndex - nowReadIndex >= 4)
                {
                    byte[] B2 = new byte[4];
                    ByteCode.copyByte(allReceiveByte, nowReadIndex, B2, 0, 4);
                    nowReadIndex += 4;
                    nowPktLength  = ByteCode.getInt(B2, 0);
                }
                else
                {
                    break;
                }
            }

            if (nowPktLength != -1)
            {
                //Debug.Log(" deserialize nowPktLength " + nowPktLength);
                if (nowWriteIndex - nowReadIndex >= nowPktLength)
                {
                    byte[] needReadBytes = new byte[nowPktLength];
                    ByteCode.copyByte(allReceiveByte, nowReadIndex, needReadBytes, 0, nowPktLength);
                    nowReadIndex += nowPktLength;
                    nowPktLength  = -1;


                    ByteBuffer buffer = new ByteBuffer(needReadBytes);
                    NetworkManagerV2.AddEvent(Protocal.Message, buffer);
                }
                else
                {
                    break;
                }
            }
        }
    }
コード例 #16
0
    public void LinkServer(string ip, int port)
    {
        try
        {
            OnRegister();

            IPEndPoint epServer = new IPEndPoint(IPAddress.Parse(ip), port);
            Socket     socket   = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);

            socket.BeginConnect(epServer, new AsyncCallback(ConnectResult), socket);

            Debug.Log(" 开始与" + ip + ":" + port + "服务器进行连接 ");
        }
        catch (Exception exc)
        {
            this.CloseConnect();

            //netUnit.AddEvent(NotiConst.ConnectError, this.netUnit.netType);
            Debug.Log("1 与服务器进行连接异常" + exc.ToString());

            NetworkManagerV2.AddEvent(Protocal.ConnectError, new ByteBuffer());
        }
    }
コード例 #17
0
    public void Work()
    {
        while (true)
        {
            try
            {
                if (this.IsSocketConnected())
                {
                    //如果远程主机处于关机状态或关闭了连接,则 Available 会引发 SocketException
                    //if (this.socket.Available != 0) 妈的这个有 BUG 别用

                    //C# 进行SOCKET开发---当连接与与服务器断开时的问题 http://blog.csdn.net/qwlovedzm/article/details/6171992

                    //C# 如何判断socket连接断开? http://bbs.csdn.net/topics/290010343

                    //https://msdn.microsoft.com/zh-cn/library/system.net.sockets.socket.poll.aspx
                    //此方法不能检测某些类型的连接问题,例如,网络电缆中断或远程主机意外关闭。您必须尝试发送或接收数据以检测这些类型的错误
                    if (this.socket.Poll(0, SelectMode.SelectRead))
                    {
                        byte[] bt   = new byte[buffSize];
                        int    leng = this.socket.Receive(bt, bt.Length, SocketFlags.None);
                        Debug.Log(" leng " + leng);
                        //Debug.Log(" this.socket.Receiv leng  " + leng);
                        //为什么TCP Socket.Receive不阻塞,直接返回0字节
                        //https://social.msdn.microsoft.com/Forums/vstudio/zh-CN/fcc03f1b-4329-4bc7-be60-09210629169c/tcp-socketreceive0
                        if (leng <= 0)
                        {
                            CloseConnect();
                            NetworkManagerV2.AddEvent(Protocal.Disconnect, new ByteBuffer());
                            Debug.Log("net error  Receive 0 ");
                            break;
                        }
                        else
                        {
                            if (leng > buffSize - nowWriteIndex)
                            {
                                Debug.Log("缓冲区不够了" + (buffSize - nowWriteIndex));
                                byte[] newReceiveByte = new byte[buffSize];
                                ByteCode.copyByte(allReceiveByte, nowReadIndex, newReceiveByte, 0, nowWriteIndex - nowReadIndex);
                                allReceiveByte = newReceiveByte;
                                nowWriteIndex  = nowWriteIndex - nowReadIndex;
                                nowReadIndex   = 0;
                            }
                            ByteCode.copyByte(bt, 0, allReceiveByte, nowWriteIndex, leng);
                            //bt = null;
                            nowWriteIndex = nowWriteIndex + leng;
                            //Debug.Log(" nowWriteIndex  " + nowWriteIndex);
                            DecodeData();
                            //Debug.Log(" nowReadIndex  " + nowReadIndex);
                        }
                    }
                }
                else
                {
                    CloseConnect();
                    NetworkManagerV2.AddEvent(Protocal.Exception, new ByteBuffer());
                    Debug.Log("  !this.IsSocketConnected()  ");
                    break;
                }
            }
            catch (Exception e)
            {
                CloseConnect();
                NetworkManagerV2.AddEvent(Protocal.Exception, new ByteBuffer());
                Debug.Log("  net error  " + e.ToString());
                break;
            }

            //when close connection
            //if not add below will raise
            //System.ObjectDisposedException: The object was used after being disposed.
            Thread.Sleep(10);
        }
    }