コード例 #1
0
 /// <summary>
 /// 发送数据包
 /// </summary>
 /// <param name="sock"></param>
 /// <param name="data"></param>
 public void SendData(byte[] data)
 {
     if (this.connected)
     {
         SocketAsyncEventArgsImpl senderSocketAsyncEventArgs = _readWirted.SendSAEA;
         senderSocketAsyncEventArgs.SetBuffer(data, 0, data.Length);
         senderSocketAsyncEventArgs.RemoteEndPoint = this.hostEndPoint;
         clientSocket.SendAsync(senderSocketAsyncEventArgs);
     }
     else
     {
         throw new SocketException((int)SocketError.NotConnected);
     }
 }
コード例 #2
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="uid"></param>
        /// <param name="msg"></param>
        /// <param name="type"></param>
        public void SendMessageAsync(string uid, string msg, bool allUser = false, SocketMessageInfoType type = SocketMessageInfoType.NONE)
        {
            if ((!allUser && string.IsNullOrEmpty(uid)) || string.IsNullOrEmpty(msg))
            {
                return;
            }
            if (allUser)
            {
                if (this.m_readWritePool.m_busypool.Count == 0)
                {
                    LogOutEvent(null, SocketMessageType.Error, string.Format("No client online"));
                }

                foreach (string id in this.m_readWritePool.m_busypool.Keys)
                {
                    SendMessageAsync(id, msg, false, type);
                }
            }
            else
            {
                SocketAsyncEventArgsWithIdDuplex _socket = m_readWritePool.FindByUID(uid);
                if (_socket == null)
                {
                    OnSended(uid, SocketMessageType.UseOffline.ToString());
                }
                else
                {
                    string _msgTmpl             = @"[length={0}][MSG={1}]{2}";
                    SocketAsyncEventArgsImpl _e = _socket.SendSAEA;
                    if (_e.SocketError == SocketError.Success)
                    {
                        int _i = 0;
                        try
                        {
                            string _msg        = string.Format(_msgTmpl, msg.Length, type.ToString(), 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++;
                                Thread.Sleep(10);
                                SendMessageAsync(uid, msg, allUser, type);
                            }
                            else
                            {
                                OnSended(uid, ee.ToString());
                            }
                        }
                    }
                    else
                    {
                        OnSended(uid, SocketMessageType.Failed.ToString());
                        this.CloseClientSocket(_e.UID, false);
                    }
                }
            }
        }