示例#1
0
        public static void Server()
        {
            RuffleSocket socket = new RuffleSocket(new Configuration.SocketConfig()
            {
                AllowBroadcasts          = true,
                AllowUnconnectedMessages = true,
                DualListenPort           = 5555
            });

            socket.Start();

            while (true)
            {
                // Wait for message. This is to prevent a tight loop
                socket.SyncronizationEvent.WaitOne(1000);

                NetworkEvent @event;
                while ((@event = socket.Poll()).Type != NetworkEventType.Nothing)
                {
                    if (@event.Type == NetworkEventType.BroadcastData)
                    {
                        // We got a broadcast. Reply to them with the same token they used
                        socket.SendUnconnected(@event.Data, (IPEndPoint)@event.EndPoint);
                    }

                    // Recycle the event
                    @event.Recycle();
                }
            }
        }
        /// <summary>
        /// Sends bytes to endpoint.
        /// </summary>
        /// <returns>The bytes sent. 0 or less if failed.</returns>
        /// <param name="buffer">The buffer to send.</param>
        /// <param name="offset">The offset of the buffer to start sending at.</param>
        /// <param name="length">The length to send from the buffer.</param>
        /// <param name="timeoutMs">The operation timeout in milliseconds.</param>
        /// <param name="endpoint">The endpoint to send to.</param>
        public int SendTo(byte[] buffer, int offset, int length, int timeoutMs, IPEndPoint endpoint)
        {
            if (socket != null)
            {
                var ars = new ArraySegment <byte>(buffer, offset, length);
                if (socket.SendUnconnected(ars, endpoint))
                {
                    return(buffer.Length);
                }
            }

            return(-1);
        }