ConnectionState.cs 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. /* Copyright (C) 2014-2017 Tal Aloni <tal.aloni.il@gmail.com>. All rights reserved.
  2. *
  3. * You can redistribute this program and/or modify it under the terms of
  4. * the GNU Lesser Public License as published by the Free Software Foundation,
  5. * either version 3 of the License, or (at your option) any later version.
  6. */
  7. using System;
  8. using System.Collections.Generic;
  9. using System.Net;
  10. using System.Net.Sockets;
  11. using SMBLibrary.NetBios;
  12. using Utilities;
  13. namespace SMBLibrary.Server
  14. {
  15. public delegate void LogDelegate(Severity severity, string message);
  16. public class ConnectionState
  17. {
  18. public Socket ClientSocket;
  19. public IPEndPoint ClientEndPoint;
  20. public NBTConnectionReceiveBuffer ReceiveBuffer;
  21. protected LogDelegate LogToServerHandler;
  22. public SMBDialect ServerDialect;
  23. public object AuthenticationContext;
  24. public ConnectionState(LogDelegate logToServerHandler)
  25. {
  26. ReceiveBuffer = new NBTConnectionReceiveBuffer();
  27. LogToServerHandler = logToServerHandler;
  28. ServerDialect = SMBDialect.NotSet;
  29. }
  30. public ConnectionState(ConnectionState state)
  31. {
  32. ClientSocket = state.ClientSocket;
  33. ClientEndPoint = state.ClientEndPoint;
  34. ReceiveBuffer = state.ReceiveBuffer;
  35. LogToServerHandler = state.LogToServerHandler;
  36. ServerDialect = state.ServerDialect;
  37. }
  38. public void LogToServer(Severity severity, string message)
  39. {
  40. message = String.Format("[{0}] {1}", ConnectionIdentifier, message);
  41. if (LogToServerHandler != null)
  42. {
  43. LogToServerHandler(severity, message);
  44. }
  45. }
  46. public void LogToServer(Severity severity, string message, params object[] args)
  47. {
  48. LogToServer(severity, String.Format(message, args));
  49. }
  50. public string ConnectionIdentifier
  51. {
  52. get
  53. {
  54. if (ClientEndPoint != null)
  55. {
  56. return ClientEndPoint.Address + ":" + ClientEndPoint.Port;
  57. }
  58. return String.Empty;
  59. }
  60. }
  61. }
  62. }