private void ProcessSend()
            {
                if (m_SendPacketPool.Count <= 0)
                {
                    return;
                }

                if (!m_SendState.IsFree)
                {
                    return;
                }

                Packet packet = null;

                lock (m_SendPacketPool)
                {
                    packet = m_SendPacketPool.Dequeue();
                }

                byte[] packetBytes = null;
                try
                {
                    packetBytes = m_NetworkChannelHelper.Serialize(packet);
                }
                catch (Exception exception)
                {
                    m_Active = false;
                    if (NetworkChannelError != null)
                    {
                        NetworkChannelError(this, NetworkErrorCode.SerializeError, exception.ToString());
                        return;
                    }

                    throw;
                }

                if (packetBytes == null || packetBytes.Length <= 0)
                {
                    string errorMessage = "Serialized packet is invalid.";
                    if (NetworkChannelError != null)
                    {
                        NetworkChannelError(this, NetworkErrorCode.SerializeError, errorMessage);
                        return;
                    }

                    throw new GameFrameworkException(errorMessage);
                }

                m_SendState.SetPacket(packetBytes);
                Send();
            }
Esempio n. 2
0
            private void ProcessSend()
            {
                if (m_SendState.Stream.Length > 0 || m_SendPacketPool.Count <= 0)
                {
                    return;
                }

                while (m_SendPacketPool.Count > 0)
                {
                    Packet packet = null;
                    lock (m_SendPacketPool)
                    {
                        packet = m_SendPacketPool.Dequeue();
                    }

                    bool serializeResult = false;
                    try
                    {
                        serializeResult = m_NetworkChannelHelper.Serialize(packet, m_SendState.Stream);
                    }
                    catch (Exception exception)
                    {
                        m_Active = false;
                        if (NetworkChannelError != null)
                        {
                            SocketException socketException = exception as SocketException;
                            NetworkChannelError(this, NetworkErrorCode.SerializeError, socketException != null ? socketException.SocketErrorCode : SocketError.Success, exception.ToString());
                            return;
                        }

                        throw;
                    }

                    if (!serializeResult)
                    {
                        string errorMessage = "Serialized packet failure.";
                        if (NetworkChannelError != null)
                        {
                            NetworkChannelError(this, NetworkErrorCode.SerializeError, SocketError.Success, errorMessage);
                            return;
                        }

                        throw new GameFrameworkException(errorMessage);
                    }
                }

                m_SendState.Stream.Position = 0L;

                Send();
            }
            /// <summary>
            /// 向远程主机发送消息包。
            /// </summary>
            /// <typeparam name="T">消息包类型。</typeparam>
            /// <param name="packet">要发送的消息包。</param>
            /// <param name="userData">用户自定义数据。</param>
            public void Send<T>(T packet, object userData)
                where T : Packet
            {
                if (packet == null)
                {
                    string errorMessage = "Packet is invalid.";
                    if (NetworkChannelError != null)
                    {
                        NetworkChannelError(this, NetworkErrorCode.SendError, errorMessage);
                        return;
                    }

                    throw new GameFrameworkException(errorMessage);
                }

                byte[] buffer = null;
                try
                {
                    buffer = m_NetworkChannelHelper.Serialize(packet);
                }
                catch (Exception exception)
                {
                    m_Active = false;
                    if (NetworkChannelError != null)
                    {
                        NetworkChannelError(this, NetworkErrorCode.SerializeError, exception.ToString());
                        return;
                    }

                    throw;
                }

                if (buffer == null || buffer.Length <= 0)
                {
                    string errorMessage = "Serialized packet is invalid.";
                    if (NetworkChannelError != null)
                    {
                        NetworkChannelError(this, NetworkErrorCode.SerializeError, errorMessage);
                        return;
                    }

                    throw new GameFrameworkException(errorMessage);
                }

                Send(buffer, userData);
            }
            public void Send<T>(T packet, object userData) where T : Packet
            {
                if (packet == null)
                {
                    NetworkChannelError();
                    return;

                    throw;
                }

                byte[] buffer = null;
                try
                {
                    buffer = m_NetworkChannelHelper.Serialize(packet);
                }

                if (buffer == null)
                {

                }

                Send(buffer, userData);

            }