private void Initialize() { AlreadyDisposed = false; logger = new Logger(); Status = ServerStatusLevel.Off; ConnectionsQueueLength = 500; MaxBufferSize = 1024 * 100; FirstByte = new byte[MaxBufferSize]; LastByte = new byte[MaxBufferSize]; FirstByte[0] = 0x00; LastByte[0] = 0xFF; logger.LogEvents = true; }
private void Initialize() { AlreadyDisposed = false; logger = new Logger(); Status = ServerStatusLevel.Off; ConnectionsQueueLength = 500; MaxBufferSize = 1024; receivedDataBuffer = new byte[MaxBufferSize]; FirstByte = new byte[MaxBufferSize]; LastByte = new byte[MaxBufferSize]; FirstByte[0] = 0x00; LastByte[0] = 0xFF; logger.LogEvents = true; Handshake = "HTTP/1.1 101 Web Socket Protocol Handshake" + Environment.NewLine; Handshake += "Upgrade: WebSocket" + Environment.NewLine; Handshake += "Connection: Upgrade" + Environment.NewLine; Handshake += "Sec-WebSocket-Origin: " + ConnectionOrigin + Environment.NewLine; Handshake += "Sec-WebSocket-Location: " + ServerLocation + Environment.NewLine; Handshake += Environment.NewLine; }
private void ClientDisconnected() { if (Status == ServerStatusLevel.WaitingConnection) return; logger.Log("Client disconnected."); if (Disconnected != null) Disconnected(EventArgs.Empty); Listener.BeginAccept(new AsyncCallback(NewClientConnection), null); Status = ServerStatusLevel.WaitingConnection; logger.Log("Waiting for another connection attempt ..."); logger.Log(""); }
private void HandshakeFinished(IAsyncResult status) { logger.Log("New connection from " + ConnectionSocket.LocalEndPoint + " established."); Status = ServerStatusLevel.ConnectionEstablished; ConnectionSocket.EndSend(status); ConnectionSocket.BeginReceive(receivedDataBuffer, 0, receivedDataBuffer.Length, 0, new AsyncCallback(Read), null); if (NewConnection != null) NewConnection(EventArgs.Empty); }
public void StartServer() { Listener = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.IP); Listener.Bind(new IPEndPoint(IPAddress.Loopback, ServerPort)); Listener.Listen(ConnectionsQueueLength); Listener.BeginAccept(new AsyncCallback(NewClientConnection), null); logger.Log("Press 'q' and enter to exit this program."); logger.Log("Server started. Listening for connection requests ..."); Status = ServerStatusLevel.WaitingConnection; string ConsoleInput = Console.ReadLine(); while (ConsoleInput != "Q" && ConsoleInput != "q") { ConsoleInput = Console.ReadLine(); } }