Exemplo n.º 1
0
 /// <summary>
 /// 发送信息
 /// </summary>
 /// <param name="message"></param>
 public void SendRequest(BaseRequest request)
 {
     if (this.connected)
     {
         request.Message = string.Format("[{0}]{1}", request.MessageType.ToString(), request.Message);
         byte[] sendBuffer = Encoding.Unicode.GetBytes(request.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);
     }
 }
Exemplo n.º 2
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);
     }
 }
Exemplo n.º 3
0
        /// <summary>
        /// 连接服务端
        /// </summary>
        public bool Connect()
        {
            MySocketAsyncEventArgs connectArgs = new MySocketAsyncEventArgs("Connect");

            connectArgs.UserToken      = new AsyncUserToken(clientSocket);
            connectArgs.RemoteEndPoint = this.hostEndPoint;
            connectArgs.Completed     += new EventHandler <SocketAsyncEventArgs>(OnConnect);
            clientSocket.ConnectAsync(connectArgs);
            //等待连接结果
            autoConnectEvent.WaitOne();
            SocketError errorCode = connectArgs.SocketError;

            if (errorCode == SocketError.Success)
            {
                this.m_bufferManager.InitBuffer();
                _readWirted = new DuplexSocketAsyncEventArgsWithId();
                //_readWirted.State = true;
                _readWirted.ReceiveSAEA.Completed += OnReceive;
                _readWirted.SendSAEA.Completed    += OnSend;

                _readWirted.ReceiveSAEA.UserToken = new AsyncUserToken(clientSocket);
                _readWirted.SendSAEA.UserToken    = new AsyncUserToken(clientSocket);

                this.m_bufferManager.SetBuffer(_readWirted.ReceiveSAEA);
                Listen();

                if (StartListenThread != null)
                {
                    StartListenThread();
                }
                return(true);
            }
            else
            {
                //throw new SocketException((Int32)errorCode);
                return(false);
            }
        }
Exemplo n.º 4
0
 //constructor
 internal DuplexSocketAsyncEventArgsWithId()
 {
     m_receivesaea = new MySocketAsyncEventArgs("Receive");
     m_sendsaea    = new MySocketAsyncEventArgs("Send");
 }
Exemplo n.º 5
0
        /// <summary>
        /// send message
        /// </summary>
        /// <param name="uid"></param>
        /// <param name="msg"></param>
        public void SendMessage(string msg, string uid = "", bool allUser = false)
        {
            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)
                {
                    SendMessage(msg, id);
                }
            }
            else
            {
                DuplexSocketAsyncEventArgsWithId _socket = m_readWritePool.FindByUID(uid);

                //说明用户已经断开
                //100   发送成功
                //200   发送失败
                //300   用户不在线
                //其它  表示异常的信息
                if (_socket == null)
                {
                    OnSended(uid, SocketMessageType.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 AsyncUserToken).Socket.SendAsync(_e);
                            if (!_willRaiseEvent)
                            {
                                this.ProcessSend(_e);
                            }
                        }
                        catch (Exception ee)
                        {
                            if (_i <= 5)
                            {
                                _i++;
                                //如果发送出现异常就延迟0.01秒再发
                                Thread.Sleep(10);
                                SendMessage(msg, uid);
                            }
                            else
                            {
                                OnSended(uid, ee.ToString());
                            }
                        }
                    }
                    else
                    {
                        OnSended(uid, SocketMessageType.Failed.ToString());
                        this.CloseClientSocket(_e.UID, false);
                    }
                }
            }
        }