Skip to content

Commit

Permalink
Refactor ZipSocket
Browse files Browse the repository at this point in the history
  • Loading branch information
soshimozi committed Oct 21, 2011
1 parent 3d2948b commit f4366a5
Show file tree
Hide file tree
Showing 10 changed files with 82 additions and 75 deletions.
2 changes: 1 addition & 1 deletion SocketService.Client.API/Server.cs
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ public void Connect(string address, int port)
}

// wrap the socket
_socket = new ZipSocket(rawSocket, Guid.NewGuid());
_socket = new ZipSocket(rawSocket);
NegotiateKeys();

OnConnectionResponse(new ConnectionEventArgs { IsSuccessful = true });
Expand Down
49 changes: 22 additions & 27 deletions SocketService.Framework.Client/Sockets/ZipSocket.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text;
using System.Net.Sockets;
using System.Net;
using System.IO;
Expand All @@ -12,15 +9,13 @@ namespace SocketService.Framework.Client.Sockets
{
public class ZipSocket
{
private Mutex _sendMutex = new Mutex();
private readonly Mutex _sendMutex = new Mutex();
/// <summary>
/// Initializes a new instance of the <see cref="ZipSocket"/> class.
/// </summary>
/// <param name="socket">The socket.</param>
/// <param name="clientId">The client id.</param>
public ZipSocket(Socket socket, Guid clientId)
public ZipSocket(Socket socket)
{
ClientId = clientId;
RawSocket = socket;
RemoteAddress = ((IPEndPoint)socket.RemoteEndPoint).Address.ToString();
}
Expand Down Expand Up @@ -83,14 +78,14 @@ public bool IsEqual(Socket socket)
return socket == RawSocket;
}

/// <summary>
/// Gets the client id.
/// </summary>
public Guid ClientId
{
get;
private set;
}
///// <summary>
///// Gets the client id.
///// </summary>
//public Guid ClientId
//{
// get;
// private set;
//}

/// <summary>
/// Gets the remote address.
Expand All @@ -107,7 +102,7 @@ public string RemoteAddress
/// <returns></returns>
public byte[] ReceiveData()
{
byte[] zippedData = new byte[RawSocket.Available];
var zippedData = new byte[RawSocket.Available];
RawSocket.Receive(zippedData);
return Decompress(zippedData);
}
Expand All @@ -117,15 +112,15 @@ public byte[] ReceiveData()
/// </summary>
/// <param name="data">The data.</param>
/// <returns></returns>
private byte[] Compress(byte[] data)
private static byte[] Compress(byte[] data)
{
using (MemoryStream msData = new MemoryStream())
using (var msData = new MemoryStream())
{
using (MemoryStream ms = new MemoryStream(data))
using (var ms = new MemoryStream(data))
{
using (GZipStream gz = new GZipStream(msData, CompressionMode.Compress))
using (var gz = new GZipStream(msData, CompressionMode.Compress))
{
byte[] bytes = new byte[4096];
var bytes = new byte[4096];
int n;
while ((n = ms.Read(bytes, 0, bytes.Length)) != 0)
{
Expand All @@ -144,15 +139,15 @@ private byte[] Compress(byte[] data)
/// </summary>
/// <param name="data">The data.</param>
/// <returns></returns>
private byte[] Decompress(byte[] data)
private static byte[] Decompress(byte[] data)
{
using (MemoryStream ms = new MemoryStream(data))
using (var ms = new MemoryStream(data))
{
using (GZipStream gs = new GZipStream(ms, CompressionMode.Decompress))
using (var gs = new GZipStream(ms, CompressionMode.Decompress))
{
using (MemoryStream msOut = new MemoryStream())
using (var msOut = new MemoryStream())
{
byte[] bytes = new byte[4096];
var bytes = new byte[4096];
int n;

while ((n = gs.Read(bytes, 0, bytes.Length)) != 0)
Expand Down
2 changes: 1 addition & 1 deletion SocketService/Command/GetCentralAuthorityCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public override void Execute()
{
var ca = new CentralAuthority(CAKeyProtocol.DH64);

Connection connection = ConnectionRepository.Instance.FindConnectionByClientId(_clientId);
ClientConnection connection = ConnectionRepository.Instance.FindConnectionByClientId(_clientId);
if (connection != null)
{
connection.Provider = ca.GetProvider();
Expand Down
2 changes: 1 addition & 1 deletion SocketService/Command/LogoutUserCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public LogoutUserCommand(Guid clientId)

public override void Execute()
{
Connection connection = ConnectionRepository.Instance.FindConnectionByClientId(_clientId);
ClientConnection connection = ConnectionRepository.Instance.FindConnectionByClientId(_clientId);
if (connection != null)
ConnectionRepository.Instance.RemoveConnection(connection);

Expand Down
2 changes: 1 addition & 1 deletion SocketService/Command/NegotiateKeysCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public NegotiateKeysCommand(Guid clientId, byte[] publicKey)

public override void Execute()
{
Connection connection = ConnectionRepository.Instance.FindConnectionByClientId(_clientId);
ClientConnection connection = ConnectionRepository.Instance.FindConnectionByClientId(_clientId);
if (connection != null)
{
//connection.RemotePublicKey = connection.Provider.Import(_data);
Expand Down
46 changes: 46 additions & 0 deletions SocketService/Net/Client/ClientConnection.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
using System;
using SocketService.Crypto;

namespace SocketService.Net.Client
{
public class ClientConnection : Connection
{
/// <summary>
/// Initializes a new instance of the <see cref="ClientConnection"/> class.
/// </summary>
/// <param name="clientId">The client id.</param>
public ClientConnection(Guid clientId) : base(clientId)
{
}

/// <summary>
/// Gets or sets the remote public key.
/// </summary>
/// <value>
/// The remote public key.
/// </value>
public DiffieHellmanKey RemotePublicKey { get; set; }

/// <summary>
/// Gets or sets the provider.
/// </summary>
/// <value>
/// The provider.
/// </value>
public DiffieHellmanProvider Provider { get; set; }

/// <summary>
/// Gets or sets the state of the connection.
/// </summary>
/// <value>
/// The state of the connection.
/// </value>
public ConnectionState ConnectionState { get; set; }
}

public enum ConnectionState
{
NegotiateKeyPair,
Connected
}
}
37 changes: 1 addition & 36 deletions SocketService/Net/Client/Connection.cs
Original file line number Diff line number Diff line change
@@ -1,19 +1,13 @@
using System;
using SocketService.Crypto;
using System;

namespace SocketService.Net.Client
{
public class Connection
{
/// <summary>
/// Initializes a new instance of the <see cref="Connection"/> class.
/// </summary>
/// <param name="clientId">The client id.</param>
public Connection(Guid clientId)
{
ClientId = clientId;
}

/// <summary>
/// Gets or sets the client id.
/// </summary>
Expand All @@ -22,34 +16,5 @@ public Connection(Guid clientId)
/// </value>
public Guid ClientId { get; set; }

/// <summary>
/// Gets or sets the remote public key.
/// </summary>
/// <value>
/// The remote public key.
/// </value>
public DiffieHellmanKey RemotePublicKey { get; set; }

/// <summary>
/// Gets or sets the provider.
/// </summary>
/// <value>
/// The provider.
/// </value>
public DiffieHellmanProvider Provider { get; set; }

/// <summary>
/// Gets or sets the state of the connection.
/// </summary>
/// <value>
/// The state of the connection.
/// </value>
public ConnectionState ConnectionState { get; set; }
}

public enum ConnectionState
{
NegotiateKeyPair,
Connected
}
}
12 changes: 6 additions & 6 deletions SocketService/Net/Client/ConnectionRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ namespace SocketService.Net.Client
public class ConnectionRepository
{
private static ConnectionRepository _instance;
private readonly List<Connection> _connectionList = new List<Connection>();
private readonly List<ClientConnection> _connectionList = new List<ClientConnection>();
private readonly Mutex _connectionMutex = new Mutex();

protected ConnectionRepository()
Expand All @@ -26,7 +26,7 @@ public static ConnectionRepository Instance
/// <summary>
/// Gets the connection list.
/// </summary>
public List<Connection> ConnectionList
public List<ClientConnection> ConnectionList
{
get
{
Expand All @@ -48,12 +48,12 @@ public List<Connection> ConnectionList
/// </summary>
/// <param name="clientId">The client id.</param>
/// <returns></returns>
public Connection FindConnectionByClientId(Guid clientId)
public ClientConnection FindConnectionByClientId(Guid clientId)
{
_connectionMutex.WaitOne();
try
{
IEnumerable<Connection> q = from c in _connectionList
IEnumerable<ClientConnection> q = from c in _connectionList
where c.ClientId == clientId
select c;

Expand All @@ -69,7 +69,7 @@ public Connection FindConnectionByClientId(Guid clientId)
/// Removes the connection.
/// </summary>
/// <param name="connection">The connection.</param>
public void RemoveConnection(Connection connection)
public void RemoveConnection(ClientConnection connection)
{
_connectionMutex.WaitOne();
try
Expand All @@ -86,7 +86,7 @@ public void RemoveConnection(Connection connection)
/// Adds the connection.
/// </summary>
/// <param name="connection">The connection.</param>
public void AddConnection(Connection connection)
public void AddConnection(ClientConnection connection)
{
_connectionMutex.WaitOne();
try
Expand Down
4 changes: 2 additions & 2 deletions SocketService/Net/SocketManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public SocketManager()

protected void SocketServerDataRecieved(object sender, DataRecievedArgs e)
{
Connection connection = ConnectionRepository.Instance.FindConnectionByClientId(e.ClientId);
ClientConnection connection = ConnectionRepository.Instance.FindConnectionByClientId(e.ClientId);
if (connection != null)
{
ParseRequest(connection.ClientId, e.Data);
Expand All @@ -40,7 +40,7 @@ protected void SocketServerClientConnecting(object sender, ConnectArgs e)
{
SocketRepository.Instance.AddSocket(e.ClientId, e.RawSocket);

var connection = new Connection(e.ClientId);
var connection = new ClientConnection(e.ClientId);
ConnectionRepository.Instance.AddConnection(connection);
}

Expand Down
1 change: 1 addition & 0 deletions SocketService/SocketService.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@
<Compile Include="Handler\PublicMessageRequestHandler.cs" />
<Compile Include="Handler\UpdateRoomVariableRequestHandler.cs" />
<Compile Include="Net\Client\ConnectArgs.cs" />
<Compile Include="Net\Client\ClientConnection.cs" />
<Compile Include="Net\Client\Connection.cs" />
<Compile Include="Net\Client\ConnectionRepository.cs" />
<Compile Include="Net\Client\DisconnectedArgs.cs" />
Expand Down

0 comments on commit f4366a5

Please sign in to comment.