Example #1
0
        private void HandleCustomMessage(NetBuffer buffer)
        {
            Debuger.Log(LOG_TAG_RECV, "HandleCustomMessage()");

            m_ListCustomCmd_InSend.Clear();

            buffer.Position = 0;
            buffer.Skip(4);
            List <SyncFrame> listFrame = FSPCodec.ReadFrameListFromBufferS2C(buffer);

            for (int i = 0; i < listFrame.Count; i++)
            {
                SyncFrame frame = listFrame[i];
                for (int j = 0; j < frame.cmdList.Count; j++)
                {
                    SyncCmd cmd = frame.cmdList[j];
                    HandleCustomCmd(cmd);
                }
            }

            m_CustomSendBuffer.Clear();
            m_CustomSendBuffer.WriteUShort(0);
            m_CustomSendBuffer.WriteUShort(0);
            m_CustomSendBuffer.WriteUShort(m_SessionId);
            FSPCodec.WriteCmdListToBufferC2S(m_ListCustomCmd_InSend, m_CustomSendBuffer);
            SendBuffer(m_CustomSendBuffer, false);
        }
Example #2
0
 public int Serialize(out byte[] tempBuffer)
 {
     lock (DefaultWriter)
     {
         DefaultWriter.Clear();
         this.Serialize(DefaultWriter);
         tempBuffer = DefaultWriter.GetBytes();
         return(DefaultWriter.Length);
     }
 }
Example #3
0
        private void ReceiveCallback(IAsyncResult ar)
        {
            try
            {
                // Retrieve the state object and the client socket
                // from the asynchronous state object.
                TCPState state  = (TCPState)ar.AsyncState;
                Socket   client = state.Socket;

                if (!IsConnected())
                {
                    Debug.Log("<color=red>不想接收 了</color>");
                    return;
                }

                // Read data from the remote device.
                int bytesRead = client.EndReceive(ar);

                //Debug.Log("get from server, length = " + bytesRead);
                if (bytesRead > 0)
                {
                    byte[] realData = new byte[bytesRead];
                    Array.Copy(state.Buffer, realData, bytesRead);

                    //Globals.SceneSingleton<SocketNetworkMng>().Handler(realData);

                    // 拼接数据包
                    if (m_buffer.TargetLength < 0)
                    {
                        m_buffer.Init(realData);

                        if (m_buffer.CheckComplete())
                        {
                            CmdRequest.Handle(this, m_buffer.Buffer.ToArray());
                            m_buffer.Clear();
                        }
                    }
                    else
                    {
                        if (m_buffer.CheckComplete())
                        {
                            CmdRequest.Handle(this, m_buffer.Buffer.ToArray());

                            m_buffer.Clear();
                        }
                        else
                        {
                            m_buffer.Buffer.AddRange(realData);
                        }
                    }
                    Receive();
                }
            }
            catch (SocketException e)
            {
                Debug.Log(e.ToString());
            }
            catch (Exception e)
            {
                Debug.LogError(e.ToString());
            }
        }