示例#1
0
        private void OnClientConnected(SocketAsyncEventArgs e)
        {
            var client = new SoundClient(e.AcceptSocket);

            m_clients.Add(client);

            var args = new SocketAsyncEventArgs();

            args.Completed += (sender, arg) => OnClientReceived(arg);
            args.SetBuffer(new byte[8192], 0, 8192);
            args.UserToken = client;

            if (!e.AcceptSocket.ReceiveAsync(args))
            {
                OnClientReceived(args);
            }

            var listenArgs = new SocketAsyncEventArgs();

            listenArgs.Completed += (sender, x) => OnClientConnected(x);
            if (!m_clientListener.AcceptAsync(listenArgs))
            {
                OnClientConnected(listenArgs);
            }
        }
示例#2
0
        private void OnRegReceived(SocketAsyncEventArgs e)
        {
            while (true)
            {
                if (e.BytesTransferred == 0 || e.SocketError != SocketError.Success)
                {
                    ((Socket)e.UserToken).Disconnect(false);
                }
                else
                {
                    var message = Encoding.ASCII.GetString(e.Buffer, e.Offset, e.BytesTransferred);

                    Debug.WriteLine("REG : " + message);

                    // example : main_client_is():1366402807812|
                    if (message.Contains("main_client_is():"))
                    {
                        var index = message.IndexOf("main_client_is():") + ("main_client_is():").Length;
                        var idStr = message.Substring(index, message.IndexOf("|", index) - index);

                        var id = long.Parse(idStr);

                        m_mainClient = m_clients.First(x => x.ID == id);
                    }

                    foreach (var soundClient in m_clients)
                    {
                        soundClient.Socket.Send(e.Buffer, e.Offset, e.BytesTransferred, SocketFlags.None);
                    }

                    if (!((Socket)e.UserToken).ReceiveAsync(e))
                    {
                        continue;
                    }
                }
                break;
            }
        }
示例#3
0
 private void RemoveClient(SoundClient client)
 {
     m_clients.Remove(client);
 }