Пример #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);
        }
Пример #2
0
        /// <summary>
        /// 处理连接
        /// </summary>
        /// <param name="cmd"></param>
        /// <returns></returns>
        protected ThreadResult ConnectHandler(ActorCmd cmd)
        {
            if (mState == NetState.Connecting)
            {
                return(ThreadResult.Sleep);
            }

            if (mState != NetState.None)
            {
                mLogger.LogError("mState != NetState.None, mState = " + mState, "ConnectHandler");
                return(ThreadResult.Stop);
            }

            string ip   = cmd.Param0.ToString();
            int    port = (int)cmd.Param1;

            try
            {
                mState = NetState.Connecting;
                IPAddress serverIp;
                if (!IPAddress.TryParse(ip, out serverIp))
                {
                    IPAddress[] address = Dns.GetHostAddresses(ip);
                    if (address.Length < 1)
                    {
                        mLogger.LogError("ipHost.AddressList.Length < 1", "ConnectHandler");
                        return(ThreadResult.Stop);
                    }
                    serverIp = address[0];
                }

                mSocket = new Socket(serverIp.AddressFamily, SocketType.Stream, ProtocolType.Tcp);
                mSocket.Connect(serverIp, port);
                mLogger.LogInfo("Socket connect success", "ConnectHandler");
                if (mReader == null)
                {
                    mBuffer = new byte[mBuffSize];
                    mReader = new ByteArray(mBuffer);
                }
                else
                {
                    mReader.Reset();
                }

                //初始化读取数据的参数
                mNeedSize = HEAD_SIZE;
                mIsHead   = true;
                mPosition = 0;

                mState = NetState.Connected;
                SendToOutput(new ActorCmd(NetworkOutCmd.Connected));
            }
            catch (SocketException e)
            {
                mState = NetState.None;
                mLogger.LogError(e.Message, "ConnectHandler");
                SendToOutput(new ActorCmd(NetworkOutCmd.ConnectError));
                //CloseSocket(mSocket);
                //return ThreadResult.Stop;
            }

            return(ThreadResult.Sleep);
        }