Exemplo n.º 1
0
 ///
 /// 引发发送前事件
 ///
 /// 数据
 protected virtual void OnBeforeSendPacketServerEvent(BeforeSendPacketEventArgs e)
 {
     if (BeforeSendPacketServerEvent != null)
     {
         BeforeSendPacketServerEvent(this, e);
     }
 }
Exemplo n.º 2
0
        ///
        /// 发送一个流数据
        ///
        public bool Send(ClientContext client, byte[] txBytes, int offset, int count)
        {
            try
            {
                //lock (client)
                {
                    if (client == null)
                    {
                        return(false);
                    }
                    if (client.ClientSocket == null)
                    {
                        return(false);
                    }
                    client.txBytes = new byte[count];
                    Array.Copy(txBytes, offset, client.txBytes, 0, count); //复制数据
                    BeforeSendPacketEventArgs beforeSendPacketEventArgs = new BeforeSendPacketEventArgs(client, client.txBytes);
                    OnBeforeSendPacketServerEvent(beforeSendPacketEventArgs);
                    if (beforeSendPacketEventArgs.isCancel)
                    {
                        return(false);
                    }

                    client.SendEventArgs.SetBuffer(client.txBytes, 0, count);
                    bool willRaiseEvent = client.ClientSocket.SendAsync(client.SendEventArgs);
                    if (!willRaiseEvent)
                    {
                        return(ProcessSend(client.SendEventArgs));
                    }
                    else
                    {
                        return(true);
                    }
                }
            }
            catch (Exception e)
            {
                //Exception ex = new Exception("send:" + e.Message, e);
                OnErrorServerEvent(new ErrorServerEventArgs(e, client, TCPErrorType.Unkown, "send:"));
                //LastException = e;
            }
            return(false);
        }