Esempio n. 1
0
        public void Send(MySocketAsyncEventArgs e, string uid, string msg)
        {
            int i = 0;

            try
            {
                string _msg        = string.Format(msg, msg.Length, msg);
                byte[] _sendBuffer = Encoding.Unicode.GetBytes(_msg);
                e.SetBuffer(_sendBuffer, 0, _sendBuffer.Length);
                bool _willRaiseEvent = (e.UserToken as Socket).SendAsync(e);
                if (!_willRaiseEvent)
                {
                    this.ProcessSend(e);
                }
            }
            catch (Exception ex)
            {
                if (i <= 5)
                {
                    i++;
                    //如果发送出现异常就延迟0.01秒再发
                    Thread.Sleep(10);
                    Send(e, uid, msg);
                }
                else
                {
                    Send(e, uid, ex.ToString());
                }
            }
        }
Esempio n. 2
0
        /// <summary>
        /// send message
        /// </summary>
        /// <param name="uid"></param>
        /// <param name="msg"></param>
        public void Send(string uid, string msg)
        {
            if (string.IsNullOrEmpty(uid) || string.IsNullOrEmpty(msg))
            {
                return;
            }
            DuplexSocketAsyncEventArgsWithId _socket = m_readWritePool.FindByUID(uid);

            //说明用户已经断开
            //100   发送成功
            //200   发送失败
            //300   用户不在线
            //其它  表示异常的信息
            if (_socket == null)
            {
                OnSended(uid, MessageType.UseOffline.ToString());
            }
            else
            {
                string _msgTmpl           = @"[length={0}]{1}";
                MySocketAsyncEventArgs _e = _socket.SendSAEA;
                if (_e.SocketError == SocketError.Success)
                {
                    int _i = 0;
                    try
                    {
                        string _msg        = string.Format(_msgTmpl, msg.Length, msg);
                        byte[] _sendBuffer = Encoding.Unicode.GetBytes(_msg);
                        _e.SetBuffer(_sendBuffer, 0, _sendBuffer.Length);
                        bool _willRaiseEvent = (_e.UserToken as Socket).SendAsync(_e);
                        if (!_willRaiseEvent)
                        {
                            this.ProcessSend(_e);
                        }
                    }
                    catch (Exception ee)
                    {
                        if (_i <= 5)
                        {
                            _i++;
                            //如果发送出现异常就延迟0.01秒再发
                            Thread.Sleep(10);
                            Send(uid, msg);
                        }
                        else
                        {
                            OnSended(uid, ee.ToString());
                        }
                    }
                }
                else
                {
                    OnSended(uid, MessageType.Failed.ToString());
                    this.CloseClientSocket(_e.UID, false);
                }
            }
        }
Esempio n. 3
0
 /// <summary>
 /// 发送信息
 /// </summary>
 /// <param name="message"></param>
 public void SendMessage(string message)
 {
     if (this.connected)
     {
         message = string.Format("[length={0}]{1}", message.Length, message);
         byte[] sendBuffer = Encoding.Unicode.GetBytes(message);
         MySocketAsyncEventArgs senderSocketAsyncEventArgs = _readWirted.SendSAEA;
         senderSocketAsyncEventArgs.SetBuffer(sendBuffer, 0, sendBuffer.Length);
         senderSocketAsyncEventArgs.RemoteEndPoint = this.hostEndPoint;
         clientSocket.SendAsync(senderSocketAsyncEventArgs);
     }
     else
     {
         throw new SocketException((int)SocketError.NotConnected);
     }
 }