Exemplo n.º 1
0
        public AtemServerConnection FindOrCreateConnection(EndPoint ep, out bool isNew)
        {
            lock (connections)
            {
                AtemServerConnection val;
                if (connections.TryGetValue(ep, out val))
                {
                    isNew = false;
                    return(val);
                }

                val               = new AtemServerConnection(ep, 0x8008);// TODO - make dynamic
                connections[ep]   = val;
                val.OnDisconnect += RemoveTimedOut;

                Log.InfoFormat("New connection from {0}", ep);

                isNew = true;
                return(val);
            }
        }
Exemplo n.º 2
0
        public void StartReceive()
        {
            _socket = CreateSocket();

            var thread = new Thread(async() =>
            {
                while (true)
                {
                    try
                    {
                        //Start receiving data
                        ArraySegment <byte> buff = new ArraySegment <byte>(new byte[2500]);
                        var end = new IPEndPoint(IPAddress.Any, 0);
                        SocketReceiveFromResult v = await _socket.ReceiveFromAsync(buff, SocketFlags.None, end);

                        AtemServerConnection conn = _connections.FindOrCreateConnection(v.RemoteEndPoint, out _);
                        if (conn == null)
                        {
                            continue;
                        }

                        byte[] buffer = buff.Array;
                        var packet    = new ReceivedPacket(buffer);

                        if (packet.CommandCode.HasFlag(ReceivedPacket.CommandCodeFlags.Handshake))
                        {
                            conn.ResetConnStatsInfo();
                            // send handshake back
                            byte[] test =
                            {
                                buffer[0], buffer[1], // flags + length
                                buffer[2], buffer[3], // session id
                                0x00, 0x00,           // acked pkt id
                                0x00, 0x00,           // retransmit request
                                buffer[8], buffer[9], // unknown2
                                0x00, 0x00,           // server pkt id
                                0x02, 0x00, 0x00, 0x08, 0x00, 0x00, 0x00, 0x00
                            };

                            var sendThread = new Thread(o =>
                            {
                                while (!conn.HasTimedOut)
                                {
                                    conn.TrySendQueued(_socket);
                                    Task.Delay(1).Wait();
                                }
                                Console.WriteLine("send finished");
                            });
                            sendThread.Start();

                            await _socket.SendToAsync(new ArraySegment <byte>(test, 0, 20), SocketFlags.None, v.RemoteEndPoint);

                            continue;
                        }

                        if (!conn.IsOpened)
                        {
                            var recvThread = new Thread(o =>
                            {
                                while (!conn.HasTimedOut || conn.HasCommandsToProcess)
                                {
                                    List <ICommand> cmds = conn.GetNextCommands();

                                    Log.DebugFormat("Recieved {0} commands", cmds.Count);
                                    //conn.HandleInner(_state, connection, cmds);
                                }
                            });
                            recvThread.Start();
                        }
                        conn.Receive(_socket, packet);

                        if (conn.ReadyForData)
                        {
                            QueueDataDumps(conn);
                        }
                    }
                    catch (SocketException)
                    {
                        // Reinit the socket as it is now unavailable
                        //_socket = CreateSocket();
                    }
                }
            });

            thread.Start();
        }