Esempio n. 1
0
//JAVA TO C# CONVERTER WARNING: Method 'throws' clauses are not available in .NET:
//ORIGINAL LINE: @Override public int receive(byte[] buffer, int size) throws java.io.IOException
        public override int receive(sbyte[] buffer, int size)
        {
            DatagramPacket packet = new DatagramPacket(buffer, size);

            socket.receive(packet);
            ReceivedPort    = packet.Port;
            ReceivedAddress = packet.Address;

            return(packet.Length);
        }
Esempio n. 2
0
        private bool receiveAccessPointMessage()
        {
            bool packetReceived = false;

            if (!createAccessPointSocket())
            {
                return(packetReceived);
            }

            sbyte[]        bytes  = new sbyte[10000];
            DatagramPacket packet = new DatagramPacket(bytes, bytes.Length);

            try
            {
                apSocket.receive(packet);
                //if (log.DebugEnabled)
                {
                    Console.WriteLine(string.Format("receiveMessage message: {0}", Utilities.getMemoryDump(packet.Data, packet.Offset, packet.Length)));
                }

                packetReceived = true;

                sbyte[]   dataBytes  = packet.Data;
                int       dataOffset = packet.Offset;
                int       dataLength = packet.Length;
                NetPacket netPacket  = new NetPacket(dataBytes, dataOffset, dataLength);

                processMessage(netPacket);
            }
            catch (SocketTimeoutException)
            {
                // Timeout can be ignored as we are polling
            }
            catch (IOException e)
            {
                Console.WriteLine("receiveMessage", e);
            }

            return(packetReceived);
        }
Esempio n. 3
0
        static string GetMSSqlPort(string instanceName, string dataSource, int timeout)
        {
            string port = String.Empty;

            try {
                DatagramSocket socket = new DatagramSocket();

                // send request
                sbyte[]        buf     = new sbyte[] { 2 };
                InetAddress    address = InetAddress.getByName(dataSource);
                DatagramPacket packet  = new DatagramPacket(buf, buf.Length, address, 1434);
                socket.send(packet);
                sbyte[] recbuf = new sbyte[1024];
                packet = new DatagramPacket(recbuf, recbuf.Length, packet.getAddress(), packet.getPort());

                // try to receive from socket while increasing timeouts in geometric progression
                int iterationTimeout = 1;
                int totalTimeout     = 0;
                for (;;)
                {
                    socket.setSoTimeout(iterationTimeout);
                    try {
                        socket.receive(packet);
                        break;
                    }
                    catch (SocketTimeoutException e) {
                        totalTimeout     += iterationTimeout;
                        iterationTimeout *= 2;
                        if (totalTimeout >= timeout * 1000)
                        {
                            throw new java.sql.SQLException(
                                      String.Format("Unable to retrieve the port number for {0} using UDP on port 1434. Please see your network administrator to solve this problem or add the port number of your SQL server instance to your connection string (i.e. port=1433).", dataSource)
                                      );
                        }
                    }
                }
                sbyte[] rcvdSbytes = packet.getData();
                char[]  rcvdChars  = new char[rcvdSbytes.Length];
                for (int i = 0; i < rcvdSbytes.Length; i++)
                {
                    rcvdChars[i] = (char)rcvdSbytes[i];
                }
                String received = new String(rcvdChars);

                java.util.StringTokenizer st = new java.util.StringTokenizer(received, ";");
                String prev            = "";
                bool   instanceReached = instanceName == null || instanceName.Length == 0;
                while (st.hasMoreTokens())
                {
                    if (!instanceReached)
                    {
                        if (prev.Trim().Equals("InstanceName"))
                        {
                            if (String.Compare(instanceName, st.nextToken().Trim(), true, CultureInfo.InvariantCulture) == 0)
                            {
                                instanceReached = true;
                            }
                        }
                    }
                    else
                    {
                        if (prev.Trim().Equals("tcp"))
                        {
                            port = st.nextToken().Trim();
                            //ensure we got a valid int
                            java.lang.Integer.parseInt(port);
                            break;
                        }
                    }
                    prev = st.nextToken();
                }
                socket.close();

                if (!instanceReached)
                {
                    throw new java.sql.SQLException(
                              String.Format("Specified SQL Server '{0}\\{1}' not found.", dataSource, instanceName)
                              );
                }
                return(port);
            }
            catch (java.sql.SQLException) {
                throw;
            }
            catch (Exception e) {
                throw new java.sql.SQLException(e.Message);
            }
        }
Esempio n. 4
0
        private void pollPeers()
        {
            try
            {
                openSocket();

                // Poll all the available messages.
                // Exiting the loop only when no more messages are available (SocketTimeoutException)
                while (true)
                {
                    sbyte[]        bytes  = new sbyte[JpcspAdhocctlMessage.MessageLength];
                    DatagramPacket packet = new DatagramPacket(bytes, bytes.Length);
                    adhocctlSocket.receive(packet);
                    JpcspAdhocctlMessage adhocctlMessage = new JpcspAdhocctlMessage(packet.Data, packet.Length);

                    //if (log.DebugEnabled)
                    {
                        Console.WriteLine(string.Format("broadcast received from peer: {0}", adhocctlMessage));
                    }

                    // Ignore messages coming from myself
                    if (!sceNetAdhoc.isSameMacAddress(Wlan.MacAddress, adhocctlMessage.macAddress))
                    {
                        if (adhocctlMessage.groupName.Equals(sceNetAdhocctlModule.hleNetAdhocctlGetGroupName()))
                        {
                            sceNetAdhocctlModule.hleNetAdhocctlAddPeer(adhocctlMessage.nickName, new pspNetMacAddress(adhocctlMessage.macAddress));
                        }

                        if (adhocctlMessage.ibss.Equals(sceNetAdhocctlModule.hleNetAdhocctlGetIBSS()))
                        {
                            sceNetAdhocctlModule.hleNetAdhocctlAddNetwork(adhocctlMessage.groupName, new pspNetMacAddress(adhocctlMessage.macAddress), adhocctlMessage.channel, adhocctlMessage.ibss, adhocctlMessage.mode);

                            if (adhocctlMessage.mode == PSP_ADHOCCTL_MODE_GAMEMODE)
                            {
                                sceNetAdhocctlModule.hleNetAdhocctlAddGameModeMac(adhocctlMessage.macAddress);
                                if (sceNetAdhocctlModule.hleNetAdhocctlGetRequiredGameModeMacs().size() <= 0)
                                {
                                    sceNetAdhocctlModule.hleNetAdhocctlSetGameModeJoinComplete(adhocctlMessage.gameModeComplete);
                                    if (adhocctlMessage.gameModeComplete)
                                    {
                                        sbyte[][] macs = adhocctlMessage.gameModeMacs;
                                        if (macs != null)
                                        {
                                            sceNetAdhocctlModule.hleNetAdhocctlSetGameModeMacs(macs);
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
            catch (SocketException e)
            {
                Console.WriteLine("broadcastPeers", e);
            }
            catch (SocketTimeoutException)
            {
                // Nothing available
            }
            catch (IOException e)
            {
                Console.WriteLine("broadcastPeers", e);
            }
        }