Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support for server behaviors #114

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions packages/repositories.config
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?xml version="1.0" encoding="utf-8"?>
<repositories>
<repository path="../test/Integration/Alchemy/packages.config" />
<repository path="../test/Unit/Alchemy/packages.config" />
<repository path="..\test\Integration\Alchemy\packages.config" />
<repository path="..\test\Unit\Alchemy\packages.config" />
</repositories>
3 changes: 2 additions & 1 deletion src/Alchemy/Alchemy.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@
<Compile Include="Classes\Header.cs" />
<Compile Include="Classes\Response.cs" />
<Compile Include="Classes\UserContext.cs" />
<Compile Include="Classes\ServerBehaviour.cs" />
<Compile Include="Handlers\Handler.cs" />
<Compile Include="Handlers\WebSocket\hybi00\Handler.cs" />
<Compile Include="Handlers\WebSocket\hybi00\Handshakes.cs" />
Expand All @@ -80,7 +81,7 @@
<Compile Include="Handlers\WebSocket\rfc6455\Handshakes.cs" />
<Compile Include="Handlers\WebSocket\rfc6455\Handler.cs" />
<Compile Include="Handlers\WebSocket\WebSocketHandler.cs" />
<Compile Include="TcpServer.cs" />
<Compile Include="TCPServer.cs" />
<Compile Include="WebSocketClient.cs" />
<Compile Include="WebSocketServer.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
Expand Down
36 changes: 36 additions & 0 deletions src/Alchemy/Classes/ServerBehaviour.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
namespace Alchemy.Classes
{
/// <summary>
/// The server behaviour base class.
/// </summary>
public abstract class ServerBehaviour
{
/// <summary>
/// The event triggered when the user is connected.
/// </summary>
/// <param name="context">The current user context.</param>
public virtual void OnConnected(UserContext context)
{ }

/// <summary>
/// The event triggered when the server receive data.
/// </summary>
/// <param name="context">The current user context.</param>
public virtual void OnReceive(UserContext context)
{ }

/// <summary>
/// The event triggered when the server send data.
/// </summary>
/// <param name="context">The current user context.</param>
public virtual void OnSend(UserContext context)
{ }

/// <summary>
/// The event triggered when the user is disconnected.
/// </summary>
/// <param name="context">The current user context.</param>
public virtual void OnDisconnect(UserContext context)
{ }
}
}
28 changes: 16 additions & 12 deletions src/Alchemy/WebSocketClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,8 @@ static WebSocketClient()
NewClients = new Queue<Context>();
ContextMapping = new Dictionary<Context, WebSocketClient>();

for(int i = 0; i < ClientThreads.Length; i++){
for (int i = 0; i < ClientThreads.Length; i++)
{
ClientThreads[i] = new Thread(HandleClientThread);
ClientThreads[i].Start();
}
Expand Down Expand Up @@ -97,9 +98,12 @@ private static void HandleClientThread()
}
public WebSocketClient(string path)
{
var r = new Regex("^(wss?)://(.*)\\:([0-9]*)/(.*)$");
var r = new Regex("^(\\w+)://(.*)\\:([0-9]*)/(.*)$");
var matches = r.Match(path);

if (matches.Groups[1].Value != "ws" || matches.Groups[1].Value != "wss")
SubProtocols = new string[] {matches.Groups[1].Value};

_host = matches.Groups[2].Value;
_port = Int32.Parse(matches.Groups[3].Value);
_path = matches.Groups[4].Value;
Expand All @@ -108,7 +112,7 @@ public WebSocketClient(string path)
public void Connect()
{
if (_client != null) return;

try
{
ReadyState = ReadyStates.CONNECTING;
Expand Down Expand Up @@ -142,7 +146,7 @@ protected void OnRunClient(IAsyncResult result)
{
_client.EndConnect(result);
}
catch (Exception ex)
catch (Exception)
{
Disconnect();
connectError = true;
Expand Down Expand Up @@ -192,7 +196,7 @@ private void SetupContext(Context context)
{
ReceiveEventArgs_Completed(_context.Connection.Client, _context.ReceiveEventArgs);
}


if (!IsAuthenticated)
{
Expand Down Expand Up @@ -236,7 +240,7 @@ void ReceiveEventArgs_Completed(object sender, SocketAsyncEventArgs e)

private void Authenticate()
{
_handshake = new ClientHandshake { Version = "8", Origin = Origin, Host = _host, Key = GenerateKey(), ResourcePath = _path, SubProtocols = SubProtocols};
_handshake = new ClientHandshake { Version = "8", Origin = Origin, Host = _host, Key = GenerateKey(), ResourcePath = _path, SubProtocols = SubProtocols };

_client.Client.Send(Encoding.UTF8.GetBytes(_handshake.ToString()));
}
Expand Down Expand Up @@ -264,7 +268,7 @@ private bool CheckAuthenticationResponse(Context context)
}

}
if(String.IsNullOrEmpty(CurrentProtocol))
if (String.IsNullOrEmpty(CurrentProtocol))
{
return false;
}
Expand Down Expand Up @@ -305,7 +309,7 @@ private void ReceiveData(Context context)

private void DoReceive(IAsyncResult result)
{
var context = (Context) result.AsyncState;
var context = (Context)result.AsyncState;
context.Reset();

try
Expand Down Expand Up @@ -335,7 +339,7 @@ private static String GenerateKey()

for (var index = 0; index < bytes.Length; index++)
{
bytes[index] = (byte) random.Next(0, 255);
bytes[index] = (byte)random.Next(0, 255);
}

return Convert.ToBase64String(bytes);
Expand Down Expand Up @@ -369,12 +373,12 @@ public void Send(byte[] data)
{
_context.UserContext.Send(data);
}

public void Dispose()
{
cancellation.Cancel();
Handler.Instance.Dispose();
}

}
}
}
Loading