Exemplo n.º 1
0
        /// <summary>
        /// 连接服务端
        /// </summary>
        public bool Connect()
        {
            SocketAsyncEventArgsImpl connectArgs = new SocketAsyncEventArgsImpl(SocketAsyncType.Connect);

            connectArgs.UserToken      = 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 SocketAsyncEventArgsWithIdDuplex();

                _readWirted.ReceiveSAEA.Completed += OnReceive;
                _readWirted.SendSAEA.Completed    += OnSend;

                _readWirted.ReceiveSAEA.UserToken = clientSocket;
                _readWirted.SendSAEA.UserToken    = clientSocket;

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

                return(true);
            }
            else
            {
                return(false);
            }
        }
Exemplo n.º 2
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);
     }
 }
Exemplo n.º 3
0
 //constructor
 internal SocketAsyncEventArgsWithIdDuplex()
 {
     m_receivesaea = new SocketAsyncEventArgsImpl(SocketAsyncType.Receive);
     m_sendsaea    = new SocketAsyncEventArgsImpl(SocketAsyncType.Send);
 }
Exemplo n.º 4
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);
                    }
                }
            }
        }