public void SendPacket(NetCommands command) { Packet sending = new Packet(); sending.Write((uint)command); peer.Send(0, sending.Bytes); }
private void HandlePacket(NetCommands command, Packet packet) { switch (command) { case NetCommands.IdentifySuccessful: isIdentified = true; Log("[NetLib] Identify successful"); OnIdentificationSuccess(); OnIdentificationSuccessEvent?.Invoke(); break; case NetCommands.IdentifyFailure: isIdentified = false; Log("[NetLib] Identify failure: The server didn't accept your access token! It might be already in use or it has expired. Please retry logging in."); var failuerCode = (IdentifyResponse)packet.ReadUint(); OnIdentificationFailure(failuerCode); OnIdentificationFailureEvent?.Invoke(failuerCode); break; case NetCommands.HandshakeSuccess: Log("[NetLib] Handshake successful"); var identificationPacket = GetIdentity(); SendPacket(NetCommands.Identify, identificationPacket); Log("[NetLib] Identifying..."); break; default: Log("[NetLib] command was not implemented"); break; } }
public void Send(NetCommands cmd, byte[] data) { if (socket.Clients.Count == 0) { return; } if (hosting && cmd == NetCommands.Image) { imageReady = false; } MemoryStream packet = new MemoryStream(); BinaryWriter bw = new BinaryWriter(packet); bw.Write((byte)cmd); bw.Write(data.Length); if (data.Length > 0) { bw.Write(data); } bw.Flush(); bw.Close(); packet.Flush(); socket.SendToAll(packet.ToArray()); packet.Dispose(); }
private void processCommand(NetCommands cmd, byte[] data) { switch (cmd) { case NetCommands.Image: int currentX = 0; int currentY = 0; for (int i = 0; i < data.Length; i++) { C64.vic.Screen[currentX, currentY] = C64.palette.MapColorValue((byte)(data[i] >> 4)); currentX++; if (currentX == 403) { currentX = 0; currentY++; } C64.vic.Screen[currentX, currentY] = C64.palette.MapColorValue((byte)(data[i] & 0xF)); currentX++; if (currentX == 403) { currentX = 0; currentY++; } } screenReceived(this, null); socket.SendToAll(new byte[] { (byte)NetCommands.ImageReady, 0, 0, 0, 0 }); break; case NetCommands.ImageReady: imageReady = true; break; case NetCommands.KeyEvent: KeyboardEventArgs kd = new KeyboardEventArgs( (Key)data[0], (ModifierKeys)(data[1] != 0 ? Key.LeftShift : 0), data[2] == 1 ); keyEventReceived(null, kd); break; case NetCommands.JoyEvent: JoyEventArgs jd = new JoyEventArgs( data[0], (JoystickFunction)data[1], data[2] == 1 ); joyEventReceived(null, jd); break; } }
private void socket_ReceivedCommand(object sender, NetCommands cmd, byte[] data) { processCommand(cmd, data); }