Esempio n. 1
0
        /// <summary>
        /// 发送消息
        /// 在游戏线程调用
        /// </summary>
        /// <param name="msgId"></param>
        /// <param name="msg"></param>
        /// <returns></returns>
        public bool Send(uint msgId, IMessage msg)
        {
            if (Disposed)
            {
                mLogger.LogError(this + "Been dispose", "Send");

                /////////////////////////////////////////////////////
                // 접속끊김
                UIMgr.instance.AddPopup(141, 109, 117, 0, 0, () =>
                {
                    SceneManager.instance.ShowLoadingTipPanel(true, GAME_MODE.NONE, () =>
                    {
                        UIMgr.ClearUI(true);
                        UITextList.ClearTextList();
                        //UIMgr.GetTownBasePanel().Close();
                        //UIMgr.instance.Clear(UIMgr.UIType.System);

                        //NetworkClient.instance.DisconnectGameServer();//연결 종료
                        NetData.instance.InitUserData();
                        NetData.instance.ClearCharIdc();

                        SceneManager.instance.ActionEvent(_ACTION.GO_LOGIN);
                    });
                });
                /////////////////////////////////////////////////////

                return(false);
            }

            if (msg == null)
            {
                mLogger.LogError("msg == null", "Send");
                return(false);
            }

            Socket socket = mSocket;

            if (socket == null)
            {
                mLogger.LogError("socket == null", "Send");
                return(false);
            }

            try
            {
                if (!socket.Connected)
                {
                    mLogger.LogError("socket.Connected == false", "Send");
                    return(false);
                }

                ByteArray writer = mWriter;
                if (writer == null)
                {
                    mWriterBuffer = new byte[mBuffSize];
                    mWriter       = new ByteArray(mWriterBuffer);
                    writer        = mWriter;
                }
                else
                {
                    writer.Reset();
                }

                UInt16 uMsgId = (UInt16)msgId;
                //先占位
                writer.WriteU16(0);
                writer.WriteU16(uMsgId);
                var ms = writer.GetStream();
                msg.WriteTo(ms);
                var len = writer.Position;

                //重写长度
                writer.Position = 0;
                writer.WriteU16((ushort)len);
                writer.Position = len;
                writer.SetLength(len);

                if (SendBeforCallback != null)
                {
                    SendBeforCallback(uMsgId, writer);
                }

                int sendSize = socket.Send(mWriterBuffer, (int)len, SocketFlags.None);

                if (MsgTransferLog)
                {
                    mLogger.LogInfo("send msg, msgId=" + msgId, "Send");
                }

                if (sendSize != writer.Length)
                {
                    mLogger.LogError("sendSize != mWriter.Length", "Send");
                    return(false);
                }
            }
            catch (Exception e)
            {
                mLogger.LogError(e.Message, "Send");
                return(false);
            }

            return(true);
        }
Esempio n. 2
0
        protected override ThreadResult OnUpdate(ThreadProxy thread)
        {
            if (mState == NetState.Close)
            {
                return(ThreadResult.Stop);
            }

            if (mState != NetState.Connected)
            {
                return(ThreadResult.Sleep);
            }

            Socket socket = mSocket;

            if (socket == null)
            {
                mLogger.LogError("socket == null", "OnUpdate");
                return(ThreadResult.Stop);
            }

            if (!socket.Connected)
            {
                mLogger.LogError("socket.Connected = false", "OnUpdate");
                return(ThreadResult.Stop);
            }

            try
            {
                //利用socket本身的buff包,先收头,再收尾,满一个包,再从头开始,如此循环
                //if (MsgTransferLog)
                //    mLogger.LogInfo("Start recv data, position =" + mPosition + ", NeedSize =" + mNeedSize, "OnUpdate");

                int allLen = mPosition + mNeedSize;
                if (allLen > mBuffSize)
                {
                    throw new Exception("allLen > mBuffSize,need = " + allLen + ", current = " + mBuffSize);
                }

                int recvLen = socket.Receive(mBuffer, mPosition, mNeedSize, SocketFlags.None);
                if (recvLen < 1)
                {
                    mLogger.LogInfo(
                        "recvLen =" + recvLen +
                        ", socket.Connected = " + socket.Connected +
                        ", May be the server is close", "OnUpdate");

                    CloseSocket(socket);
                    return(ThreadResult.ErrorStop);
                }

                mPosition += recvLen;
                if (recvLen < mNeedSize)
                {
                    mNeedSize -= recvLen;
                    mLogger.LogInfo("mPosition < mNeedSize, recv = " + recvLen + ",  need = " + mNeedSize, "OnUpdate");
                    //下一次循环再收
                    return(ThreadResult.Just);
                }
            }
            catch (SocketException e)
            {
                mLogger.LogError("recv data error: " + e.Message, "OnUpdate");

                if ((SocketError)e.ErrorCode == SocketError.ConnectionAborted)
                {
                    //서버에서 끊어진건데 클라이언트가 끊을때도 나옴

                    CloseSocket(socket);
                    return(ThreadResult.ErrorStop);
                }

                mLogger.LogError("Socket Disconnect - " + String.Format("{0}({1})", e.ErrorCode, ((SocketError)e.ErrorCode).ToString()));

                CloseSocket(socket);
                return(ThreadResult.Stop);
            }

            //消息定义
            if (mIsHead)
            {
                Int16 msglen = 0;
                mReader.ReadI16(ref msglen);
                if (msglen < HEAD_SIZE)
                {
                    mLogger.LogError("msglen < HEAD_SIZE", "OnUpdate");
                    return(ThreadResult.Stop);
                }

                mReader.SetLength(msglen);

                //剩下读消息主体
                mNeedSize = msglen - HEAD_SIZE;
                if (mNeedSize == 0)
                {
                    //只有消息头的资源
                    ProcessMsg(mReader, mPosition);
                    mIsHead = true;
                }
                else
                {
                    //因为还没有读完
                    mIsHead = false;
                }
            }
            else
            {
                ProcessMsg(mReader, mPosition);
                //消息主体读完
                mIsHead = true;
            }

            //说明已经读完了
            //有时一个消息就只有包头,内容长度为0
            if (mIsHead)
            {
                mPosition        = 0;
                mReader.Position = 0;
                mNeedSize        = HEAD_SIZE;
            }

            return(ThreadResult.Just);
        }