Ejemplo n.º 1
0
        // 投递接收数据操作
        public bool PostRcv()
        {
            if (netSocket == null || IsReadClose() || postRcving)
            {
                Trace.Assert(false, "postRcving is err");
                return(false);
            }

            byte[] rcvBuffer    = null;
            Int32  bufferOffset = 0;
            Int32  bufferLength = 0;

            rcvDataBuffer.GetRcvBuffer(ref rcvBuffer, ref bufferOffset, ref bufferLength);
            if (rcvBuffer != null && bufferOffset >= 0 && bufferLength > 0)
            {
                postRcving = true;
                rcvAsyncEventArgs.SetBuffer(rcvBuffer, bufferOffset, bufferLength);
                if (!netSocket.ReceiveAsync(rcvAsyncEventArgs))  // 如果返回 false 表示同步IO完成
                {
                    NetModuleMgr.GetInstance().ProcessRcvComplete(rcvAsyncEventArgs);
                }
            }

            return(true);
        }
Ejemplo n.º 2
0
        // 投递发送数据操作
        public bool PostSend()
        {
            if (netSocket == null || IsReadClose())
            {
                return(false);
            }

            if (sendDataBufferMgr.IsSending())
            {
                return(true);
            }

            byte[] sendDataBuffer = null;
            Int32  dataOffset     = 0;
            Int32  dataLength     = 0;

            sendDataBufferMgr.GetSendBuffer(ref sendDataBuffer, ref dataOffset, ref dataLength);
            if (sendDataBuffer != null && dataOffset >= 0 && dataLength > 0)
            {
                sendAsyncEventArgs.SetBuffer(sendDataBuffer, dataOffset, dataLength);
                if (!netSocket.SendAsync(sendAsyncEventArgs)) // 如果返回 false 表示同步IO完成
                {
                    NetModuleMgr.GetInstance().ProcessSendComplete(sendAsyncEventArgs);
                }
            }

            return(true);
        }
Ejemplo n.º 3
0
        // 关闭
        public void Shutdown()
        {
            // 关闭网络底层
            NetModuleMgr.GetInstance().Shutdown();

            if (msgHandleDic != null)
            {
                MonoBehaviour.print("clear msgHandleDic");
                msgHandleDic.Clear();
                msgHandleDic = null;
            }
        }
Ejemplo n.º 4
0
        // 初始化
        public bool Init(ref NetModuleInit netModuleInit)
        {
            msgHandleDic = new Dictionary <ushort, HandleMsg>();
            if (msgHandleDic == null)
            {
                return(false);
            }

            // 初始化网络底层
            if (!NetModuleMgr.GetInstance().Init(ref netModuleInit, this.HandleNetMessage))
            {
                return(false);
            }

            return(true);
        }
Ejemplo n.º 5
0
 // 关闭网络会话
 public void CloseNetSession(Int32 netSessionID)
 {
     NetModuleMgr.GetInstance().CloseNetSession(netSessionID);
 }
Ejemplo n.º 6
0
 // 连接远程地址
 // 同步连接时返回连接成功的网络会话
 // isSuccess 返回函数是否操作成功
 public INetSession Connect(string remoteAddr, Int32 remotePort, bool isSync, ref bool isSuccess)
 {
     return(NetModuleMgr.GetInstance().Connect(remoteAddr, remotePort, isSync, ref isSuccess));
 }
Ejemplo n.º 7
0
 // 刷新操作
 public void Update()
 {
     // 刷新网络底层
     NetModuleMgr.GetInstance().Update();
 }
Ejemplo n.º 8
0
        // 连接远程地址
        public bool PostConnect(string remoteIP, Int32 remotePort, bool isSync)
        {
            netSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
            if (netSocket == null)
            {
                return(false);
            }

            SetRemoteAddress(remoteIP, remotePort);
            IPAddress remoteAddr = IPAddress.Parse(remoteIP);

            if (isSync)
            {
                // 同步连接
                try
                {
                    netSocket.Connect(remoteAddr, remotePort);
                }
                catch (SocketException excetption)
                {
                    Trace.Assert(false, ("netSocket.Connect error" + excetption.ToString()));
                    return(false);
                }
            }
            else
            {
                // 异步连接
                SocketAsyncEventArgs connectAsyncEvent = new SocketAsyncEventArgs();
                if (connectAsyncEvent == null)
                {
                    return(false);
                }

                connectAsyncEvent.Completed     += new EventHandler <SocketAsyncEventArgs>(NetModuleMgr.GetInstance().NetIOComplete);
                connectAsyncEvent.UserToken      = this;
                connectAsyncEvent.RemoteEndPoint = new IPEndPoint(remoteAddr, remotePort);

                netSocket.ConnectAsync(connectAsyncEvent);
            }

            return(true);
        }
Ejemplo n.º 9
0
        public bool Init()
        {
            usingStateLock = new object();
            if (usingStateLock == null)
            {
                return(false);
            }

            rcvAsyncEventArgs = new SocketAsyncEventArgs();
            if (rcvAsyncEventArgs == null)
            {
                return(false);
            }
            else
            {
                rcvAsyncEventArgs.Completed += new EventHandler <SocketAsyncEventArgs>(NetModuleMgr.GetInstance().NetIOComplete);
                rcvAsyncEventArgs.UserToken  = this;
            }

            sendAsyncEventArgs = new SocketAsyncEventArgs();
            if (sendAsyncEventArgs == null)
            {
                return(false);
            }
            else
            {
                sendAsyncEventArgs.Completed += new EventHandler <SocketAsyncEventArgs>(NetModuleMgr.GetInstance().NetIOComplete);
                sendAsyncEventArgs.UserToken  = this;
            }

            rcvDataBuffer = DataBufferPool.GetInstance().MallocRcvBuffer();
            if (rcvDataBuffer == null)
            {
                return(false);
            }

            sendDataBufferMgr = DataBufferPool.GetInstance().MallocSendBufferMgr();
            if (sendDataBufferMgr == null)
            {
                DataBufferPool.GetInstance().FreeRcvBuffer(rcvDataBuffer);
                rcvDataBuffer = null;
                return(false);
            }

            return(true);
        }