void send_to_all(packet p) { byte[] raw = Packet.serialize_packet(p); foreach (var dest in clients) { socket.SendTo(raw, dest.addr); } }
static public byte [] serialize_packet(packet p) { MemoryStream stream = new MemoryStream(); BinaryWriter bw = new BinaryWriter(stream); bw.Write((byte)p.id); switch (p.id) { case packet_type.ping: case packet_type.disconnect: break; case packet_type.connect: bw.Write(((packet_connect)p).name); break; case packet_type.move: bw.Write(((packet_move)p).speed_x); bw.Write(((packet_move)p).speed_y); break; case packet_type.position: bw.Write(((packet_position)p).name); bw.Write(((packet_position)p).x); bw.Write(((packet_position)p).y); break; case packet_type.remove_client: bw.Write(((packet_remove_client)p).name); break; case packet_type.violation: bw.Write(((packet_violation)p).msg); break; case packet_type.equ8_data: bw.Write(((packet_equ8_data)p).data.Length); bw.Write(((packet_equ8_data)p).data); break; default: throw new System.ArgumentException("Unknown packet id"); } return(stream.ToArray()); }
void update_clients(IPEndPoint from, packet p) { string key = from.ToString(); game_client gc = null; foreach (var c in clients) { if (c.key() == key) { gc = c; } } if (null == gc) { if (packet_type.connect == p.id) { string unique_id = ((packet_connect)p).name; var status = equ8.session_manager.user_status(unique_id); if (!user_is_allowed_to_play(from, unique_id, status)) { Console.WriteLine( "Client '{0}' not allowed to play (status: {1})", unique_id, status); return; } gc = new game_client(from, unique_id, random.Next(0, 80), random.Next(0, 25)); clients.Add(gc); Console.WriteLine("Client connect '{0}'", unique_id); send_positions(); } return; } switch (p.id) { case packet_type.equ8_data: var payload = ((packet_equ8_data)p).data; Console.WriteLine("EQU8 Received Data: user: '******' size: {1}", gc.unique_id(), payload.Length); equ8.session_manager.on_data_received(gc.unique_id(), payload); break; case packet_type.disconnect: remove_client(gc.key()); return; case packet_type.move: if (null != gc) { gc.move_x(((packet_move)p).speed_x); gc.move_y(((packet_move)p).speed_y); } break; case packet_type.ping: if (null != gc) { gc.ping_ok = true; } return; } send_positions(); }
void send_packet(game_client c, packet p) { send_packet(c.addr, p); }
void send_packet(IPEndPoint addr, packet p) { byte[] raw = Packet.serialize_packet(p); socket.SendTo(raw, addr); }