ConnectionState.cs 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  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. internal delegate void LogDelegate(Severity severity, string message);
  16. internal class ConnectionState
  17. {
  18. public Socket ClientSocket;
  19. public IPEndPoint ClientEndPoint;
  20. public NBTConnectionReceiveBuffer ReceiveBuffer;
  21. public BlockingQueue<SessionPacket> SendQueue;
  22. protected LogDelegate LogToServerHandler;
  23. public SMBDialect Dialect;
  24. public object AuthenticationContext;
  25. public ConnectionState(LogDelegate logToServerHandler)
  26. {
  27. ReceiveBuffer = new NBTConnectionReceiveBuffer();
  28. SendQueue = new BlockingQueue<SessionPacket>();
  29. LogToServerHandler = logToServerHandler;
  30. Dialect = SMBDialect.NotSet;
  31. }
  32. public ConnectionState(ConnectionState state)
  33. {
  34. ClientSocket = state.ClientSocket;
  35. ClientEndPoint = state.ClientEndPoint;
  36. ReceiveBuffer = state.ReceiveBuffer;
  37. SendQueue = state.SendQueue;
  38. LogToServerHandler = state.LogToServerHandler;
  39. Dialect = state.Dialect;
  40. }
  41. public void LogToServer(Severity severity, string message)
  42. {
  43. message = String.Format("[{0}] {1}", ConnectionIdentifier, message);
  44. if (LogToServerHandler != null)
  45. {
  46. LogToServerHandler(severity, message);
  47. }
  48. }
  49. public void LogToServer(Severity severity, string message, params object[] args)
  50. {
  51. LogToServer(severity, String.Format(message, args));
  52. }
  53. public string ConnectionIdentifier
  54. {
  55. get
  56. {
  57. if (ClientEndPoint != null)
  58. {
  59. return ClientEndPoint.Address + ":" + ClientEndPoint.Port;
  60. }
  61. return String.Empty;
  62. }
  63. }
  64. }
  65. }