private void OnClientClosed(NetIQServerClient ctx) { //Remove existing stream, if any if (ctx.stream != null) { lock (streams) streams.Remove(ctx.stream); } }
private void OnClientReceiveData(IAsyncResult ar) { //Get data NetIQServerClient ctx = (NetIQServerClient)ar.AsyncState; int read = ctx.sock.EndReceive(ar); //Handle try { OnClientCommand(ctx, ctx.buffer, read); } catch (Exception ex) { //Unknown error. Close connection ctx.sock.Close(); OnClientClosed(ctx); return; } //Listen for next ctx.sock.BeginReceive(ctx.buffer, 0, ctx.buffer.Length, SocketFlags.None, OnClientReceiveData, ctx); }
private void OnClientConnect(IAsyncResult ar) { //Get socket Socket client = sock.EndAccept(ar); //Create client context NetIQServerClient ctx = new NetIQServerClient(client); //Create and send info packet NetIQCommandServerInfo info = new NetIQCommandServerInfo(); info.ServerVersionMinor = NetIQUtil.PROTO_VERSION_MINOR; info.ServerVersionMajor = NetIQUtil.PROTO_VERSION_MAJOR; info.SampleRate = sampleRate; info.SendOnSocket(client); //Begin listening on this client.BeginReceive(ctx.buffer, 0, ctx.buffer.Length, SocketFlags.None, OnClientReceiveData, ctx); //Look for another new client sock.BeginAccept(OnClientConnect, null); }
private void OnClientCommand(NetIQServerClient ctx, byte[] data, int len) { //Get opcode NetIQOpcode op = (NetIQOpcode)BitConverter.ToUInt16(data, 0); //Switch switch (op) { case NetIQOpcode.OPEN_STREAM: //Decode command NetIQCommandOpenStream cmd = new NetIQCommandOpenStream(data); //Get endpoint to send to IPAddress addr = ((IPEndPoint)ctx.sock.RemoteEndPoint).Address; IPEndPoint ep = new IPEndPoint(addr, cmd.StreamPort); //Make stream NetIQServerStream stream = new NetIQServerStream(ep, cmd.BufferSize, cmd.SampleFormat); //Apply to list lock (streams) { //Remove existing stream, if any if (ctx.stream != null) { streams.Remove(ctx.stream); } //Add streams.Add(stream); ctx.stream = stream; } break; default: throw new Exception("Unknown opcode!"); } }