Example #1
0
            public void ClientDataReceptionCallback(IAsyncResult ar)
            {
                try
                {
                    int bytesRead = 0;

                    // Read data from the client socket.
                    try
                    {
                        bytesRead = this._BroadCastlisteningSocket.EndReceive(ar);
                    }
                    catch (Exception)
                    {
                    }

                    if (bytesRead > 0)
                    {
                        byte[] RealBuffer = new byte[bytesRead - 1 + 1];

                        System.Array.Copy(this._ReceiveDataBuffer, RealBuffer, bytesRead);

                        if (!(RealBuffer == null))
                        {
                            Services.BroadCasting.Handling.DataBroadCastHandler receivedBroadCastHandler = Services.BroadCasting.Handling.DataBroadCastHandler.Deserialize(RealBuffer);

                            try
                            {
                                if (DataReceivedEvent != null)
                                {
                                    DataReceivedEvent(receivedBroadCastHandler);
                                }
                            }
                            catch (Exception)
                            {
                            }
                        }

                        System.Array.Clear(this._ReceiveDataBuffer, 0, this._ReceiveDataBuffer.Length);

                        this._BroadCastlisteningSocket.BeginReceive(this._ReceiveDataBuffer, 0, this._ReceiveDataBuffer.Length, (System.Net.Sockets.SocketFlags) 0, new AsyncCallback(ClientDataReceptionCallback), null);
                    }
                }
                catch (SocketException ex)
                {
                    CustomEventLog.WriteEntry(EventLogEntryType.Error, ex.ToString());
                }
                catch (Exception ex)
                {
                    CustomEventLog.WriteEntry(EventLogEntryType.Error, ex.ToString());
                }
            }
            private void broadcastDataReceived(Services.BroadCasting.Handling.DataBroadCastHandler broadcastDataReceptionHandler)
            {
                bool   raiseTheSuccesfullQueryEvent = false;
                string serviceNameBeingLooking      = "UNDEFINED";

                this._clientQueriesReceived++;

                if (broadcastDataReceptionHandler.BroadcastingMode == Services.BroadCasting.Handling.DataBroadCastHandler.BroadCastMode.BroadCasterIsWaitingReply)
                {
                    //evaluates if the received broadcast corresponds to a event to find a STX network service over the network
                    if (broadcastDataReceptionHandler.broadcastIDName == DiscoverableServiceHandlingOperativeDefs.SERVICE_NAME_QUERY_BROADCAST_ID_NAME_STRING)
                    {
                        //because the service name comes in the broadcasted data in the dataname evaluates
                        //if the dataname received correspond to the estandard dataname defined to
                        //retrieve the service name who is being lookig for a broadcast client
                        if (broadcastDataReceptionHandler.BroadCastedData.DataName == DiscoverableServiceHandlingOperativeDefs.SERVICE_NAME)
                        {
                            serviceNameBeingLooking = System.Convert.ToString(broadcastDataReceptionHandler.BroadCastedData.Value);
                            serviceNameBeingLooking = serviceNameBeingLooking.ToUpper();
                            //operation to evaluate if the service being looked corresponds to the service
                            //name defined within the handler

                            if (this.ServiceName == serviceNameBeingLooking)
                            {
                                //the local service corresponds to the query, so the handler replyes
                                // the service parameters list to the broadcaster
                                try
                                {
                                    BroadCastReply serviceReply = new BroadCastReply(DiscoverableServiceHandlingOperativeDefs.SERVICE_NAME_QUERY_REPLYIDNAME_STRING, DiscoverableServiceHandlingOperativeDefs.SERVICE_PARAMETERS_TABLE, this._serviceParameters.ParametersTable);
                                    broadcastDataReceptionHandler.SendReplyToBroadcaster(serviceReply);
                                    raiseTheSuccesfullQueryEvent = true;
                                    this._succesfullQueriesResulted++;
                                }
                                catch (Exception ex)
                                {
                                    CustomEventLog.WriteEntry(EventLogEntryType.Error, ex.ToString());
                                }
                            }
                        }
                    }
                }
                else
                {
                    //save the error that the object can respond to a reply from the broadcast client
                }

                try
                {
                    if (STXServiceQueryReceivedEvent != null)
                    {
                        STXServiceQueryReceivedEvent(serviceNameBeingLooking, broadcastDataReceptionHandler.BroadCastedData.BroadCasterInformation.BroadcasterName, broadcastDataReceptionHandler.BroadCastedData.BroadCasterInformation.Host);
                    }
                }
                catch (Exception)
                {
                }

                if (raiseTheSuccesfullQueryEvent)
                {
                    try
                    {
                        if (STXServiceSuccesfullQueryResultEvent != null)
                        {
                            STXServiceSuccesfullQueryResultEvent(serviceNameBeingLooking, broadcastDataReceptionHandler.BroadCastedData.BroadCasterInformation.BroadcasterName, broadcastDataReceptionHandler.BroadCastedData.BroadCasterInformation.Host);
                        }
                    }
                    catch (Exception)
                    {
                    }
                }
            }