Esempio n. 1
0
        /// <summary>
        /// 断开连接
        /// </summary>
        public override void DisConnect()
        {
            try
            {
                Connected = false;
                if (NetSocket == null)
                {
                    return;
                }
                OnDisConnect?.Invoke(this);
            }
            catch { }

            try
            {
                SendParser.Clear();
                RecvParser.Clear();
                NetSocket.Close();
                NetSocket.Dispose();
                NetSocket = null;

                this.inArgs.Dispose();
                this.outArgs.Dispose();
            }
            catch { }
        }
Esempio n. 2
0
        public override async Task SendAsync(Packet packet)
        {
            try
            {
                await sendSemaphore.WaitAsync();

                SendParser.WriteBuffer(packet);
                if (!netStream.CanWrite)
                {
                    return;
                }
                while (SendParser.Buffer.DataSize > 0)
                {
                    CancellationTokenSource cancel = new CancellationTokenSource(3000);
                    cancel.Token.Register(() => { if (Connected)
                                                  {
                                                      OnError?.Invoke(this, SocketError.SocketError);
                                                  }
                                          }, false);
                    await netStream.WriteAsync(SendParser.Buffer.First, SendParser.Buffer.FirstOffset, SendParser.Buffer.FirstCount, cancel.Token);

                    SendParser.Buffer.UpdateRead(SendParser.Buffer.FirstCount);
                }
            }
            catch
            {
                OnError?.Invoke(this, SocketError.SocketError);
            }
            finally
            {
                sendSemaphore.Release();
            }
        }
Esempio n. 3
0
 public override void Close()
 {
     DisConnect();
     rpcActions.Clear();
     SendParser.Clear();
     RecvParser.Clear();
     OnClose?.Invoke();
     OnClose   = null;
     OnError   = null;
     OnReceive = null;
     base.Close();
 }
Esempio n. 4
0
 /// <summary>
 /// 写入发送包到缓冲区队列(合并发送)
 /// </summary>
 /// <param name="packet"></param>
 public override void WriteSendBuffer(Packet packet)
 {
     SendParser.WriteBuffer(packet);
 }