Exemplo n.º 1
0
        /// <summary>
        /// 发送信息
        /// </summary>
        /// <param name="uid">要发送的用户的uid</param>
        /// <param name="msg">消息体</param>
        public void Send(string uid, string msg)
        {
            if (uid == string.Empty || uid == "" || msg == string.Empty || msg == "")
            {
                return;
            }
            SocketAsyncEventArgsWithId socketWithId = readWritePool.FindByUID(uid);

            if (socketWithId == null)
            {
                //说明用户已经断开
                //100   发送成功
                //200   发送失败
                //300   用户不在线
                //其它  表示异常的信息
                OnSended(uid, "300");
            }
            else
            {
                MySocketAsyncEventArgs e = socketWithId.SendSAEA;
                if (e.SocketError == SocketError.Success)
                {
                    int i = 0;
                    try
                    {
                        string message    = @"[lenght=" + msg.Length + @"]" + msg;
                        byte[] sendbuffer = Encoding.Unicode.GetBytes(message);
                        e.SetBuffer(sendbuffer, 0, sendbuffer.Length);
                        Boolean 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(uid, msg);
                        }
                        else
                        {
                            OnSended(uid, ex.ToString());
                        }
                    }
                }
                else
                {
                    OnSended(uid, "200");
                    this.CloseClientSocket(((MySocketAsyncEventArgs)e).UID);
                }
            }
        }
Exemplo n.º 2
0
        /// <summary>
        /// 发送信息,发送失败,重试五次
        /// </summary>
        /// <param name="uid">要发送的用户的uid</param>
        /// <param name="msg">消息体</param>
        public void Send(string uid, byte[] msg)
        {
            SocketAsyncEventArgsWithId socketWithId = readWritePool.FindByUID(uid);

            if (socketWithId == null)
            {
                //说明用户已经断开
                //100   发送成功
                //200   发送失败
                //300   用户不在线
                //其它  表示异常的信息
                if (OnSended != null)
                {
                    OnSended(uid, "300");
                }
                Console.WriteLine("{0:M} {1:t}:{2}", DateTime.Now, DateTime.Now, "用户不在线");
            }
            else
            {
                MySocketAsyncEventArgs e = socketWithId.SendSAEA;
                if (e.SocketError == SocketError.Success)
                {
                    int i = 0;
                    try
                    {
                        byte[]      arrayLength = BitConverter.GetBytes(msg.Length);
                        List <byte> listByte    = new List <byte>(arrayLength.Length + msg.Length);
                        listByte.AddRange(arrayLength);
                        listByte.AddRange(msg);
                        byte[] sendBuffer = listByte.ToArray();
                        e.SetBuffer(sendBuffer, 0, sendBuffer.Length);
                        Boolean 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(uid, msg);
                        }
                        else
                        {
                            if (OnSended != null)
                            {
                                OnSended(uid, ex.ToString());
                            }
                        }
                    }
                }
                else
                {
                    if (OnSended != null)
                    {
                        OnSended(uid, "200");
                    }
                    this.CloseClientSocket(((MySocketAsyncEventArgs)e).UID);
                }
            }
        }