Exemple #1
0
 public DebugConnection(SynchronizationContext eventContext, string host, int port)
 {
     this.IsConnected  = false;
     this.EventContext = eventContext;
     this.Host         = host;
     this.Port         = port;
     VMState           = null;
 }
Exemple #2
0
        private void HandlePacket(PacketHeader header, System.IO.BinaryReader payloadReader)
        {
            System.Diagnostics.Debug.WriteLine("Handling packet of type " + header.Type.ToString());

            if (header.Type == PayloadType.ServerConnected)
            {
                IsConnected = true;
                lock (VMSyncObject)
                {
                    VMState = new VMState();
                }
                ConnectedEvent.Set();
            }
            else if (header.Type == PayloadType.ServerDisconnect)
            {
                if (!IsConnected)
                {
                    throw new Exception("Connection attempt was rejected.");
                }
                else
                {
                    this.ClientConnection.Close();
                }
            }
            else if (header.Type == PayloadType.ServerDebugOutput)
            {
                this.ServerDebugOutput?.Invoke(this, new ServerDebugOutputPayload(payloadReader).Output);
            }
            else if (header.Type == PayloadType.ServerException)
            {
                ServerExceptionPayload payload = new ServerExceptionPayload(payloadReader);
                this.ServerException?.Invoke(this, payload.Output);
            }
            else if (header.Type == PayloadType.ServerStateChange)
            {
                lock (VMSyncObject)
                {
                    VMState.OnExecutionStateChanged(this, new ServerStateChangePayload(payloadReader));
                }
            }
            else
            {
                payloadReader.ReadBytes(header.PayloadLength);
                System.Diagnostics.Debug.WriteLine("Recieved unimplemented packet of type " + header.Type.ToString());
            }
        }
Exemple #3
0
        private void ListenThreadEntrypoint()
        {
            System.IO.MemoryStream ms     = new System.IO.MemoryStream();
            System.IO.BinaryReader reader = new System.IO.BinaryReader(ms);
            try
            {
                this.ClientConnection         = new TcpClient(this.Host, this.Port);
                this.ClientConnection.NoDelay = true;

                lock (StateSyncObject)
                    Writer = new System.IO.BinaryWriter(this.ClientConnection.GetStream());

                //Thread.Sleep(3000);

                SendPacket(new PacketHeader(0, PayloadType.ClientConnection), new ClientConnectionPayload(ClientConnectionPayload.CurrentVersionMajor, ClientConnectionPayload.CurrentVersionMinor));
                int read = 1;
                while (read > 0)
                {
                    ms.SetLength(4);
                    ms.Position = 0;
                    while (ms.Position < 4)
                    {
                        read         = this.ClientConnection.GetStream().Read(ms.GetBuffer(), (int)ms.Position, (int)ms.Length - (int)ms.Position);
                        ms.Position += read;
                        if (read <= 0)
                        {
                            break;
                        }
                    }
                    if (read <= 0)
                    {
                        break;
                    }

                    //ms.SetLength(read);
                    ms.Position = 0;
                    PacketHeader header = new PacketHeader(reader);
                    ms.Position = 0;

                    if (header.PayloadLength > 0)
                    {
                        ms.SetLength(header.PayloadLength);
                        while (ms.Position < header.PayloadLength)
                        {
                            read         = this.ClientConnection.GetStream().Read(ms.GetBuffer(), (int)ms.Position, (int)ms.Length - (int)ms.Position);
                            ms.Position += read;
                            if (read <= 0)
                            {
                                break;
                            }
                        }
                        ms.Position = 0;
                        if (read <= 0)
                        {
                            break;
                        }
                    }

                    if (header.Type == PayloadType.ServerDisconnect)
                    {
                        break;
                    }
                    else
                    {
                        HandlePacket(header, reader);
                    }
                    ms.Position = 0;
                }
            }
            catch (Exception ex)
            {
                System.Diagnostics.Debug.WriteLine("Exception in ListenThreadEntrypoint: " + ex.ToString());
            }
            if (!this.IsConnected)
            {
                this.ConnectedEvent.Set();
            }
            this.ClientConnection?.Close();
            this.ClientConnection?.Dispose();
            System.Diagnostics.Debug.WriteLine("Connection closed.");
            this.IsConnected = false;
            //this.ServerDisconnect?.Invo
            //EventContext.Send((state) => { this.ServerDisconnect?.Invoke(null, null); }, null);
            lock (VMSyncObject)
            {
                VMState = null;
            }
            this.ServerDisconnect?.Invoke(this, new EventArgs());
            this.ShutdownEvent.Set();
            ms.Dispose();
            reader.Dispose();
        }