/// <summary> /// Read this message from the packet buffer /// </summary> /// <returns>new read pointer position</returns> internal void ReadFrom(NetBuffer buffer, IPEndPoint endpoint) { m_senderEndPoint = endpoint; // read header byte header = buffer.ReadByte(); m_type = (NetMessageLibraryType)(header & 7); m_sequenceChannel = (NetChannel)(header >> 3); m_sequenceNumber = buffer.ReadUInt16(); int payLen = (int)buffer.ReadVariableUInt32(); // copy payload into message buffer m_data.EnsureBufferSize(payLen * 8); buffer.ReadBytes(m_data.Data, 0, payLen); m_data.Reset(0, payLen * 8); return; }
/// <summary> /// Read this message from the packet buffer /// </summary> /// <returns>new read pointer position</returns> internal bool ReadFrom(NetBuffer buffer, NetworkEndPoint endpoint) { m_senderEndPoint = endpoint; // read header byte header; // ignore zero padding in the beginning until we get something non-zero do { if (buffer.BytesRemaining == 0) { return(false); } header = buffer.ReadByte(); } while (header == 0); m_type = (NetMessageLibraryType)(header & 7); m_sequenceChannel = (NetChannel)(header >> 3); //TODO: do not read seqno for unreliable, just set it to 0 (DAVID) m_sequenceNumber = buffer.ReadUInt16(); int payLen = (int)buffer.ReadVariableUInt32(); if (payLen > buffer.BytesRemaining) { return(false); // bad packet } // copy payload into message buffer m_data.EnsureBufferSizeInBytes(payLen); buffer.ReadBytes(m_data.Data, 0, payLen); m_data.Reset(0, payLen * 8); return(true); }
/// <summary> /// Handle incoming message /// </summary> private void HandleMessage(NetMessageType type, NetBuffer buffer) { switch (type) { case NetMessageType.DebugMessage: // // it's a library debug message; just display it in the console if debugger is attached // Console.WriteLine(buffer.ReadString()); break; case NetMessageType.StatusChanged: // // it's a status change message; set the reason as window title and refresh picture // this.Text = buffer.ReadString(); pictureBox1.Refresh(); break; case NetMessageType.ServerDiscovered: // // it's a server discovered message; connect to the discovered server // m_imageWidth = 0; m_imageHeight = 0; m_lineDisplayed = 0; m_client.Connect(buffer.ReadIPEndPoint()); break; case NetMessageType.Data: // // it's a data message (data sent from the server) // if (m_imageWidth == 0) { // first message is size m_imageWidth = (int)buffer.ReadVariableUInt32(); m_imageHeight = (int)buffer.ReadVariableUInt32(); this.Size = new System.Drawing.Size(m_imageWidth + 40, m_imageHeight + 60); pictureBox1.Image = new Bitmap(m_imageWidth, m_imageHeight); pictureBox1.SetBounds(12, 12, m_imageWidth, m_imageHeight); return; } uint pixelPos = buffer.ReadUInt32(); // it's color data int y = (int)(pixelPos / m_imageWidth); int x = (int)(pixelPos - (y * m_imageWidth)); Bitmap bm = pictureBox1.Image as Bitmap; pictureBox1.SuspendLayout(); int pixels = (buffer.LengthBytes - 4) / 3; for (int i = 0; i < pixels; i++) { // set pixel byte r = buffer.ReadByte(); byte g = buffer.ReadByte(); byte b = buffer.ReadByte(); Color col = Color.FromArgb(r, g, b); if (y > m_imageHeight) continue; bm.SetPixel(x, y, col); x++; if (x >= m_imageWidth) { x = 0; y++; } } pictureBox1.ResumeLayout(); // refresh image every horizontal line if (pixelPos / m_imageWidth > m_lineDisplayed) { m_lineDisplayed = (int)(pixelPos / m_imageWidth); pictureBox1.Refresh(); } break; default: // unhandled break; } }