Skip to content

Commit

Permalink
refactoring code to groom better
Browse files Browse the repository at this point in the history
  • Loading branch information
gaochundong committed Mar 30, 2016
1 parent cd7fa6b commit d9bc50d
Show file tree
Hide file tree
Showing 7 changed files with 564 additions and 437 deletions.
81 changes: 73 additions & 8 deletions Cowboy/Cowboy.Codec.WebSocket/AsyncWebSocketSession.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,23 +11,88 @@ public sealed class AsyncWebSocketSession : IAsyncTcpSocketServerMessageDispatch
{
public async Task OnSessionStarted(AsyncTcpSocketSession session)
{
//Console.WriteLine(string.Format("TCP session {0} has connected {1}.", session.RemoteEndPoint, session));
await Task.CompletedTask;
}

public async Task OnSessionDataReceived(AsyncTcpSocketSession session, byte[] data, int offset, int count)
{
var text = Encoding.UTF8.GetString(data, offset, count);
//Console.Write(string.Format("Client : {0} --> ", session.RemoteEndPoint));
//Console.WriteLine(text);
//var handshaker = OpenHandshake();
//if (!handshaker.Wait(session.ConnectTimeout))
//{
// throw new TimeoutException(string.Format(
// "Handshake with remote [{0}] timeout [{1}].", session.RemoteEndPoint, session.ConnectTimeout));
//}
//if (!handshaker.Result)
//{
// //var responseBuffer = WebSocketServerHandshaker.CreateOpenningHandshakeBadRequestResponse(this);
// //await _stream.WriteAsync(responseBuffer, 0, responseBuffer.Length);

await session.SendAsync(Encoding.UTF8.GetBytes(text));
// //throw new WebSocketException(string.Format(
// // "Handshake with remote [{0}] failed.", session.RemoteEndPoint));
//}
}

public async Task OnSessionClosed(AsyncTcpSocketSession session)
{
//Console.WriteLine(string.Format("TCP session {0} has disconnected.", session));
await Task.CompletedTask;
}

//private async Task<bool> OpenHandshake()
//{
// bool handshakeResult = false;

// try
// {
// int terminatorIndex = -1;
// while (!WebSocketHelpers.FindHeaderTerminator(_receiveBuffer, _receiveBufferOffset, out terminatorIndex))
// {
// int receiveCount = await _stream.ReadAsync(_receiveBuffer, _receiveBufferOffset, _receiveBuffer.Length - _receiveBufferOffset);
// if (receiveCount == 0)
// {
// throw new WebSocketHandshakeException(string.Format(
// "Handshake with remote [{0}] failed due to receive zero bytes.", RemoteEndPoint));
// }

// BufferDeflector.ReplaceBuffer(_bufferManager, ref _receiveBuffer, ref _receiveBufferOffset, receiveCount);

// if (_receiveBufferOffset > 2048)
// {
// throw new WebSocketHandshakeException(string.Format(
// "Handshake with remote [{0}] failed due to receive weird stream.", RemoteEndPoint));
// }
// }

// string secWebSocketKey = string.Empty;
// string path = string.Empty;
// string query = string.Empty;
// handshakeResult = WebSocketServerHandshaker.HandleOpenningHandshakeRequest(this,
// _receiveBuffer, 0, terminatorIndex + Consts.HeaderTerminator.Length,
// out secWebSocketKey, out path, out query);

// _module = _routeResolver.Resolve(path, query);
// if (_module == null)
// {
// throw new WebSocketHandshakeException(string.Format(
// "Handshake with remote [{0}] failed due to cannot identify the resource name [{1}{2}].", RemoteEndPoint, path, query));
// }

// if (handshakeResult)
// {
// var responseBuffer = WebSocketServerHandshaker.CreateOpenningHandshakeResponse(this, secWebSocketKey);
// await _stream.WriteAsync(responseBuffer, 0, responseBuffer.Length);
// }

// BufferDeflector.ShiftBuffer(_bufferManager, terminatorIndex + Consts.HeaderTerminator.Length, ref _receiveBuffer, ref _receiveBufferOffset);
// }
// catch (ArgumentOutOfRangeException)
// {
// handshakeResult = false;
// }
// catch (WebSocketHandshakeException ex)
// {
// _log.Error(string.Format("Session [{0}] exception occurred, [{1}].", this, ex.Message), ex);
// handshakeResult = false;
// }

// return handshakeResult;
//}
}
}
2 changes: 2 additions & 0 deletions Cowboy/Cowboy.Sockets/Tcp/Server/APM/TcpSocketSession.cs
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,8 @@ public TcpSocketSession(
public bool Active { get { return _tcpClient != null && _tcpClient.Connected; } }
public IPEndPoint RemoteEndPoint { get { return Active ? (IPEndPoint)_tcpClient.Client.RemoteEndPoint : _remoteEndPoint; } }
public IPEndPoint LocalEndPoint { get { return Active ? (IPEndPoint)_tcpClient.Client.LocalEndPoint : _localEndPoint; } }
public Socket Socket { get { return _tcpClient.Client; } }
public Stream Stream { get { return _stream; } }
public TcpSocketServer Server { get { return _server; } }
public TimeSpan ConnectTimeout { get { return _configuration.ConnectTimeout; } }

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ public TcpSocketSaeaSession(
public bool Connected { get { return _socket != null && _socket.Connected; } }
public IPEndPoint RemoteEndPoint { get { return Connected ? (IPEndPoint)_socket.RemoteEndPoint : _remoteEndPoint; } }
public IPEndPoint LocalEndPoint { get { return Connected ? (IPEndPoint)_socket.LocalEndPoint : _localEndPoint; } }
public Socket Socket { get { return _socket; } }
public TcpSocketSaeaServer Server { get { return _server; } }

public TcpSocketConnectionState State
Expand Down
2 changes: 2 additions & 0 deletions Cowboy/Cowboy.Sockets/Tcp/Server/TAP/AsyncTcpSocketSession.cs
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,8 @@ public IPEndPoint LocalEndPoint
(IPEndPoint)_tcpClient.Client.LocalEndPoint : _localEndPoint;
}
}
public Socket Socket { get { return _tcpClient.Client; } }
public Stream Stream { get { return _stream; } }
public AsyncTcpSocketServer Server { get { return _server; } }
public TimeSpan ConnectTimeout { get { return _configuration.ConnectTimeout; } }

Expand Down
Loading

0 comments on commit d9bc50d

Please sign in to comment.