/// <summary>
 /// This disposes the object.
 /// It cannot be used again after this call.
 /// </summary>
 /// <param name="disposing"></param>
 protected virtual void Dispose(bool disposing)
 {
     if (!disposed)
     {
         if (disposing)
         {
             // Dispose managed resources.
             try
             {
                 if (writeStream != null)
                 {
                     writeStream.Dispose();
                 }
             }
             catch (Exception e)
             {
                 Logger.Error("Failed to dispose of stream", e);
             }
             try
             {
                 if (readStream != null)
                 {
                     readStream.Dispose();
                 }
             }
             catch (Exception e)
             {
                 Logger.Error("Failed to dispose of stream", e);
             }
         }
         // There are no unmanaged resources to release, but
         // if we add them, they need to be released here.
     }
     disposed = true;
 }
Exemple #2
0
 /// <summary>
 /// This disposes the object.
 /// It cannot be used again after this call.
 /// </summary>
 /// <param name="disposing"></param>
 protected virtual void Dispose(bool disposing)
 {
     if (!disposed)
     {
         if (disposing)
         {
             // Dispose managed resources.
             if (writeStream != null)
             {
                 writeStream.Dispose();
             }
         }
         // There are no unmanaged resources to release, but
         // if we add them, they need to be released here.
     }
     disposed = true;
 }
Exemple #3
0
 public void Dispose()
 {
     _writeStream.Dispose();
     _provider.Dispose();
     _offsetWriteStream.Dispose();
 }
        private bool StartWrite(SocketService socketService, byte[] buffer, int offset, int count, IWriteStream writeStream,
                                bool isRecycle = false)
        {
            if (socketService == null)
            {
                Logger.Error($"{nameof(socketService)} 为 null !");
                return(false);
            }

            if (socketService.Connection.RemoteAddress == null)
            {
                Logger.Error($"远程地址不能为null!");
                return(false);
            }

            lock (_senderLock) {
                try {
                    _readSocket.BeginSendTo(buffer, offset, count, SocketFlags.None,
                                            socketService.Connection.RemoteAddress, result => {
                        var bytesSended = 0;
                        try {
                            lock (_senderLock) {
                                bytesSended = _readSocket.EndSendTo(result);
                            }
                        } catch (SocketException socketException) {
                            socketService.OnError(socketException.ToString());
                            OnDisconnect(socketService);
                        } catch (ObjectDisposedException objectDisposedException) {
                            socketService.OnError(objectDisposedException.ToString());
                            OnDisconnect(socketService);
                        } catch (InvalidOperationException invalidOperationException) {
                            socketService.OnError(invalidOperationException.ToString());
                            OnDisconnect(socketService);
                        }

                        if (bytesSended != count)
                        {
                            Logger.Warn("未完全发送完,自动丢弃!");
                        }
                        else
                        {
                            Logger.Debug($"发送{socketService.Connection.RemoteAddress}:{count}字节数据!");
                            socketService.OnWrite(true);
                        }

                        if (isRecycle)
                        {
                            writeStream.Dispose();
                        }
                    }, null);
                } catch (SocketException socketException) {
                    Logger.Error(socketException.ToString());
                    socketService.OnError(socketException.ToString());
                    OnDisconnect(socketService);
                } catch (ObjectDisposedException objectDisposedException) {
                    socketService.OnError(objectDisposedException.ToString());
                    OnDisconnect(socketService);
                } catch (InvalidOperationException invalidOperationException) {
                    socketService.OnError(invalidOperationException.ToString());
                    OnDisconnect(socketService);
                }
            }
            return(true);
        }