Пример #1
0
        private void client_dataReceived(object sender, IncomingMessageEventArgs e)
        {
            // Check to see if the client is still connected.
            if (!Connected)
            {
                // The client has disconnected.

                if (e == null) { return; } // Don't give listeners of dataReceived a null IncomingMessageEventArgs.
            }

            if (startedRequest && e.PacketType == expectedResponse)
            {
                // This packet is a response to an open request.
                response = e.Message;
                startedRequest = false; // Lower the unfulfilled flag.
                return;
            }

            switch ((PacketType)e.PacketType)
            {
                case PacketType.RequestList:
                    // Deserialize the tracklist and pass it up.
                    AudioListReceived.Invoke(this, (TrackList)ObjectSerialization.Deserialize(e.Message, typeof(TrackList)));
                    break;
                case PacketType.AudioSegment:
                    // TODO: Implement audio streaming.
                    break;
                default:
                    break;
            }

            //dataReceived.Invoke(sender, e);
        }
Пример #2
0
        private void server_dataReceived(object sender, IncomingMessageEventArgs e)
        {
            // Invoke on the listbox thread if needed (yes).
            if (messagesRichTextBox.InvokeRequired)
            {
                messagesRichTextBox.Invoke(new MethodInvoker(delegate // Invoke a generic delegate using MethodInvoker
                {
                    server_dataReceived(sender, e);
                }));
                return;
            }

            if (e == null)
            {
                // This is probably a disconnect.
                return;
            }

            if (e.Error != null)
            {
                var errClient = (TcpTransmission.Client.ClientConnection)sender;
                messagesRichTextBox.AppendText("Error on client " + errClient.RemoteEndPoint.ToString() + " : " + e.Error.Message + "\n");
            }

            if (e.Message != null)
            {
                ASCIIEncoding encoder = new ASCIIEncoding();
                string source = ((TcpTransmission.Client.ClientConnection)sender).RemoteEndPoint.ToString();
                string message = encoder.GetString(e.Message, 0, e.Message.Length);

                messagesRichTextBox.AppendText(source + ": " + message + "\n");

            }
        }
Пример #3
0
 // Wrap the client events so the server owner can catch them.
 protected override void DataReceivedHandle(ClientConnection sender, IncomingMessageEventArgs e)
 {
     // If client already authenticated.
     if (authenticatedConnections.ContainsKey(sender.RemoteEndPoint))
     {
         switch ((PacketType)e.PacketType)
         {
             case PacketType.AudioSegment:
                 authenticatedConnections[sender.RemoteEndPoint].ContinueStream();
                 break;
             case PacketType.RequestList:
                 GeneralEvent.Invoke(this, new GeneralEventArgs(authenticatedConnections[sender.RemoteEndPoint].Username,
                     sender.RemoteEndPoint, ("Requested a new track list.")));
                 authenticatedConnections[sender.RemoteEndPoint].RequestUpdateList();
                 break;
             case PacketType.StartAudioStream:
                 byte[] requestReply = null;
                 BeginStreamingAudio(sender.RemoteEndPoint, e.Message, out requestReply);
                 SendData(sender.RemoteEndPoint, requestReply, (byte)PacketType.StartAudioStream);
                 break;
             case PacketType.StopAudioStream:
                 authenticatedConnections[sender.RemoteEndPoint].EndStreaming();
                 break;
             case PacketType.UnknownPacket:
                 GeneralEvent.Invoke(this, new GeneralEventArgs(authenticatedConnections[sender.RemoteEndPoint].Username,
                     sender.RemoteEndPoint, "Made a request to close the connection."));
                 DropClient(sender.RemoteEndPoint);
                 break;
             default:
                 GeneralEvent.Invoke(this, new GeneralEventArgs(authenticatedConnections[sender.RemoteEndPoint].Username,
                     sender.RemoteEndPoint, ("Made a request with packet type " + ((PacketType)e.PacketType).ToString() + " which is invalid for this context.")));
                 break;
         }
     }
     else
     {
         // See if the client is trying to authenticate or register.
         byte[] send = null;
         switch ((PacketType)e.PacketType)
         {
             case PacketType.Authenticate:
                 // Authenticate this client.
                 AuthenticateClient(sender.RemoteEndPoint, e.Message, out send);
                 SendData(sender.RemoteEndPoint, send, (byte)PacketType.Authenticate);
                 break;
             case PacketType.Register:
                 // Register new credentials.
                 RegisterNewUser(sender.RemoteEndPoint, e.Message, out send);
                 SendData(sender.RemoteEndPoint, send, (byte)PacketType.Register);
                 break;
             case PacketType.UnknownPacket:
                 GeneralEvent.Invoke(this, new GeneralEventArgs(null, sender.RemoteEndPoint, "Made a request to close the connection."));
                 DropClient(sender.RemoteEndPoint);
                 break;
             default:
                 GeneralEvent.Invoke(this, new GeneralEventArgs(null, sender.RemoteEndPoint,
                     ("Made a request with packet type " + ((PacketType)e.PacketType).ToString() + " which is invalid for this context.")));
                 break;
         }
         if (send != null) { sender.SendDataPacket(ref send, e.PacketType); } // Reply with the same header and some data.
     }
 }