/// <summary> /// Update broadcast information. /// </summary> private void UpdateBroadcastInfo() { var msg = new PirateMessage(PirateMessageHead.Bcst, PirateMessage.ConstructHostInfo(this)); this.Broadcaster.Message = msg.GetBytes(); }
/// <summary> /// Send message to a client. /// </summary> /// <param name="pclient">Client to send to.</param> /// <param name="msg">Message to send.</param> public void SendMessage(PirateClient pclient, PirateMessage msg) { Contract.Requires(pclient != null && msg != null); try { byte[] buffer = msg.GetBytes(); pclient.Socket.BeginSend( buffer, 0, buffer.Length, SocketFlags.None, MessageSent, new PirateMessageObj(pclient, msg)); } catch(SocketException ex) { if(!IgnoreSocketErrors.Contains(ex.SocketErrorCode)) Console.WriteLine("SocketException:" + ex); this.SocketDisconnect(pclient); }catch(Exception ex){ Console.WriteLine(ex); } }
/// <summary> /// Send socket message asynchronously. /// </summary> /// <param name="msg">The message to send.</param> public void SendMessage(PirateMessage msg) { Contract.Requires(msg != null); var buffer = msg.GetBytes(); Socket.BeginSend(buffer, 0, buffer.Length, SocketFlags.None, MessageSent, msg); }
/// <summary> /// Check if host is available. /// </summary> /// <param name="socket">The socket to communicate through.</param> /// <returns>True if host is available, false if not.</returns> public static bool KnockKnock(Socket socket) { Contract.Requires(socket != null); var knock = new PirateMessage(PirateMessageHead.Knck, ""); socket.Send(knock.GetBytes()); var buffer = new byte[PirateMessage.BufferSize]; var read = socket.Receive(buffer); return read > 4 && PirateMessage.GetMessages(buffer, read).Any(msg => msg.Head == PirateMessageHead.Knck); }
/// <summary> /// Constructor. /// </summary> /// <param name="client">The client.</param> /// <param name="msg">The message to get the byte buffer from.</param> public PirateMessageObj(PirateClient client, PirateMessage msg) { Contract.Requires(client != null && msg != null); this.Client = client; this.Buffer = msg.GetBytes(); }